script.js 6.9 KB

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