index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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)
  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. if (selected){
  149. ctx.lineWidth = 2
  150. ctx.stroke();
  151. }
  152. ctx.fill();
  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. }
  183. class Rectangle extends Drawable { //смочь скопировать в Rectangle и поменять отрисовку
  184. constructor(x,y, width, height, color){
  185. super()
  186. this.x = x;
  187. this.y = y;
  188. this.width = width;
  189. this.height = height;
  190. this.color = color;
  191. this.draw();
  192. }
  193. draw(selected){
  194. ctx.beginPath();
  195. ctx.rect(this.x,this.y,this.width,this.height);
  196. ctx.closePath();
  197. ctx.fillStyle = this.color;
  198. if (selected){
  199. ctx.lineWidth = 2
  200. ctx.stroke();
  201. }
  202. ctx.fill();
  203. }
  204. in(x,y){
  205. return x > this.x && x < this.x+this.width &&
  206. y > this.y && y < this.y+this.height;//условие попадания x,y в прямоугольник
  207. }
  208. }
  209. //Ellipse по аналогии
  210. //
  211. class Ellipse extends Drawable { //смочь скопировать в Ellipse и поменять отрисовку
  212. constructor(x,y, width, height, color){
  213. super()
  214. this.x = x;
  215. this.y = y;
  216. this.width = width;
  217. this.height = height;
  218. this.color = color;
  219. this.draw();
  220. }
  221. draw(selected){
  222. ctx.beginPath()
  223. ctx.ellipse(this.x,this.y, Math.abs(this.width), Math.abs(this.height), 0, 0, 2 * Math.PI)
  224. ctx.closePath();
  225. ctx.fillStyle = this.color;
  226. if (selected){
  227. ctx.lineWidth = 2
  228. ctx.stroke();
  229. }
  230. ctx.fill();
  231. }
  232. in(x,y){
  233. return (Math.pow(x-this.x,2)/Math.pow(this.width,2)+
  234. Math.pow(y-this.y,2)/Math.pow(this.height,2))<=1; //условие попадания x,y в ellipse
  235. }
  236. }
  237. color.onchange = () => {
  238. selection.forEach(c => c.color = color.value)
  239. Drawable.drawAll(selection)
  240. }
  241. document.getElementById('delete').onclick = () =>{
  242. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  243. selection = []
  244. Drawable.drawAll()
  245. }
  246. //new Line(0,0,100,100, "red")
  247. ////new Circle(30,30,10, "red")
  248. ////canvas.onmousemove = function(e){
  249. ////}
  250. //undo.onclick = function(){
  251. //Drawable.instances.pop()
  252. ////Drawable.instances = []
  253. //Drawable.drawAll()
  254. //}