script.js 7.3 KB

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