main.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. elipse: {
  27. mousedown(e){
  28. current = new Elipse(e.clientX,e.clientY, 1,1, color.value)
  29. },
  30. mousemove(e){
  31. if (!current) return;
  32. var width = e.clientX - current.x;
  33. var height = e.clientY - current.y;
  34. current.width = width ? width : width * -1;
  35. current.height = height ? height : height * -1;
  36. Drawable.drawAll()
  37. },
  38. mouseup(e){
  39. current = null
  40. }
  41. },
  42. line: {
  43. mousedown(e){
  44. current = new Line(e.clientX, e.clientY, 0, 0, color.value, +size.value)
  45. },
  46. mousemove(e){
  47. if (!current) return;
  48. current.width = e.clientX - current.x
  49. current.height = e.clientY - current.y
  50. Drawable.drawAll()
  51. },
  52. mouseup(e){
  53. current = null
  54. }
  55. },
  56. rectangle : {
  57. mousedown(e){
  58. current = new Rectangle(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 Elipse extends Drawable {
  163. constructor(x,y,width,height,color){
  164. super()
  165. this.x = x ? x : x*-1;
  166. this.y = y ? y : y*-1;
  167. this.width = width;
  168. this.height = height;
  169. this.color = color;
  170. this.draw();
  171. }
  172. draw(){
  173. this.width = this.width > 0 ? this.width : this.width * (-1);
  174. this.height = this.height > 0 ? this.height : this.height * (-1);
  175. ctx.beginPath();
  176. ctx.moveTo(this.x, this.y);
  177. ctx.ellipse(this.x, this.y, this.height, this.width, Math.PI / 2, 0, 2 * Math.PI);
  178. ctx.closePath();
  179. ctx.fillStyle = this.color;
  180. ctx.fill();
  181. }
  182. in(x,y){
  183. return this.distanceTo(x,y) < this.radius
  184. }
  185. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  186. return this.x >= x && this.x <= x + w &&
  187. this.y >= y && this.y <= y + h
  188. }
  189. }
  190. class Line extends Drawable {
  191. constructor(x,y, width, height, color, lineWidth){
  192. super()
  193. this.x = x;
  194. this.y = y;
  195. this.width = width;
  196. this.height = height;
  197. this.color = color;
  198. this.lineWidth = lineWidth;
  199. this.draw();
  200. }
  201. draw(){
  202. ctx.beginPath();
  203. ctx.moveTo(this.x, this.y);
  204. ctx.lineTo(this.x + this.width, this.y + this.height);
  205. ctx.closePath();
  206. ctx.strokeStyle = this.color;
  207. ctx.lineWidth = this.lineWidth
  208. ctx.stroke();
  209. }
  210. }
  211. class Rectangle extends Drawable {
  212. constructor(x,y, width, height, color, lineWidth){
  213. super()
  214. this.x = x;
  215. this.y = y;
  216. this.width = width;
  217. this.height = height;
  218. this.color = color;
  219. this.lineWidth = lineWidth;
  220. this.draw();
  221. }
  222. draw(){
  223. ctx.beginPath();
  224. ctx.rect(this.x , this.y ,this.width,this.height);
  225. ctx.fillStyle = this.color;
  226. ctx.fill();
  227. }
  228. }
  229. color.onchange = () => {
  230. selection.forEach(c => c.color = color.value)
  231. Drawable.drawAll(selection)
  232. }
  233. document.getElementById('delete').onclick = () =>{
  234. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  235. selection = []
  236. Drawable.drawAll()
  237. }
  238. //new Line(0,0,100,100, "red")
  239. ////new Circle(30,30,10, "red")
  240. ////canvas.onmousemove = function(e){
  241. ////}
  242. undo.onclick = function(){
  243. Drawable.instances.pop()
  244. //Drawable.instances = []
  245. Drawable.drawAll()
  246. }