script.js 6.9 KB

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