main.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //debugger;
  2. const canvas = document.getElementById('canvas')
  3. const ctx = canvas.getContext('2d')
  4. const width = canvas.width;
  5. const height = canvas.height;
  6. let current;
  7. let selection = []
  8. const tools = {
  9. graffity: {
  10. mousemove(e){ //e.buttons 0b00000x11 & 0b00000100 == x
  11. (e.buttons & 1) && new Circle(e.clientX, e.clientY, +size.value, color.value)
  12. }
  13. },
  14. circle: {
  15. mousedown(e){
  16. current = new Circle(e.clientX,e.clientY, 1, color.value)
  17. },
  18. mousemove(e){
  19. if (!current) return;
  20. current.radius = current.distanceTo(e.clientX, e.clientY)
  21. Drawable.drawAll()
  22. },
  23. mouseup(e){
  24. current = null
  25. }
  26. },
  27. rectangl: {
  28. mousedown(e){
  29. current = new Rectangle(e.clientX, e.clientY, 0, 0, color.value, +size.value)
  30. },
  31. mousemove(e){
  32. if (!current) return;
  33. current.width = e.clientX - current.x
  34. current.height = e.clientY - current.y
  35. Drawable.drawAll()
  36. },
  37. mouseup(e){
  38. current = null
  39. }
  40. },
  41. ellipse: {
  42. mousedown(e){
  43. current = new Ellipse(e.clientX, e.clientY, 0, 0, color.value, +size.value)
  44. },
  45. mousemove(e){
  46. if (!current) return;
  47. current.width = e.clientX - current.x
  48. current.height = e.clientY - current.y
  49. Drawable.drawAll()
  50. },
  51. mouseup(e){
  52. current = null
  53. }
  54. },
  55. line: {
  56. mousedown(e){
  57. current = new Line(e.clientX, e.clientY, 0, 0, color.value, +size.value)
  58. },
  59. mousemove(e){
  60. if (!current) return;
  61. current.width = e.clientX - current.x
  62. current.height = e.clientY - current.y
  63. Drawable.drawAll()
  64. },
  65. mouseup(e){
  66. current = null
  67. }
  68. },
  69. select: {
  70. click(e){
  71. console.log(e)
  72. let found = Drawable.instances.filter(c => c.in && c.in(e.clientX, e.clientY))
  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(){
  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. }
  182. color.onchange = () => {
  183. selection.forEach(c => c.color = color.value)
  184. Drawable.drawAll(selection)
  185. }
  186. document.getElementById('delete').onclick = () =>{
  187. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  188. selection = []
  189. Drawable.drawAll()
  190. }
  191. // new Line(0,0,100,100, "red")
  192. // //new Circle(30,30,10, "red")
  193. // //canvas.onmousemove = function(e){
  194. // //}
  195. // undo.onclick = function(){
  196. // Drawable.instances.pop()
  197. // //Drawable.instances = []
  198. // Drawable.drawAll()
  199. // }
  200. class Rectangle extends Drawable {
  201. constructor(x,y, width, height, color){
  202. super()
  203. this.x = x;
  204. this.y = y;
  205. this.width = width;
  206. this.height = height;
  207. this.color = color;
  208. this.draw();
  209. }
  210. draw(check){
  211. ctx.beginPath(); console.log('check', check)
  212. ctx.moveTo(this.x, this.y);
  213. ctx.lineTo(this.x + this.width, this.y);
  214. ctx.lineTo(this.x + this.width, this.y + this.height);
  215. ctx.lineTo(this.x , this.y + this.height);
  216. ctx.closePath();
  217. ctx.fillStyle = this.color;
  218. if (check) {
  219. ctx.strokeStyle = 'red';
  220. } else {
  221. ctx.strokeStyle = this.color;
  222. }
  223. ctx.fillRect(this.x, this.y, this.width, this.height);
  224. ctx.stroke();
  225. }
  226. in(x,y){
  227. let t =((this.x < x) && (x < (this.x + this.width))
  228. && (this.y < y) && (y < (this.y + this.height)));
  229. console.log('t', t);
  230. return t;
  231. }
  232. }
  233. class Ellipse extends Drawable {
  234. constructor(x,y, width, height, color){
  235. super()
  236. this.x = x;
  237. this.y = y;
  238. this.width = width/2;
  239. this.height = height/2;
  240. this.color = color;
  241. this.draw();
  242. }
  243. draw(check){
  244. ctx.beginPath();
  245. ctx.ellipse(this.x, this.y, this.width, this.height, Math.PI / 2, 0, 2 * Math.PI );
  246. ctx.fillStyle = this.color;
  247. ctx.fill();
  248. ctx.strokeStyle = this.color;
  249. if (check) {
  250. ctx.strokeStyle = 'red';
  251. }
  252. ctx.stroke();
  253. }
  254. in(x, y) {
  255. const t = ((x - this.x )**2 / (this.height**2)) + ((y -this.y )**2 / (this.width**2)) <= 1;
  256. console.log('t1', t);
  257. return t;
  258. }
  259. }