index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. let angleCrank = Math.atan2(this.height, this.width);
  181. let angleMouse = Math.atan2(y - this.y, x - this.x);
  182. let lengthLine = this.distanceTo(this.x + this.width, this.y + this.height);
  183. let horizontal = angleMouse - angleCrank;
  184. let newX = Math.cos(horizontal) * distance(x, y, this.x, this.y);
  185. let newY = Math.sin(horizontal) * distance(x, y, this.x, this.y);
  186. if (
  187. newX > 0 &&
  188. newX < lengthLine &&
  189. newY > this.lineWidth / -2 &&
  190. newY < this.lineWidth / 2
  191. ) {
  192. console.log("selected");
  193. }
  194. }
  195. }
  196. class Rectangle extends Drawable {
  197. constructor(x, y, width, height, color) {
  198. super();
  199. this.x = x;
  200. this.y = y;
  201. this.width = width;
  202. this.height = height;
  203. this.color = color;
  204. this.draw();
  205. }
  206. draw(selected) {
  207. ctx.fillStyle = this.color;
  208. ctx.fillRect(this.x, this.y, this.width, this.height);
  209. if (selected) {
  210. ctx.beginPath();
  211. ctx.strokeStyle = "green";
  212. ctx.rect(this.x, this.y, this.width, this.height);
  213. ctx.stroke();
  214. ctx.closePath();
  215. }
  216. }
  217. in(x, y) {
  218. return (
  219. this.x <= x &&
  220. this.x + this.width >= x &&
  221. this.y <= y &&
  222. this.y + this.height >= y
  223. );
  224. }
  225. }
  226. class Ellipse extends Drawable {
  227. constructor(x, y, width, height, color) {
  228. super();
  229. this.x = x;
  230. this.y = y;
  231. this.width = width;
  232. this.height = height;
  233. this.color = color;
  234. this.draw();
  235. }
  236. #width = 0;
  237. #height = 0;
  238. set height(newHeight) {
  239. if (newHeight < 0) {
  240. this.#height = -newHeight;
  241. this.y += newHeight;
  242. } else {
  243. this.#height = newHeight;
  244. }
  245. }
  246. set width(newWidth) {
  247. if (newWidth < 0) {
  248. this.#width = -newWidth;
  249. this.x += newWidth;
  250. } else {
  251. this.#width = newWidth;
  252. }
  253. }
  254. get width() {
  255. return this.#width;
  256. }
  257. get height() {
  258. return this.#height;
  259. }
  260. get rx() {
  261. return this.#width / 2;
  262. }
  263. get ry() {
  264. return this.#height / 2;
  265. }
  266. draw(selected) {
  267. ctx.beginPath();
  268. ctx.moveTo(this.x, this.y);
  269. ctx.fillStyle = this.color;
  270. ctx.ellipse(
  271. this.x + this.width / 2,
  272. this.y + this.height / 2,
  273. this.width / 2,
  274. this.height / 2,
  275. 0,
  276. 0,
  277. 2 * Math.PI
  278. );
  279. ctx.fill();
  280. ctx.closePath();
  281. if (selected) {
  282. ctx.beginPath();
  283. ctx.strokeStyle = "green";
  284. ctx.ellipse(
  285. this.x + this.width / 2,
  286. this.y + this.height / 2,
  287. this.width / 2,
  288. this.height / 2,
  289. 0,
  290. 0,
  291. 2 * Math.PI
  292. );
  293. ctx.stroke();
  294. ctx.closePath();
  295. }
  296. }
  297. in(x, y) {
  298. let cx = (this.x * 2 + this.width) / 2;
  299. let cy = (this.y * 2 + this.height) / 2;
  300. return (
  301. (x - cx) ** 2 / (this.width / 2) ** 2 +
  302. (y - cy) ** 2 / (this.height / 2) ** 2 <=
  303. 1
  304. );
  305. }
  306. }
  307. color.onchange = () => {
  308. selection.forEach((c) => (c.color = color.value));
  309. Drawable.drawAll(selection);
  310. };
  311. document.getElementById("delete").onclick = () => {
  312. Drawable.instances = Drawable.instances.filter(
  313. (item) => !selection.includes(item)
  314. );
  315. selection = [];
  316. Drawable.drawAll();
  317. };
  318. // new Rectangle(10, 10, 100, 100, "black");
  319. //new Line(0,0,100,100, "red")
  320. ////new Circle(30,30,10, "red")
  321. ////canvas.onmousemove = function(e){
  322. ////}
  323. //undo.onclick = function(){
  324. //Drawable.instances.pop()
  325. ////Drawable.instances = []
  326. //Drawable.drawAll()
  327. //}