script.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 & 1) && new Circle(e.clientX, e.clientY, +size.value, color.value)
  11. }
  12. },
  13. circle: {
  14. mousedown(e){
  15. current = new Circle(e.clientX,e.clientY, 1, color.value)
  16. },
  17. mousemove(e){
  18. if (!current) return;
  19. current.radius = current.distanceTo(e.clientX, e.clientY)
  20. Drawable.drawAll()
  21. },
  22. mouseup(e){
  23. current = null
  24. }
  25. },
  26. rectangle: {
  27. mousedown(e){
  28. current = new Rectangle(e.clientX,e.clientY, 1, 1, color.value)
  29. },
  30. mousemove(e){
  31. if (!current) return;
  32. ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
  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. line: {
  42. mousedown(e){
  43. current = new Line(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. select: {
  56. click(e){
  57. console.log(e)
  58. let found = Drawable.instances.filter(c => c.in && c.in(e.clientX, e.clientY))
  59. if (found.length){
  60. if (e.ctrlKey){
  61. selection.push(found.pop())
  62. }
  63. else {
  64. selection = [found.pop()]
  65. }
  66. }
  67. else {
  68. if (!e.ctrlKey) selection = []
  69. }
  70. Drawable.drawAll(selection)
  71. },
  72. mousedown(e){
  73. //
  74. },
  75. mousemove(e){
  76. },
  77. mouseup(e){
  78. //x,y, w, h прямоугольника
  79. //selection - только те элеменеты Drawable.instances которые в границах прямоугольника.
  80. },
  81. }
  82. }
  83. function superHandler(evt){
  84. let t = tools[tool.value]
  85. if (typeof t[evt.type] === 'function')
  86. t[evt.type].call(this, evt)
  87. }
  88. canvas.onmousemove = superHandler
  89. canvas.onmouseup = superHandler
  90. canvas.onmousedown = superHandler
  91. canvas.onclick = superHandler
  92. ////
  93. function Drawable(){
  94. Drawable.addInstance(this);
  95. }
  96. const distance = (x1,y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  97. Drawable.prototype.draw = function(){};
  98. Drawable.prototype.distanceTo = function(x,y){
  99. if (typeof this.x !== 'number' ||
  100. typeof this.y !== 'number'){
  101. return NaN
  102. }
  103. return distance(this.x, this.y, x, y)
  104. };
  105. Drawable.instances = [];
  106. Drawable.addInstance = function(item){
  107. Drawable.instances.push(item);
  108. }
  109. Drawable.drawAll = function(selection=[]){
  110. ctx.clearRect(0,0,width,height);
  111. Drawable.forAll(item => item.draw())
  112. selection.forEach(item => item.draw(true))
  113. }
  114. Drawable.forAll = function(callback){
  115. for(var i = 0; i<Drawable.instances.length;i++){
  116. callback(Drawable.instances[i])
  117. }
  118. }
  119. class Circle extends Drawable {
  120. constructor(x,y,radius, color){
  121. debugger
  122. super()
  123. this.x = x;
  124. this.y = y;
  125. this.radius = radius;
  126. this.color = color;
  127. this.draw();
  128. }
  129. draw(selected){
  130. ctx.beginPath();
  131. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  132. ctx.closePath();
  133. ctx.fillStyle = this.color;
  134. if (selected){
  135. ctx.lineWidth = 2
  136. ctx.stroke();
  137. }
  138. ctx.fill();
  139. }
  140. in(x,y){
  141. return this.distanceTo(x,y) < this.radius
  142. }
  143. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  144. return this.x >= x && this.x <= x + w &&
  145. this.y >= y && this.y <= y + h
  146. }
  147. }
  148. class Line extends Drawable {
  149. constructor(x,y, width, height, color, lineWidth){
  150. super()
  151. this.x = x;
  152. this.y = y;
  153. this.width = width;
  154. this.height = height;
  155. this.color = color;
  156. this.lineWidth = lineWidth;
  157. this.draw();
  158. }
  159. draw(){
  160. ctx.beginPath();
  161. ctx.moveTo(this.x, this.y);
  162. ctx.lineTo(this.x + this.width, this.y + this.height);
  163. ctx.closePath();
  164. ctx.strokeStyle = this.color;
  165. ctx.lineWidth = this.lineWidth
  166. ctx.stroke();
  167. }
  168. }
  169. color.onchange = () => {
  170. selection.forEach(c => c.color = color.value)
  171. Drawable.drawAll(selection)
  172. }
  173. document.getElementById('delete').onclick = () =>{
  174. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  175. selection = []
  176. Drawable.drawAll()
  177. }
  178. //new Line(0,0,100,100, "red")
  179. ////new Circle(30,30,10, "red")
  180. ////canvas.onmousemove = function(e){
  181. ////}
  182. //undo.onclick = function(){
  183. //Drawable.instances.pop()
  184. ////Drawable.instances = []
  185. //Drawable.drawAll()
  186. //}
  187. class Rectangle extends Drawable {
  188. constructor(x,y, width, height, color){
  189. debugger
  190. super()
  191. this.x = x;
  192. this.y = y;
  193. this.width = width;
  194. this.height = height;
  195. this.color = color;
  196. this.draw();
  197. }
  198. draw(){
  199. ctx.beginPath();
  200. ctx.moveTo(this.x, this.y);
  201. ctx.rect(this.x, this.y, this.width, this.height);
  202. ctx.closePath();
  203. ctx.fillStyle = this.color;
  204. ctx.fill();
  205. }
  206. }