main.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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){
  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. select: {
  41. click(e){
  42. console.log(e)
  43. let found = Drawable.instances.filter(c => c.in && c.in(e.clientX, e.clientY))
  44. if (found.length){
  45. if (e.altKey){
  46. selection.push(found.pop())
  47. }
  48. else {
  49. selection = [found.pop()]
  50. }
  51. }
  52. else {
  53. if (!e.altKey) selection = []
  54. }
  55. Drawable.drawAll(selection)
  56. },
  57. mousedown(e){},
  58. mousemove(e){},
  59. mouseup(e){},
  60. },
  61. rectangle: {
  62. mousedown(e){
  63. current = new Rectangle(e.clientX, e.clientY, 0, 0, color.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. current = null
  73. }
  74. },
  75. ellipse: {
  76. mousedown(e){
  77. current = new Ellipse(e.clientX, e.clientY, 0, 0, color.value)
  78. },
  79. mousemove(e){
  80. if (!current) return;
  81. current.width = e.clientX - current.x
  82. current.height = e.clientY - current.y
  83. Drawable.drawAll()
  84. },
  85. mouseup(e){
  86. current = null
  87. }
  88. }
  89. }
  90. function superHandler(evt){
  91. let t = tools[tool.value]
  92. if (typeof t[evt.type] === 'function')
  93. t[evt.type].call(this, evt)
  94. }
  95. canvas.onmousemove = superHandler
  96. canvas.onmouseup = superHandler
  97. canvas.onmousedown = superHandler
  98. canvas.onclick = superHandler
  99. function Drawable(){
  100. Drawable.addInstance(this);
  101. }
  102. const distance = (x1, y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  103. Drawable.prototype.draw = function(){};
  104. Drawable.prototype.distanceTo = function(x,y){
  105. if (typeof this.x !== 'number' ||
  106. typeof this.y !== 'number'){
  107. return NaN
  108. }
  109. return distance(this.x, this.y, x, y)
  110. };
  111. Drawable.instances = [];
  112. Drawable.addInstance = function(item){
  113. Drawable.instances.push(item);
  114. }
  115. Drawable.drawAll = function(selection=[]){
  116. ctx.clearRect(0,0,width,height);
  117. Drawable.forAll(item => item.draw())
  118. selection.forEach(item => item.draw(true))
  119. }
  120. Drawable.forAll = function(callback){
  121. for(var i = 0; i<Drawable.instances.length;i++){
  122. callback(Drawable.instances[i])
  123. }
  124. }
  125. class Circle extends Drawable {
  126. constructor(x,y,radius, color){
  127. super()
  128. this.x = x
  129. this.y = y
  130. this.radius = radius
  131. this.color = color
  132. this.draw()
  133. }
  134. draw(selected){
  135. ctx.beginPath()
  136. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
  137. ctx.closePath()
  138. ctx.fillStyle = this.color
  139. if (selected){
  140. ctx.lineWidth = 2
  141. ctx.stroke()
  142. }
  143. ctx.fill()
  144. }
  145. in(x, y){
  146. return this.distanceTo(x, y) < this.radius
  147. }
  148. inBounds(x, y, w, h){
  149. return this.x >= x && this.x <= x + w && this.y >= y && this.y <= y + h
  150. }
  151. }
  152. class Line extends Drawable {
  153. constructor(x,y, width, height, color, lineWidth){
  154. super()
  155. this.x = x
  156. this.y = y
  157. this.width = width
  158. this.height = height
  159. this.color = color
  160. this.lineWidth = lineWidth
  161. this.draw()
  162. }
  163. draw(selected){
  164. ctx.beginPath()
  165. ctx.moveTo(this.x, this.y)
  166. ctx.lineTo(this.x + this.width, this.y + this.height)
  167. ctx.closePath()
  168. ctx.strokeStyle = this.color
  169. ctx.lineWidth = this.lineWidth
  170. if (selected){
  171. ctx.strokeStyle = '#f0f'
  172. ctx.stroke()
  173. }
  174. ctx.stroke()
  175. }
  176. in(x, y) {
  177. let rotateLine = Math.atan2(this.height, this.width)
  178. let rotCursor = Math.atan2(y - this.y, x - this.x)
  179. let distanceToPoint = distance(x, y, this.x, this.y)
  180. let angleLine = rotCursor - rotateLine
  181. let rotateX = Math.cos(angleLine) * distanceToPoint
  182. let rotateY = Math.sin(angleLine) * distanceToPoint
  183. return rotateX >= 0 && rotateX <= this.length && rotateY <= this.lineWidth/2 && rotateY >= -this.lineWidth/2
  184. }
  185. get length(){
  186. return this.distanceTo(this.x + this.width, this.y + this.height)
  187. }
  188. }
  189. class Rectangle extends Drawable{
  190. constructor(x, y, width, height, color){
  191. super()
  192. this.x = x
  193. this.y = y
  194. this.width = width
  195. this.height = height
  196. this.color = color
  197. this.draw()
  198. }
  199. draw(selected){
  200. ctx.beginPath()
  201. ctx.moveTo(this.x, this.y)
  202. ctx.fillRect(this.x, this.y, this.width, this.height)
  203. if (selected){
  204. ctx.rect(this.x, this.y, this.width, this.height)
  205. ctx.strokeStyle = '#f0f'
  206. ctx.stroke()
  207. }
  208. ctx.fillStyle = this.color
  209. ctx.stroke()
  210. }
  211. in(x, y){
  212. return x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height
  213. }
  214. }
  215. class Ellipse extends Drawable{
  216. #width = 0
  217. #height = 0
  218. constructor(x, y, width, height, color){
  219. super()
  220. this.x = x
  221. this.y = y
  222. this.#width = width
  223. this.#height = height
  224. this.color = color
  225. this.draw()
  226. }
  227. draw(selected){
  228. ctx.beginPath()
  229. ctx.moveTo(this.x, this.y)
  230. ctx.ellipse(this.x + this.rx, this.y + this.ry, this.rx, this.ry, 0, 0, 2 * Math.PI)
  231. ctx.closePath()
  232. if (selected){
  233. ctx.lineWidth = 2
  234. ctx.fill()
  235. }
  236. ctx.fillStyle = this.color
  237. ctx.fill()
  238. }
  239. in(x, y) {
  240. let h = this.x + this.rx
  241. let k = this.y + this.ry
  242. return Math.pow((x - h), 2)/Math.pow(this.rx, 2) + Math.pow((y - k), 2)/Math.pow(this.ry, 2) <= 1
  243. }
  244. set width(newWidth){
  245. if (newWidth < 0){
  246. this.#width = -newWidth
  247. this.x += newWidth
  248. }
  249. else
  250. this.#width = newWidth
  251. }
  252. set height(newHeight){
  253. if (newHeight < 0){
  254. this.#height = -newHeight
  255. this.y += newHeight
  256. }
  257. else
  258. this.#height = newHeight
  259. }
  260. get width(){
  261. return this.#width
  262. }
  263. get height(){
  264. return this.#height
  265. }
  266. get rx(){
  267. return this.#width/2
  268. }
  269. get ry(){
  270. return this.#height/2
  271. }
  272. }
  273. color.onchange = () => {
  274. selection.forEach(c => c.color = color.value)
  275. Drawable.drawAll(selection)
  276. }
  277. undo.onclick = function(){
  278. Drawable.instances.pop()
  279. Drawable.drawAll()
  280. }
  281. document.getElementById('delete').onclick = () =>{
  282. Drawable.instances = []
  283. Drawable.drawAll()
  284. }