index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. rectangle: {
  42. mousedown(e) {
  43. current = new Rectangle(e.clientX, e.clientY, 0, 0, color.value);
  44. },
  45. mousemove(e) {
  46. if (!current) return;
  47. current.width = e.clientX - current.x;
  48. current.height = e.clientY - current.y;
  49. Drawable.drawAll();
  50. },
  51. mouseup(e) {
  52. current = null;
  53. },
  54. },
  55. ellipse: {
  56. mousedown(e) {
  57. current = new Ellipse(e.clientX, e.clientY, 0, 0, color.value);
  58. },
  59. mousemove(e) {
  60. if (!current) return;
  61. current.width = e.clientX - current.x;
  62. current.height = e.clientY - current.y;
  63. Drawable.drawAll();
  64. },
  65. mouseup(e) {
  66. current = null;
  67. },
  68. },
  69. select: {
  70. click(e) {
  71. console.log(e);
  72. let found = Drawable.instances.filter(
  73. (c) => c.in && c.in(e.clientX, e.clientY)
  74. );
  75. if (found.length) {
  76. if (e.ctrlKey) {
  77. selection.push(found.pop());
  78. } else {
  79. selection = [found.pop()];
  80. }
  81. } else {
  82. if (!e.ctrlKey) selection = [];
  83. }
  84. Drawable.drawAll(selection);
  85. },
  86. mousedown(e) {
  87. //
  88. },
  89. mousemove(e) {},
  90. mouseup(e) {
  91. //x,y, w, h прямоугольника
  92. //selection - только те элеменеты Drawable.instances которые в границах прямоугольника.
  93. },
  94. },
  95. };
  96. function superHandler(evt) {
  97. let t = tools[tool.value];
  98. if (typeof t[evt.type] === "function") t[evt.type].call(this, evt);
  99. }
  100. canvas.onmousemove = superHandler;
  101. canvas.onmouseup = superHandler;
  102. canvas.onmousedown = superHandler;
  103. canvas.onclick = superHandler;
  104. function Drawable() {
  105. Drawable.addInstance(this);
  106. }
  107. const distance = (x1, y1, x2, y2) => ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5;
  108. Drawable.prototype.draw = function () {};
  109. Drawable.prototype.distanceTo = function (x, y) {
  110. if (typeof this.x !== "number" || typeof this.y !== "number") {
  111. return NaN;
  112. }
  113. return distance(this.x, this.y, x, y);
  114. };
  115. Drawable.instances = [];
  116. Drawable.addInstance = function (item) {
  117. Drawable.instances.push(item);
  118. };
  119. Drawable.drawAll = function (selection = []) {
  120. ctx.clearRect(0, 0, width, height);
  121. Drawable.forAll((item) => item.draw());
  122. selection.forEach((item) => item.draw(true));
  123. };
  124. Drawable.forAll = function (callback) {
  125. for (var i = 0; i < Drawable.instances.length; i++) {
  126. callback(Drawable.instances[i]);
  127. }
  128. };
  129. class Circle extends Drawable {
  130. constructor(x, y, radius, color) {
  131. super();
  132. this.x = x;
  133. this.y = y;
  134. this.radius = radius;
  135. this.color = color;
  136. this.draw();
  137. }
  138. draw(selected) {
  139. ctx.beginPath();
  140. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  141. ctx.closePath();
  142. ctx.fillStyle = this.color;
  143. if (selected) {
  144. ctx.lineWidth = 2;
  145. ctx.strokeStyle = "red";
  146. ctx.stroke();
  147. }
  148. ctx.fill();
  149. }
  150. in(x, y) {
  151. return this.distanceTo(x, y) < this.radius;
  152. }
  153. inBounds(x, y, w, h) {
  154. return this.x >= x && this.x <= x + w && this.y >= y && this.y <= y + h;
  155. }
  156. }
  157. class Line extends Drawable {
  158. constructor(x, y, width, height, color, lineWidth) {
  159. super();
  160. this.x = x;
  161. this.y = y;
  162. this.width = width;
  163. this.height = height;
  164. this.color = color;
  165. this.lineWidth = lineWidth;
  166. this.draw();
  167. }
  168. draw(selected) {
  169. ctx.beginPath();
  170. ctx.moveTo(this.x, this.y);
  171. ctx.lineTo(this.x + this.width, this.y + this.height);
  172. ctx.closePath();
  173. ctx.strokeStyle = this.color;
  174. ctx.lineWidth = this.lineWidth;
  175. if (selected) {
  176. }
  177. ctx.stroke();
  178. }
  179. in(x, y) {
  180. // console.log("this.x " + this.x, "this.y " + this.y);
  181. // console.log("x " + x, "y " + y);
  182. // new Line(this.x, this.y, this.width, 0, "red");
  183. }
  184. }
  185. class Rectangle extends Drawable {
  186. constructor(x, y, width, height, color) {
  187. super();
  188. this.x = x;
  189. this.y = y;
  190. this.width = width;
  191. this.height = height;
  192. this.color = color;
  193. this.draw();
  194. }
  195. draw(selected) {
  196. ctx.fillStyle = this.color;
  197. ctx.fillRect(this.x, this.y, this.width, this.height);
  198. if (selected) {
  199. ctx.beginPath();
  200. ctx.strokeStyle = "green";
  201. ctx.rect(this.x, this.y, this.width, this.height);
  202. ctx.stroke();
  203. ctx.closePath();
  204. }
  205. }
  206. in(x, y) {
  207. return (
  208. this.x <= x &&
  209. this.x + this.width >= x &&
  210. this.y <= y &&
  211. this.y + this.height >= y
  212. );
  213. }
  214. }
  215. class Ellipse extends Drawable {
  216. constructor(x, y, width, height, color) {
  217. super();
  218. this.x = x;
  219. this.y = y;
  220. this.width = width;
  221. this.height = height;
  222. this.color = color;
  223. this.draw();
  224. }
  225. #width = 0;
  226. #height = 0;
  227. set height(newHeight) {
  228. if (newHeight < 0) {
  229. this.#height = -newHeight;
  230. this.y += newHeight;
  231. } else {
  232. this.#height = newHeight;
  233. }
  234. }
  235. set width(newWidth) {
  236. if (newWidth < 0) {
  237. this.#width = -newWidth;
  238. this.x += newWidth;
  239. } else {
  240. this.#width = newWidth;
  241. }
  242. }
  243. get width() {
  244. return this.#width;
  245. }
  246. get height() {
  247. return this.#height;
  248. }
  249. get rx() {
  250. return this.#width / 2;
  251. }
  252. get ry() {
  253. return this.#height / 2;
  254. }
  255. draw(selected) {
  256. ctx.beginPath();
  257. ctx.moveTo(this.x, this.y);
  258. ctx.fillStyle = this.color;
  259. ctx.ellipse(
  260. this.x + this.width / 2,
  261. this.y + this.height / 2,
  262. this.width / 2,
  263. this.height / 2,
  264. 0,
  265. 0,
  266. 2 * Math.PI
  267. );
  268. ctx.fill();
  269. ctx.closePath();
  270. if (selected) {
  271. ctx.beginPath();
  272. ctx.strokeStyle = "green";
  273. ctx.ellipse(
  274. this.x + this.width / 2,
  275. this.y + this.height / 2,
  276. this.width / 2,
  277. this.height / 2,
  278. 0,
  279. 0,
  280. 2 * Math.PI
  281. );
  282. ctx.stroke();
  283. ctx.closePath();
  284. }
  285. }
  286. in(x, y) {
  287. let cx = (this.x * 2 + this.width) / 2;
  288. let cy = (this.y * 2 + this.height) / 2;
  289. return (
  290. (x - cx) ** 2 / (this.width / 2) ** 2 +
  291. (y - cy) ** 2 / (this.height / 2) ** 2 <=
  292. 1
  293. );
  294. }
  295. }
  296. color.onchange = () => {
  297. selection.forEach((c) => (c.color = color.value));
  298. Drawable.drawAll(selection);
  299. };
  300. document.getElementById("delete").onclick = () => {
  301. Drawable.instances = Drawable.instances.filter(
  302. (item) => !selection.includes(item)
  303. );
  304. selection = [];
  305. Drawable.drawAll();
  306. };
  307. // new Rectangle(10, 10, 100, 100, "black");
  308. //new Line(0,0,100,100, "red")
  309. ////new Circle(30,30,10, "red")
  310. ////canvas.onmousemove = function(e){
  311. ////}
  312. //undo.onclick = function(){
  313. //Drawable.instances.pop()
  314. ////Drawable.instances = []
  315. //Drawable.drawAll()
  316. //}