hw.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. let 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. inBounds(x, y, w, h) {
  149. return this.x >= x && this.x <= x + w && this.y >= y && this.y <= y + h;
  150. }
  151. }
  152. class Line extends Drawable {
  153. constructor(x, y, width, height, color, lineWidth) {
  154. super();
  155. this.x = x;
  156. this.y = y;
  157. this.width = width;
  158. this.height = height;
  159. this.color = color;
  160. this.lineWidth = lineWidth;
  161. this.draw();
  162. }
  163. draw(selected) {
  164. ctx.beginPath();
  165. ctx.moveTo(this.x, this.y);
  166. ctx.lineTo(this.x + this.width, this.y + this.height);
  167. ctx.closePath();
  168. ctx.strokeStyle = this.color;
  169. ctx.lineWidth = this.lineWidth;
  170. if (selected) {
  171. ctx.strokeStyle = '#1f600e';
  172. ctx.stroke();
  173. }
  174. ctx.stroke();
  175. }
  176. in(x, y) {
  177. let rotateLine = Math.atan2(this.height, this.width);
  178. let rotCursor = Math.atan2(y - this.y, x - this.x);
  179. let distanceToPoint = distance(x, y, this.x, this.y);
  180. let angleLine = rotCursor - rotateLine;
  181. let rotateX = Math.cos(angleLine) * distanceToPoint;
  182. let rotateY = Math.sin(angleLine) * distanceToPoint;
  183. return (
  184. rotateX >= 0 &&
  185. rotateX <= this.length &&
  186. rotateY <= this.lineWidth / 2 &&
  187. rotateY >= -this.lineWidth / 2
  188. );
  189. }
  190. get length() {
  191. return this.distanceTo(this.x + this.width, this.y + this.height);
  192. }
  193. }
  194. class Rectangle extends Drawable {
  195. constructor(x, y, width, height, color) {
  196. super();
  197. this.x = x;
  198. this.y = y;
  199. this.width = width;
  200. this.height = height;
  201. this.color = color;
  202. this.draw();
  203. }
  204. draw(selected) {
  205. ctx.beginPath();
  206. ctx.moveTo(this.x, this.y);
  207. ctx.fillRect(this.x, this.y, this.width, this.height);
  208. if (selected) {
  209. ctx.rect(this.x, this.y, this.width, this.height);
  210. ctx.strokeStyle = '#1f600e';
  211. ctx.stroke();
  212. }
  213. ctx.fillStyle = this.color;
  214. ctx.stroke();
  215. }
  216. in(x, y) {
  217. return (
  218. x >= this.x &&
  219. x <= this.x + this.width &&
  220. y >= this.y &&
  221. y <= this.y + this.height
  222. );
  223. }
  224. }
  225. class Ellipse extends Drawable {
  226. constructor(x, y, width, height, color) {
  227. super();
  228. this.x = x ? x : x * -1;
  229. this.y = y ? y : y * -1;
  230. this.width = width;
  231. this.height = height;
  232. this.color = color;
  233. this.draw();
  234. }
  235. draw() {
  236. this.width = this.width > 0 ? this.width : this.width * -1;
  237. this.height = this.height > 0 ? this.height : this.height * -1;
  238. ctx.beginPath();
  239. ctx.moveTo(this.x, this.y);
  240. ctx.ellipse(
  241. this.x,
  242. this.y,
  243. this.height,
  244. this.width,
  245. Math.PI / 2,
  246. 0,
  247. 2 * Math.PI
  248. );
  249. ctx.closePath();
  250. ctx.fillStyle = this.color;
  251. ctx.fill();
  252. }
  253. in(x, y) {
  254. return this.distanceTo(x, y) < this.radius;
  255. }
  256. inBounds(x, y, w, h) {
  257. return this.x >= x && this.x <= x + w && this.y >= y && this.y <= y + h;
  258. }
  259. }
  260. document.getElementById('delete').onclick = () => {
  261. Drawable.instances = [];
  262. Drawable.drawAll();
  263. };
  264. document.getElementById('undo').onclick = function () {
  265. Drawable.instances.pop();
  266. Drawable.drawAll();
  267. };
  268. color.onchange = () => {
  269. selection.forEach((c) => (c.color = color.value));
  270. Drawable.drawAll(selection);
  271. };