index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. ellipse: {
  27. mousedown(e){
  28. current = new Ellipse(e.layerX, e.layerY, 1, 1, color.value)
  29. },
  30. mousemove(e){
  31. if (!current) return;
  32. current.radiusX = e.layerX - current.x;
  33. current.radiusY = e.layerY - current.y;
  34. Drawable.drawAll()
  35. },
  36. mouseup(e){
  37. console.log(e)
  38. if(current.radiusX < 0 || current.radiusY < 0) {
  39. console.log('1111')
  40. }
  41. current = null
  42. }
  43. },
  44. line: { //ctrl-c ctrl-v в rectangle. Так же добавьте его в <select id='tool'>
  45. mousedown(e){
  46. current = new Line(e.layerX, e.layerY, 0, 0, color.value, +size.value)
  47. },
  48. mousemove(e){
  49. if (!current) return;
  50. current.width = e.layerX - current.x
  51. current.height = e.layerY - current.y
  52. Drawable.drawAll()
  53. },
  54. mouseup(e){
  55. current = null
  56. }
  57. },
  58. rectangle: {
  59. mousedown(e){
  60. current = new Rectangle(e.layerX, e.layerY, 0, 0, color.value, +size.value)
  61. },
  62. mousemove(e){
  63. if (!current) return;
  64. current.width = e.layerX - current.x
  65. current.height = e.layerY - current.y
  66. Drawable.drawAll()
  67. },
  68. mouseup(e){
  69. current = null
  70. }
  71. },
  72. select: {
  73. click(e){
  74. console.log(e)
  75. let found = Drawable.instances.filter(c => c.in && c.in(e.layerX, e.layerY))
  76. if (found.length){
  77. if (e.ctrlKey){
  78. selection.push(found.pop())
  79. }
  80. else {
  81. selection = [found.pop()]
  82. }
  83. }
  84. else {
  85. if (!e.ctrlKey) selection = []
  86. }
  87. console.log('selection', selection)
  88. Drawable.drawAll(selection)
  89. },
  90. mousedown(e){
  91. //
  92. },
  93. mousemove(e){
  94. },
  95. mouseup(e){
  96. //x,y, w, h прямоугольника
  97. //selection - только те элеменеты Drawable.instances которые в границах прямоугольника.
  98. },
  99. }
  100. }
  101. function superHandler(evt){
  102. let t = tools[tool.value]
  103. if (typeof t[evt.type] === 'function')
  104. t[evt.type].call(this, evt)
  105. }
  106. canvas.onmousemove = superHandler
  107. canvas.onmouseup = superHandler
  108. canvas.onmousedown = superHandler
  109. canvas.onclick = superHandler
  110. ////
  111. function Drawable(){
  112. Drawable.addInstance(this);
  113. }
  114. const distance = (x1,y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  115. Drawable.prototype.draw = function(){};
  116. Drawable.prototype.distanceTo = function(x,y){
  117. if (typeof this.x !== 'number' ||
  118. typeof this.y !== 'number'){
  119. return NaN
  120. }
  121. return distance(this.x, this.y, x, y)
  122. };
  123. Drawable.instances = [];
  124. Drawable.addInstance = function(item){
  125. Drawable.instances.push(item);
  126. }
  127. Drawable.drawAll = function(selection=[]){
  128. ctx.clearRect(0,0,width,height);
  129. Drawable.forAll(item => item.draw())
  130. selection.forEach(item => item.draw(true))
  131. }
  132. Drawable.forAll = function(callback){
  133. for(var i = 0; i<Drawable.instances.length;i++){
  134. callback(Drawable.instances[i])
  135. }
  136. }
  137. class Circle extends Drawable {
  138. //__proto__: Drawable.prototype
  139. constructor(x,y,radius, color){
  140. super()
  141. this.x = x;
  142. this.y = y;
  143. this.radius = radius;
  144. this.color = color;
  145. this.draw();
  146. }
  147. draw(selected){
  148. ctx.beginPath();
  149. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  150. ctx.closePath();
  151. ctx.fillStyle = this.color;
  152. ctx.fill();
  153. if (selected){
  154. ctx.lineWidth = 2
  155. ctx.stroke();
  156. }
  157. }
  158. in(x,y){
  159. return this.distanceTo(x,y) < this.radius
  160. }
  161. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  162. return this.x >= x && this.x <= x + w &&
  163. this.y >= y && this.y <= y + h
  164. }
  165. }
  166. class Ellipse extends Drawable {
  167. constructor(x,y, radiusX, radiusY, color){
  168. super()
  169. this.x = x;
  170. this.y = y;
  171. this.radiusX = radiusX;
  172. this.radiusY = radiusY;
  173. this.color = color;
  174. this.draw();
  175. }
  176. draw(selected){
  177. ctx.beginPath();
  178. ctx.ellipse(this.x, this.y, this.radiusX, this.radiusY, 0, 0, 2 * Math.PI, false);
  179. ctx.closePath();
  180. ctx.fillStyle = this.color;
  181. ctx.fill();
  182. if (selected){
  183. ctx.lineWidth = 2
  184. ctx.stroke();
  185. }
  186. }
  187. in(x,y){
  188. return Math.pow((x - this.x), 2) / Math.pow(this.radiusX, 2) + Math.pow((y - this.y), 2) / Math.pow(this.radiusY, 2) <= 1;
  189. }
  190. }
  191. class Line extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  192. constructor(x,y, width, height, color, lineWidth){
  193. super()
  194. this.x = x;
  195. this.y = y;
  196. this.width = width;
  197. this.height = height;
  198. this.color = color;
  199. this.lineWidth = lineWidth;
  200. this.draw();
  201. }
  202. draw(selected){
  203. ctx.beginPath();
  204. ctx.moveTo(this.x, this.y);
  205. ctx.lineTo(this.x + this.width, this.y + this.height);
  206. ctx.closePath();
  207. ctx.strokeStyle = this.color;
  208. ctx.lineWidth = this.lineWidth
  209. ctx.stroke();
  210. }
  211. in(x,y){
  212. //Гарри Поттер и орден Арктангенса
  213. }
  214. }
  215. class Rectangle extends Drawable {
  216. constructor(x,y, width, height, color){
  217. super()
  218. this.x = x;
  219. this.y = y;
  220. this.width = width;
  221. this.height = height;
  222. this.color = color;
  223. this.draw();
  224. }
  225. draw(selected){
  226. ctx.fillStyle = this.color;
  227. ctx.fillRect(this.x, this.y, this.width, this.height);
  228. }
  229. in(x,y){
  230. let left = this.x;
  231. let right = this.x + this.width;
  232. let top = this.y;
  233. let bottom = this.y + this.height;
  234. return right >= x && left <= x && bottom >= y && top <= y;
  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. //}