test.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Canvas标尺与网格</title>
  7. <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. body {
  15. font-family: 'Arial', sans-serif;
  16. background: #2c3e50;
  17. display: flex;
  18. justify-content: center;
  19. align-items: center;
  20. min-height: 100vh;
  21. padding: 20px;
  22. }
  23. #app {
  24. background: white;
  25. border-radius: 10px;
  26. box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  27. overflow: hidden;
  28. }
  29. .canvas-container {
  30. position: relative;
  31. overflow: hidden;
  32. }
  33. .rulers-container {
  34. position: relative;
  35. background: #ecf0f1;
  36. border-bottom: 1px solid #bdc3c7;
  37. border-right: 1px solid #bdc3c7;
  38. }
  39. .corner {
  40. position: absolute;
  41. top: 0;
  42. left: 0;
  43. width: 20px;
  44. height: 20px;
  45. background: #3498db;
  46. z-index: 10;
  47. }
  48. .horizontal-ruler {
  49. position: absolute;
  50. top: 0;
  51. left: 20px;
  52. height: 20px;
  53. border-bottom: 1px solid #bdc3c7;
  54. }
  55. .vertical-ruler {
  56. position: absolute;
  57. top: 20px;
  58. left: 0;
  59. width: 20px;
  60. border-right: 1px solid #bdc3c7;
  61. }
  62. .canvas-wrapper {
  63. position: absolute;
  64. top: 20px;
  65. left: 20px;
  66. overflow: visible;
  67. }
  68. .grid-canvas {
  69. background: white;
  70. cursor: crosshair;
  71. }
  72. .controls {
  73. padding: 15px;
  74. background: #f8f9fa;
  75. display: flex;
  76. justify-content: space-between;
  77. align-items: center;
  78. border-top: 1px solid #e9ecef;
  79. }
  80. .scale-info {
  81. font-weight: bold;
  82. color: #2c3e50;
  83. }
  84. button {
  85. padding: 8px 15px;
  86. background: #3498db;
  87. color: white;
  88. border: none;
  89. border-radius: 4px;
  90. cursor: pointer;
  91. transition: background 0.3s;
  92. }
  93. button:hover {
  94. background: #2980b9;
  95. }
  96. .instructions {
  97. color: #7f8c8d;
  98. font-size: 14px;
  99. margin-top: 10px;
  100. }
  101. </style>
  102. </head>
  103. <body>
  104. <div id="app">
  105. <div class="canvas-container">
  106. <div class="rulers-container" :style="{ width: containerWidth + 20 + 'px', height: containerHeight + 20 + 'px' }">
  107. <div class="corner"></div>
  108. <canvas
  109. ref="horizontalRuler"
  110. class="horizontal-ruler"
  111. :width="containerWidth"
  112. height="20"
  113. ></canvas>
  114. <canvas
  115. ref="verticalRuler"
  116. class="vertical-ruler"
  117. width="20"
  118. :height="containerHeight"
  119. ></canvas>
  120. <div class="canvas-wrapper">
  121. <canvas
  122. ref="gridCanvas"
  123. class="grid-canvas"
  124. :width="containerWidth"
  125. :height="containerHeight"
  126. @wheel="handleWheel"
  127. ></canvas>
  128. </div>
  129. </div>
  130. </div>
  131. <div class="controls">
  132. <div class="scale-info">缩放比例: {{ (scale * 100).toFixed(0) }}%</div>
  133. <div>
  134. <button @click="resetZoom">重置缩放</button>
  135. <button @click="zoomIn">放大</button>
  136. <button @click="zoomOut">缩小</button>
  137. </div>
  138. </div>
  139. <div class="instructions">使用鼠标滚轮进行缩放(以左上角为中心)</div>
  140. </div>
  141. <script>
  142. new Vue({
  143. el: '#app',
  144. data: {
  145. containerWidth: 600,
  146. containerHeight: 400,
  147. scale: 1,
  148. minScale: 0.2,
  149. maxScale: 5,
  150. gridSize: 20, // 网格基础大小
  151. offsetX: 0,
  152. offsetY: 0
  153. },
  154. mounted() {
  155. this.drawRulersAndGrid();
  156. window.addEventListener('resize', this.handleResize);
  157. },
  158. beforeDestroy() {
  159. window.removeEventListener('resize', this.handleResize);
  160. },
  161. methods: {
  162. drawRulersAndGrid() {
  163. this.drawHorizontalRuler();
  164. this.drawVerticalRuler();
  165. this.drawGrid();
  166. },
  167. drawHorizontalRuler() {
  168. const canvas = this.$refs.horizontalRuler;
  169. const ctx = canvas.getContext('2d');
  170. const width = canvas.width;
  171. ctx.clearRect(0, 0, width, 20);
  172. ctx.fillStyle = '#ecf0f1';
  173. ctx.fillRect(0, 0, width, 20);
  174. ctx.strokeStyle = '#bdc3c7';
  175. ctx.lineWidth = 1;
  176. ctx.beginPath();
  177. ctx.moveTo(0, 19.5);
  178. ctx.lineTo(width, 19.5);
  179. ctx.stroke();
  180. ctx.fillStyle = '#2c3e50';
  181. ctx.font = '10px Arial';
  182. const scaledGrid = this.gridSize * this.scale;
  183. const startValue = Math.floor(this.offsetX / scaledGrid) * scaledGrid;
  184. const endValue = startValue + width + scaledGrid;
  185. for (let x = startValue; x < endValue; x += scaledGrid) {
  186. const pixel = (x - this.offsetX) * this.scale;
  187. if (pixel < 0) continue;
  188. const tickSize = x % (scaledGrid * 5) === 0 ? 10 : 5;
  189. ctx.beginPath();
  190. ctx.moveTo(pixel + 0.5, 20 - tickSize);
  191. ctx.lineTo(pixel + 0.5, 20);
  192. ctx.stroke();
  193. if (x % (scaledGrid * 5) === 0) {
  194. ctx.fillText(Math.round(x / this.scale), pixel + 2, 9);
  195. }
  196. }
  197. },
  198. drawVerticalRuler() {
  199. const canvas = this.$refs.verticalRuler;
  200. const ctx = canvas.getContext('2d');
  201. const height = canvas.height;
  202. ctx.clearRect(0, 0, 20, height);
  203. ctx.fillStyle = '#ecf0f1';
  204. ctx.fillRect(0, 0, 20, height);
  205. ctx.strokeStyle = '#bdc3c7';
  206. ctx.lineWidth = 1;
  207. ctx.beginPath();
  208. ctx.moveTo(19.5, 0);
  209. ctx.lineTo(19.5, height);
  210. ctx.stroke();
  211. ctx.fillStyle = '#2c3e50';
  212. ctx.font = '10px Arial';
  213. const scaledGrid = this.gridSize * this.scale;
  214. const startValue = Math.floor(this.offsetY / scaledGrid) * scaledGrid;
  215. const endValue = startValue + height + scaledGrid;
  216. for (let y = startValue; y < endValue; y += scaledGrid) {
  217. const pixel = (y - this.offsetY) * this.scale;
  218. if (pixel < 0) continue;
  219. const tickSize = y % (scaledGrid * 5) === 0 ? 10 : 5;
  220. ctx.beginPath();
  221. ctx.moveTo(20 - tickSize, pixel + 0.5);
  222. ctx.lineTo(20, pixel + 0.5);
  223. ctx.stroke();
  224. if (y % (scaledGrid * 5) === 0) {
  225. ctx.save();
  226. ctx.translate(8, pixel + 3);
  227. ctx.rotate(-Math.PI/2);
  228. ctx.fillText(Math.round(y / this.scale), 0, 0);
  229. ctx.restore();
  230. }
  231. }
  232. },
  233. drawGrid() {
  234. const canvas = this.$refs.gridCanvas;
  235. const ctx = canvas.getContext('2d');
  236. const width = canvas.width;
  237. const height = canvas.height;
  238. ctx.clearRect(0, 0, width, height);
  239. ctx.save();
  240. ctx.scale(this.scale, this.scale);
  241. ctx.translate(-this.offsetX, -this.offsetY);
  242. // 绘制网格
  243. ctx.strokeStyle = '#e0e0e0';
  244. ctx.lineWidth = 1;
  245. const scaledGrid = this.gridSize;
  246. const startX = Math.floor(this.offsetX / scaledGrid) * scaledGrid;
  247. const startY = Math.floor(this.offsetY / scaledGrid) * scaledGrid;
  248. ctx.beginPath();
  249. for (let x = startX; x < this.offsetX + width / this.scale; x += scaledGrid) {
  250. ctx.moveTo(x + 0.5, startY);
  251. ctx.lineTo(x + 0.5, this.offsetY + height / this.scale);
  252. }
  253. for (let y = startY; y < this.offsetY + height / this.scale; y += scaledGrid) {
  254. ctx.moveTo(startX, y + 0.5);
  255. ctx.lineTo(this.offsetX + width / this.scale, y + 0.5);
  256. }
  257. ctx.stroke();
  258. // 绘制坐标轴
  259. ctx.strokeStyle = '#3498db';
  260. ctx.lineWidth = 2;
  261. ctx.beginPath();
  262. ctx.moveTo(0, startY);
  263. ctx.lineTo(0, this.offsetY + height / this.scale);
  264. ctx.moveTo(startX, 0);
  265. ctx.lineTo(this.offsetX + width / this.scale, 0);
  266. ctx.stroke();
  267. // 绘制原点标记
  268. ctx.fillStyle = '#e74c3c';
  269. ctx.beginPath();
  270. ctx.arc(0, 0, 4, 0, Math.PI * 2);
  271. ctx.fill();
  272. ctx.restore();
  273. },
  274. handleWheel(e) {
  275. e.preventDefault();
  276. const delta = e.deltaY > 0 ? -0.1 : 0.1;
  277. const newScale = Math.max(this.minScale, Math.min(this.maxScale, this.scale + delta));
  278. if (newScale !== this.scale) {
  279. // 以左上角为缩放中心,不需要调整offset
  280. this.scale = newScale;
  281. this.drawRulersAndGrid();
  282. }
  283. },
  284. zoomIn() {
  285. this.scale = Math.min(this.maxScale, this.scale + 0.1);
  286. this.drawRulersAndGrid();
  287. },
  288. zoomOut() {
  289. this.scale = Math.max(this.minScale, this.scale - 0.1);
  290. this.drawRulersAndGrid();
  291. },
  292. resetZoom() {
  293. this.scale = 1;
  294. this.offsetX = 0;
  295. this.offsetY = 0;
  296. this.drawRulersAndGrid();
  297. },
  298. handleResize() {
  299. // 在实际应用中,这里可以调整容器大小
  300. this.drawRulersAndGrid();
  301. }
  302. },
  303. watch: {
  304. scale() {
  305. this.drawRulersAndGrid();
  306. }
  307. }
  308. });
  309. </script>
  310. </body>
  311. </html>