main.js 7.8 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. 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: {
  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 Ellips(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. Drawable.drawAll(selection)
  84. },
  85. mousedown(e){
  86. //
  87. },
  88. mousemove(e){
  89. },
  90. mouseup(e){
  91. //x,y, w, h прямоугольника
  92. //selection - только те элеменеты Drawable.instances которые в границах прямоугольника.
  93. },
  94. }
  95. }
  96. function superHandler(evt){
  97. let t = tools[tool.value]
  98. if (typeof t[evt.type] === 'function')
  99. t[evt.type].call(this, evt)
  100. }
  101. canvas.onmousemove = superHandler
  102. canvas.onmouseup = superHandler
  103. canvas.onmousedown = superHandler
  104. canvas.onclick = superHandler
  105. ////
  106. function Drawable(){
  107. Drawable.addInstance(this);
  108. }
  109. const distance = (x1,y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  110. Drawable.prototype.draw = function(){};
  111. Drawable.prototype.distanceTo = function(x,y){
  112. if (typeof this.x !== 'number' ||
  113. typeof this.y !== 'number'){
  114. return NaN
  115. }
  116. return distance(this.x, this.y, x, y)
  117. };
  118. Drawable.instances = [];
  119. Drawable.addInstance = function(item){
  120. Drawable.instances.push(item);
  121. }
  122. Drawable.drawAll = function(selection=[]){
  123. ctx.clearRect(0,0,width,height);
  124. Drawable.forAll(item => item.draw())
  125. selection.forEach(item => item.draw(true))
  126. }
  127. Drawable.forAll = function(callback){
  128. for(var i = 0; i<Drawable.instances.length;i++){
  129. callback(Drawable.instances[i])
  130. }
  131. }
  132. class Circle extends Drawable {
  133. //__proto__: Drawable.prototype
  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 { //смочь скопировать в Rectangle и поменять отприсовку
  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.moveTo(this.x, this.y);
  175. ctx.lineTo(this.x + this.width, this.y + this.height);
  176. ctx.closePath();
  177. ctx.strokeStyle = this.color;
  178. ctx.lineWidth = this.lineWidth
  179. ctx.stroke();
  180. }
  181. in(x, y) {
  182. let rotateLine = Math.atan2(this.height, this.width)
  183. let rotCursor = Math.atan2(y - this.y, x - this.x)
  184. let distanceToPoint = distance(x, y, this.x, this.y)
  185. let angleLine = rotCursor - rotateLine
  186. let rotateX = Math.cos(angleLine) * distanceToPoint
  187. let rotateY = Math.sin(angleLine) * distanceToPoint
  188. return rotateX >= 0 && rotateX <= this.length && rotateY <= this.lineWidth/2 && rotateY >= -this.lineWidth/2
  189. }
  190. get length(){
  191. return this.distanceTo(this.x + this.width, this.y + this.height)
  192. }
  193. }
  194. class Rectangle extends Drawable {
  195. constructor(x,y, width, height, color, lineWidth){
  196. super()
  197. this.x = x;
  198. this.y = y;
  199. this.width = width;
  200. this.height = height;
  201. this.color = color;
  202. this.lineWidth = lineWidth;
  203. this.draw();
  204. }
  205. draw(selected){
  206. ctx.beginPath();
  207. ctx.rect(this.x, this.y, this.width, this.height);
  208. ctx.closePath();
  209. ctx.fillStyle = this.color;
  210. ctx.lineWidth = 2;
  211. ctx.fill();
  212. ctx.stroke();
  213. }
  214. in(x, y) {
  215. return this.x <= x &&this.x <= this.x + this.width &&
  216. this.y <= y && this.y <= this.y + this.height
  217. }
  218. }
  219. class Ellips extends Drawable {
  220. constructor(x, y, width, height, color, lineWidth){
  221. super()
  222. this.x = x;
  223. this.y = y;
  224. this.width = width;
  225. this.height = height;
  226. this.color = color;
  227. this.lineWidth = lineWidth;
  228. this.draw();
  229. }
  230. draw(selected){
  231. ctx.beginPath();
  232. ctx.ellipse(this.x + this.width / 2, this.y + this.height / 2, this.width / 2, this.height / 2, 0, 0, 2 * Math.PI);
  233. ctx.closePath();
  234. ctx.fillStyle = this.color;
  235. ctx.lineWidth = 2;
  236. ctx.fill();
  237. ctx.stroke();
  238. }
  239. in(x, y) {
  240. const rx = this.width / 2
  241. const ry = this.height / 2
  242. const h = this.x + rx
  243. const k = this.y + ry
  244. return ((((x - h) ** 2) / ((rx) ** 2)) + (((y - k) ** 2) / ((ry) ** 2))) <= 1
  245. }
  246. }
  247. color.onchange = () => {
  248. selection.forEach(c => c.color = color.value)
  249. Drawable.drawAll(selection)
  250. }
  251. document.getElementById('delete').onclick = () =>{
  252. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  253. selection = []
  254. Drawable.drawAll()
  255. }
  256. //new Line(0,0,100,100, "red")
  257. ////new Circle(30,30,10, "red")
  258. ////canvas.onmousemove = function(e){
  259. ////}
  260. //undo.onclick = function(){
  261. //Drawable.instances.pop()
  262. ////Drawable.instances = []
  263. //Drawable.drawAll()
  264. //}