index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 & 0b001) && 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: { //ctrl-c ctrl-v в rectangle. Так же добавьте его в <select id='tool'>
  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: { //ctrl-c ctrl-v в rectangle. Так же добавьте его в <select id='tool'>
  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: { //ctrl-c ctrl-v в rectangle. Так же добавьте его в <select id='tool'>
  55. mousedown(e){
  56. current = new Ellipse(e.layerX, e.layerY, 0, 0, color.value, +size.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. let found = Drawable.instances.filter(c => c.in && c.in(e.layerX, e.layerY))
  72. if (found.length){
  73. if (e.ctrlKey){
  74. selection.push(found.pop())
  75. }
  76. else {
  77. selection = [found.pop()]
  78. }
  79. }
  80. else {
  81. if (!e.ctrlKey) selection = []
  82. }
  83. console.log('selection', selection)
  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. //__proto__: Drawable.prototype
  135. constructor(x,y,radius, color){
  136. super()
  137. this.x = x;
  138. this.y = y;
  139. this.radius = radius;
  140. this.color = color;
  141. this.draw();
  142. }
  143. draw(selected){
  144. ctx.beginPath();
  145. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  146. ctx.closePath();
  147. ctx.fillStyle = this.color;
  148. ctx.fill();
  149. if (selected){
  150. ctx.lineWidth = 2
  151. ctx.stroke();
  152. }
  153. }
  154. in(x,y){
  155. return this.distanceTo(x,y) < this.radius
  156. }
  157. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  158. return this.x >= x && this.x <= x + w &&
  159. this.y >= y && this.y <= y + h
  160. }
  161. }
  162. class Line extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  163. constructor(x,y, width, height, color, lineWidth){
  164. super()
  165. this.x = x;
  166. this.y = y;
  167. this.width = width;
  168. this.height = height;
  169. this.color = color;
  170. this.lineWidth = lineWidth;
  171. this.draw();
  172. }
  173. draw(selected){
  174. ctx.beginPath();
  175. ctx.moveTo(this.x, this.y);
  176. ctx.lineTo(this.x + this.width, this.y + this.height);
  177. ctx.closePath();
  178. ctx.strokeStyle = this.color;
  179. ctx.lineWidth = this.lineWidth
  180. ctx.stroke();
  181. }
  182. in(x,y){
  183. //Гарри Поттер и орден Арктангенса
  184. }
  185. }
  186. class Rectangle extends Drawable {
  187. constructor(x,y, width, height, color){
  188. super()
  189. this.x = x;
  190. this.y = y;
  191. this.width = width;
  192. this.height = height;
  193. this.color = color;
  194. this.draw();
  195. }
  196. draw(selected){
  197. ctx.beginPath();
  198. ctx.fillRect(this.x, this.y, this.width, this.height);
  199. ctx.closePath();
  200. ctx.style = this.color;
  201. if (selected){
  202. ctx.lineWidth = 2
  203. ctx.stroke();
  204. }
  205. ctx.fill()
  206. }
  207. in(x,y){
  208. return x > this.x && x < this.x + this.width &&
  209. y > this.y && y < this.y+this.height
  210. }
  211. }
  212. //Ellipse по аналогии
  213. //
  214. class Ellipse extends Drawable {
  215. constructor(x,y, width, height, color){
  216. super()
  217. this.x = x;
  218. this.y = y;
  219. this.width = width;
  220. this.height = height;
  221. this.color = color;
  222. this.draw();
  223. }
  224. draw(selected){
  225. ctx.beginPath()
  226. ctx.ellipse(this.x,this.y, Math.abs(this.width), Math.abs(this.height), 0, 0, 2 * Math.PI)
  227. ctx.closePath();
  228. ctx.fillStyle = this.color;
  229. if (selected){
  230. ctx.lineWidth = 2
  231. ctx.stroke();
  232. }
  233. ctx.fill();
  234. }
  235. in(x,y){
  236. return (Math.pow(x - this.x, 2) / Math.pow(this.width, 2) +
  237. Math.pow(y - this.y, 2) / Math.pow(this.height, 2)) <= 1;
  238. }
  239. }
  240. color.onchange = () => {
  241. selection.forEach(c => c.color = color.value)
  242. Drawable.drawAll(selection)
  243. }
  244. document.getElementById('delete').onclick = () =>{
  245. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  246. selection = []
  247. Drawable.drawAll()
  248. }
  249. //new Line(0,0,100,100, "red")
  250. ////new Circle(30,30,10, "red")
  251. ////canvas.onmousemove = function(e){
  252. ////}
  253. //undo.onclick = function(){
  254. //Drawable.instances.pop()
  255. ////Drawable.instances = []
  256. //Drawable.drawAll()
  257. //}