js.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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(
  12. e.clientX,
  13. e.clientY,
  14. +size.value,
  15. color.value,
  16. fill.checked
  17. );
  18. },
  19. },
  20. circle: {
  21. mousedown(e) {
  22. current = new Circle(e.clientX, e.clientY, 1, color.value, fill.checked);
  23. },
  24. mousemove(e) {
  25. if (!current) return;
  26. current.radius = current.distanceTo(e.clientX, e.clientY);
  27. Drawable.drawAll();
  28. },
  29. mouseup(e) {
  30. current = null;
  31. },
  32. },
  33. line: {
  34. mousedown(e) {
  35. current = new Line(e.clientX, e.clientY, 0, 0, color.value, +size.value);
  36. },
  37. mousemove(e) {
  38. if (!current) return;
  39. current.width = e.clientX - current.x;
  40. current.height = e.clientY - current.y;
  41. Drawable.drawAll();
  42. },
  43. mouseup(e) {
  44. current = null;
  45. },
  46. },
  47. rectangle: {
  48. mousedown(e) {
  49. current = new Rectangle(
  50. e.clientX,
  51. e.clientY,
  52. 0,
  53. 0,
  54. color.value,
  55. +size.value,
  56. fill.checked
  57. );
  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. ellipse: {
  70. mousedown(e) {
  71. current = new Ellipse(
  72. e.clientX,
  73. e.clientY,
  74. 0,
  75. 0,
  76. color.value,
  77. +size.value,
  78. fill.checked
  79. );
  80. },
  81. mousemove(e) {
  82. if (!current) return;
  83. current.width = e.clientX - current.x;
  84. current.height = e.clientY - current.y;
  85. Drawable.drawAll();
  86. },
  87. mouseup(e) {
  88. current = null;
  89. },
  90. },
  91. select: {
  92. click(e) {
  93. console.log(e);
  94. let found = Drawable.instances.filter(
  95. (c) => c.in && c.in(e.clientX, e.clientY)
  96. );
  97. if (found.length) {
  98. if (e.ctrlKey) {
  99. selection.push(found.pop());
  100. } else {
  101. selection = [found.pop()];
  102. }
  103. } else {
  104. if (!e.ctrlKey) selection = [];
  105. }
  106. Drawable.drawAll(selection);
  107. },
  108. },
  109. };
  110. function superHandler(evt) {
  111. let t = tools[tool.value];
  112. if (typeof t[evt.type] === "function") t[evt.type].call(this, evt);
  113. }
  114. canvas.onmousemove = superHandler;
  115. canvas.onmouseup = superHandler;
  116. canvas.onmousedown = superHandler;
  117. canvas.onclick = superHandler;
  118. ////
  119. function Drawable() {
  120. Drawable.addInstance(this);
  121. }
  122. const distance = (x1, y1, x2, y2) => ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5;
  123. Drawable.prototype.draw = function () {};
  124. Drawable.prototype.distanceTo = function (x, y) {
  125. if (typeof this.x !== "number" || typeof this.y !== "number") {
  126. return NaN;
  127. }
  128. return distance(this.x, this.y, x, y);
  129. };
  130. Drawable.instances = [];
  131. Drawable.addInstance = function (item) {
  132. Drawable.instances.push(item);
  133. };
  134. Drawable.drawAll = function (selection = []) {
  135. ctx.clearRect(0, 0, width, height);
  136. Drawable.forAll((item) => item.draw(selection.includes(item)));
  137. };
  138. Drawable.forAll = function (callback) {
  139. for (var i = 0; i < Drawable.instances.length; i++) {
  140. callback(Drawable.instances[i]);
  141. }
  142. };
  143. class Circle extends Drawable {
  144. constructor(x, y, radius, color, fill) {
  145. super();
  146. this.x = x;
  147. this.y = y;
  148. this.radius = radius;
  149. this.color = color;
  150. this.fill = fill;
  151. this.draw();
  152. }
  153. draw(selected) {
  154. ctx.beginPath();
  155. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  156. ctx.closePath();
  157. ctx.fillStyle = this.color;
  158. if (selected) {
  159. ctx.setLineDash([5, 15]);
  160. ctx.lineWidth = 2;
  161. ctx.stroke();
  162. } else ctx.setLineDash([]);
  163. if (this.fill) ctx.fill();
  164. else ctx.stroke();
  165. }
  166. in(x, y) {
  167. return this.distanceTo(x, y) < this.radius;
  168. }
  169. inBounds(x, y, w, h) {
  170. return this.x >= x && this.x <= x + w && this.y >= y && this.y <= y + h;
  171. }
  172. }
  173. class Line extends Drawable {
  174. constructor(x, y, width, height, color, lineWidth) {
  175. super();
  176. this.x = x;
  177. this.y = y;
  178. this.width = width;
  179. this.height = height;
  180. this.color = color;
  181. this.lineWidth = lineWidth;
  182. this.draw();
  183. }
  184. draw(selected) {
  185. ctx.beginPath();
  186. ctx.moveTo(this.x, this.y);
  187. ctx.lineTo(this.x + this.width, this.y + this.height);
  188. ctx.closePath();
  189. ctx.strokeStyle = this.color;
  190. ctx.lineWidth = this.lineWidth;
  191. ctx.stroke();
  192. if (selected) {
  193. ctx.setLineDash([5, 15]);
  194. ctx.lineWidth = 4;
  195. ctx.stroke();
  196. } else ctx.setLineDash([]);
  197. if (this.fill) ctx.fill();
  198. else {
  199. ctx.strokeStyle = this.color;
  200. ctx.stroke();
  201. }
  202. }
  203. in(x, y) {
  204. return (
  205. x > this.x &&
  206. x < this.x + this.width &&
  207. y > this.y &&
  208. y < this.y + this.height
  209. );
  210. }
  211. }
  212. class Rectangle extends Drawable {
  213. constructor(x, y, width, height, color, lineWidth, fill) {
  214. super();
  215. this.x = x;
  216. this.y = y;
  217. this.width = width;
  218. this.height = height;
  219. this.color = color;
  220. this.lineWidth = lineWidth;
  221. this.fill = fill;
  222. this.draw();
  223. }
  224. draw(selected) {
  225. if (!selected) {
  226. ctx.setLineDash([]);
  227. if (this.fill) {
  228. ctx.fillStyle = this.color;
  229. ctx.fillRect(this.x, this.y, this.width, this.height);
  230. } else {
  231. ctx.beginPath();
  232. ctx.lineWidth = this.lineWidth;
  233. ctx.strokeStyle = this.color;
  234. ctx.rect(this.x, this.y, this.width, this.height);
  235. ctx.stroke();
  236. }
  237. } else {
  238. ctx.setLineDash([5, 15]);
  239. ctx.beginPath();
  240. ctx.lineWidth = this.lineWidth;
  241. ctx.strokeStyle = this.color;
  242. ctx.rect(this.x, this.y, this.width, this.height);
  243. ctx.stroke();
  244. }
  245. }
  246. in(x, y) {
  247. return (
  248. x > this.x &&
  249. x < this.x + this.width &&
  250. y > this.y &&
  251. y < this.y + this.height
  252. );
  253. }
  254. }
  255. class Ellipse extends Drawable {
  256. constructor(x, y, width, height, color, lineWidth, fill) {
  257. super();
  258. this.x = x;
  259. this.y = y;
  260. this.width = width;
  261. this.height = height;
  262. this.color = color;
  263. this.lineWidth = lineWidth;
  264. this.fill = fill;
  265. this.draw();
  266. }
  267. draw(selected) {
  268. const width2 = this.width / 2;
  269. const height2 = this.height / 2;
  270. ctx.beginPath();
  271. ctx.ellipse(
  272. this.x + width2,
  273. this.y + height2,
  274. Math.abs(width2),
  275. Math.abs(height2),
  276. 0,
  277. 0,
  278. 2 * Math.PI
  279. );
  280. ctx.closePath();
  281. ctx.fillStyle = this.color;
  282. if (selected) {
  283. ctx.setLineDash([5, 15]);
  284. ctx.lineWidth = 4;
  285. ctx.stroke();
  286. } else ctx.setLineDash([]);
  287. if (this.fill) ctx.fill();
  288. else {
  289. ctx.strokeStyle = this.color;
  290. ctx.stroke();
  291. }
  292. }
  293. in(x, y) {
  294. return (
  295. x > this.x &&
  296. x < this.x + this.width &&
  297. y > this.y &&
  298. y < this.y + this.height
  299. );
  300. }
  301. }
  302. color.onchange = () => {
  303. selection.forEach((c) => (c.color = color.value));
  304. Drawable.drawAll(selection);
  305. };
  306. document.getElementById("delete").onclick = () => {
  307. Drawable.instances = Drawable.instances.filter(
  308. (item) => !selection.includes(item)
  309. );
  310. selection = [];
  311. Drawable.drawAll();
  312. };
  313. function jsonPost(url, data) {
  314. return new Promise((resolve, reject) => {
  315. var x = new XMLHttpRequest();
  316. x.onerror = () => reject(new Error("jsonPost failed"));
  317. x.open("POST", url, true);
  318. x.send(JSON.stringify(data));
  319. x.onreadystatechange = () => {
  320. if (x.readyState == XMLHttpRequest.DONE && x.status == 200) {
  321. resolve(JSON.parse(x.responseText));
  322. } else if (x.status != 200) {
  323. reject(new Error("status is not 200"));
  324. }
  325. };
  326. });
  327. }
  328. sendInChat.onclick = () => {
  329. jsonPost("http://students.a-level.com.ua:10012", {
  330. func: "addMessage",
  331. nick: nick.value,
  332. message: `<img src = ${canvas.toDataURL()} >`,
  333. }).then(() => {
  334. console.log(imgSrcUrl5);
  335. });
  336. };