main.js 8.2 KB

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