hw.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. const canvas = document.getElementById('canvas');
  2. const ctx = canvas.getContext('2d');
  3. const width = canvas.width;
  4. const height = canvas.height;
  5. let current;
  6. let selection = [];
  7. const tools = {
  8. graffity: {
  9. mousemove(e) {
  10. e.buttons & 1 &&
  11. new Circle(e.clientX, e.clientY, +size.value, color.value);
  12. },
  13. },
  14. circle: {
  15. mousedown(e) {
  16. current = new Circle(e.clientX, e.clientY, 1, color.value);
  17. },
  18. mousemove(e) {
  19. if (!current) return;
  20. current.radius = current.distanceTo(e.clientX, e.clientY);
  21. Drawable.drawAll();
  22. },
  23. mouseup(e) {
  24. current = null;
  25. },
  26. },
  27. line: {
  28. mousedown(e) {
  29. current = new Line(e.clientX, e.clientY, 0, 0, color.value, +size.value);
  30. },
  31. mousemove(e) {
  32. if (!current) return;
  33. current.width = e.clientX - current.x;
  34. current.height = e.clientY - current.y;
  35. Drawable.drawAll();
  36. },
  37. mouseup(e) {
  38. current = null;
  39. },
  40. },
  41. select: {
  42. click(e) {
  43. let found = Drawable.instances.filter(
  44. (c) => c.in && c.in(e.clientX, e.clientY)
  45. );
  46. if (found.length) {
  47. if (e.altKey) {
  48. selection.push(found.pop());
  49. } else {
  50. selection = [found.pop()];
  51. }
  52. } else {
  53. if (!e.altKey) selection = [];
  54. }
  55. Drawable.drawAll(selection);
  56. },
  57. mousedown(e) {},
  58. mousemove(e) {},
  59. mouseup(e) {},
  60. },
  61. rectangle: {
  62. mousedown(e) {
  63. current = new Rectangle(e.clientX, e.clientY, 0, 0, color.value);
  64. },
  65. mousemove(e) {
  66. if (!current) return;
  67. current.width = e.clientX - current.x;
  68. current.height = e.clientY - current.y;
  69. Drawable.drawAll();
  70. },
  71. mouseup(e) {
  72. current = null;
  73. },
  74. },
  75. ellipse: {
  76. mousedown(e) {
  77. current = new Ellipse(e.clientX, e.clientY, 1, 1, color.value);
  78. },
  79. mousemove(e) {
  80. if (!current) return;
  81. const width = e.clientX - current.x;
  82. const height = e.clientY - current.y;
  83. current.width = width ? width : width * -1;
  84. current.height = height ? height : height * -1;
  85. Drawable.drawAll();
  86. },
  87. mouseup(e) {
  88. current = null;
  89. },
  90. },
  91. };
  92. function superHandler(evt) {
  93. const t = tools[tool.value];
  94. if (typeof t[evt.type] === 'function') t[evt.type].call(this, evt);
  95. }
  96. canvas.onmousemove = superHandler;
  97. canvas.onmouseup = superHandler;
  98. canvas.onmousedown = superHandler;
  99. canvas.onclick = superHandler;
  100. function Drawable() {
  101. Drawable.addInstance(this);
  102. }
  103. const distance = (x1, y1, x2, y2) => ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5;
  104. Drawable.prototype.draw = function () {};
  105. Drawable.prototype.distanceTo = function (x, y) {
  106. if (typeof this.x !== 'number' || typeof this.y !== 'number') {
  107. return NaN;
  108. }
  109. return distance(this.x, this.y, x, y);
  110. };
  111. Drawable.instances = [];
  112. Drawable.addInstance = function (item) {
  113. Drawable.instances.push(item);
  114. };
  115. Drawable.drawAll = function (selection = []) {
  116. ctx.clearRect(0, 0, width, height);
  117. Drawable.forAll((item) => item.draw());
  118. selection.forEach((item) => item.draw(true));
  119. };
  120. Drawable.forAll = function (callback) {
  121. for (var i = 0; i < Drawable.instances.length; i++) {
  122. callback(Drawable.instances[i]);
  123. }
  124. };
  125. class Circle extends Drawable {
  126. constructor(x, y, radius, color) {
  127. super();
  128. this.x = x;
  129. this.y = y;
  130. this.radius = radius;
  131. this.color = color;
  132. this.draw();
  133. }
  134. draw(selected) {
  135. ctx.beginPath();
  136. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  137. ctx.closePath();
  138. ctx.fillStyle = this.color;
  139. if (selected) {
  140. ctx.lineWidth = 2;
  141. ctx.stroke();
  142. }
  143. ctx.fill();
  144. }
  145. in(x, y) {
  146. return this.distanceTo(x, y) < this.radius;
  147. }
  148. }
  149. class Line extends Drawable {
  150. constructor(x, y, width, height, color, lineWidth) {
  151. super();
  152. this.x = x;
  153. this.y = y;
  154. this.width = width;
  155. this.height = height;
  156. this.color = color;
  157. this.lineWidth = lineWidth;
  158. this.draw();
  159. }
  160. draw(selected) {
  161. ctx.beginPath();
  162. ctx.moveTo(this.x, this.y);
  163. ctx.lineTo(this.x + this.width, this.y + this.height);
  164. ctx.closePath();
  165. ctx.strokeStyle = this.color;
  166. ctx.lineWidth = this.lineWidth;
  167. if (selected) {
  168. ctx.strokeStyle = '#1f600e';
  169. ctx.stroke();
  170. }
  171. ctx.stroke();
  172. }
  173. in(x, y) {
  174. let rotateLine = Math.atan2(this.height, this.width);
  175. let rotCursor = Math.atan2(y - this.y, x - this.x);
  176. let distanceToPoint = distance(x, y, this.x, this.y);
  177. let angleLine = rotCursor - rotateLine;
  178. let rotateX = Math.cos(angleLine) * distanceToPoint;
  179. let rotateY = Math.sin(angleLine) * distanceToPoint;
  180. return (
  181. rotateX >= 0 &&
  182. rotateX <= this.length &&
  183. rotateY <= this.lineWidth / 2 &&
  184. rotateY >= -this.lineWidth / 2
  185. );
  186. }
  187. }
  188. class Rectangle extends Drawable {
  189. constructor(x, y, width, height, color) {
  190. super();
  191. this.x = x;
  192. this.y = y;
  193. this.width = width;
  194. this.height = height;
  195. this.color = color;
  196. this.draw();
  197. }
  198. draw(selected) {
  199. ctx.beginPath();
  200. ctx.moveTo(this.x, this.y);
  201. ctx.fillRect(this.x, this.y, this.width, this.height);
  202. if (selected) {
  203. ctx.rect(this.x, this.y, this.width, this.height);
  204. ctx.strokeStyle = '#1f600e';
  205. ctx.stroke();
  206. }
  207. ctx.fillStyle = this.color;
  208. ctx.stroke();
  209. }
  210. in(x, y) {
  211. return (
  212. x >= this.x &&
  213. x <= this.x + this.width &&
  214. y >= this.y &&
  215. y <= this.y + this.height
  216. );
  217. }
  218. }
  219. class Ellipse extends Drawable {
  220. constructor(x, y, width, height, color) {
  221. super();
  222. this.x = x ? x : x * -1;
  223. this.y = y ? y : y * -1;
  224. this.width = width;
  225. this.height = height;
  226. this.color = color;
  227. this.draw();
  228. }
  229. draw() {
  230. this.width = this.width > 0 ? this.width : this.width * -1;
  231. this.height = this.height > 0 ? this.height : this.height * -1;
  232. ctx.beginPath();
  233. ctx.moveTo(this.x, this.y);
  234. ctx.ellipse(
  235. this.x,
  236. this.y,
  237. this.height,
  238. this.width,
  239. Math.PI / 2,
  240. 0,
  241. 2 * Math.PI
  242. );
  243. ctx.closePath();
  244. ctx.fillStyle = this.color;
  245. ctx.fill();
  246. }
  247. in(x, y) {
  248. return this.distanceTo(x, y) < this.radius;
  249. }
  250. }
  251. remove.onclick = () => {
  252. Drawable.instances = [];
  253. Drawable.drawAll();
  254. };
  255. undo.onclick = function () {
  256. Drawable.instances.pop();
  257. Drawable.drawAll();
  258. };
  259. color.onchange = () => {
  260. selection.forEach((c) => (c.color = color.value));
  261. Drawable.drawAll(selection);
  262. };