index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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.layerX, e.layerY, +size.value, color.value)
  11. }
  12. },
  13. circle: {
  14. mousedown(e){
  15. current = new Circle(e.layerX,e.layerY, 1, color.value)
  16. },
  17. mousemove(e){
  18. if (!current) return;
  19. current.radius = current.distanceTo(e.layerX, e.layerY)
  20. Drawable.drawAll()
  21. },
  22. mouseup(e){
  23. current = null
  24. }
  25. },
  26. line: {
  27. mousedown(e){
  28. current = new Line(e.layerX, e.layerY, 0, 0, color.value, +size.value)
  29. },
  30. mousemove(e){
  31. if (!current) return;
  32. current.width = e.layerX - current.x
  33. current.height = e.layerY - current.y
  34. Drawable.drawAll()
  35. },
  36. mouseup(e){
  37. current = null
  38. }
  39. },
  40. rectangle: {
  41. mousedown(e){
  42. current = new Rectangle(e.layerX, e.layerY, 0, 0, color.value)
  43. },
  44. mousemove(e){
  45. if (!current) return;
  46. current.width = e.layerX - current.x
  47. current.height = e.layerY - current.y
  48. Drawable.drawAll()
  49. },
  50. mouseup(e){
  51. current = null
  52. }
  53. },
  54. ellipse: {
  55. mousedown(e){
  56. current = new Ellipse(e.layerX, e.layerY, 0, 0, color.value)
  57. },
  58. mousemove(e){
  59. if (!current) return;
  60. current.width = e.layerX - current.x
  61. current.height = e.layerY - current.y
  62. Drawable.drawAll()
  63. },
  64. mouseup(e){
  65. current = null
  66. }
  67. },
  68. select: {
  69. click(e){
  70. console.log(e)
  71. let found = Drawable.instances.filter(c => c.in && c.in(e.layerX, e.layerY))
  72. if (found.length){
  73. if (e.ctrlKey){
  74. selection.push(found.pop())
  75. }
  76. else {
  77. selection = [found.pop()]
  78. }
  79. }
  80. else {
  81. if (!e.ctrlKey) selection = []
  82. }
  83. Drawable.drawAll(selection)
  84. },
  85. mousedown(e){
  86. //
  87. },
  88. mousemove(e){
  89. },
  90. mouseup(e){
  91. //x,y, w, h прямоугольника
  92. //selection - только те элеменеты Drawable.instances которые в границах прямоугольника.
  93. },
  94. }
  95. }
  96. function superHandler(evt){
  97. let t = tools[tool.value]
  98. if (typeof t[evt.type] === 'function')
  99. t[evt.type].call(this, evt)
  100. }
  101. canvas.onmousemove = superHandler
  102. canvas.onmouseup = superHandler
  103. canvas.onmousedown = superHandler
  104. canvas.onclick = superHandler
  105. ////
  106. function Drawable(){
  107. Drawable.addInstance(this);
  108. }
  109. const distance = (x1,y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  110. Drawable.prototype.draw = function(){};
  111. Drawable.prototype.distanceTo = function(x,y){
  112. if (typeof this.x !== 'number' ||
  113. typeof this.y !== 'number'){
  114. return NaN
  115. }
  116. return distance(this.x, this.y, x, y)
  117. };
  118. Drawable.instances = [];
  119. Drawable.addInstance = function(item){
  120. Drawable.instances.push(item);
  121. }
  122. Drawable.drawAll = function(selection=[]){
  123. ctx.clearRect(0,0,width,height);
  124. Drawable.forAll(item => item.draw())
  125. selection.forEach(item => item.draw(true))
  126. }
  127. Drawable.forAll = function(callback){
  128. for(var i = 0; i<Drawable.instances.length;i++){
  129. callback(Drawable.instances[i])
  130. }
  131. }
  132. class Circle extends Drawable {
  133. constructor(x,y,radius, color){
  134. super()
  135. this.x = x;
  136. this.y = y;
  137. this.radius = radius;
  138. this.color = color;
  139. this.draw();
  140. }
  141. draw(selected){
  142. ctx.beginPath();
  143. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  144. ctx.closePath();
  145. ctx.fillStyle = this.color;
  146. if (selected){
  147. ctx.lineWidth = 2
  148. ctx.stroke();
  149. }
  150. ctx.fill();
  151. }
  152. in(x,y){
  153. return this.distanceTo(x,y) < this.radius
  154. }
  155. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  156. return this.x >= x && this.x <= x + w &&
  157. this.y >= y && this.y <= y + h
  158. }
  159. }
  160. class Line extends Drawable {
  161. constructor(x,y, width, height, color, lineWidth){
  162. super()
  163. this.x = x;
  164. this.y = y;
  165. this.width = width;
  166. this.height = height;
  167. this.color = color;
  168. this.lineWidth = lineWidth;
  169. this.draw();
  170. }
  171. draw(selected){
  172. if (selected){
  173. ctx.beginPath()
  174. ctx.moveTo(this.x, this.y)
  175. ctx.lineTo(this.x + this.width, this.y + this.height)
  176. ctx.closePath()
  177. ctx.strokeStyle = '#000000'
  178. ctx.lineWidth = this.lineWidth + 2
  179. ctx.stroke()
  180. }
  181. ctx.beginPath()
  182. ctx.moveTo(this.x, this.y)
  183. ctx.lineTo(this.x + this.width, this.y + this.height)
  184. ctx.closePath()
  185. ctx.strokeStyle = this.color
  186. ctx.lineWidth = this.lineWidth
  187. ctx.stroke()
  188. }
  189. in(x,y){
  190. let lineAngle = Math.atan2(this.height, this.width);
  191. let mouseAngle = Math.atan2(y - this.y, x - this.x);
  192. let lineTurnAngle = mouseAngle - lineAngle;
  193. let cursorWidth = distance(this.x,this.y,x,y);
  194. let rotX = Math.cos(lineTurnAngle) * cursorWidth;
  195. let rotY = Math.sin(lineTurnAngle) * cursorWidth;
  196. if (rotX > 0 && rotX < this.distanceTo(this.x + this.width, this.y + this.height) && (rotY > -this.lineWidth / 2)
  197. && (rotY < this.lineWidth / 2)) {
  198. // console.log('попал')
  199. return true
  200. } else {
  201. // console.log('мимо')
  202. return false
  203. }
  204. }
  205. }
  206. class Rectangle extends Drawable {
  207. constructor(x,y, width, height, color){
  208. super()
  209. this.x = x;
  210. this.y = y;
  211. this.width = width;
  212. this.height = height;
  213. this.color = color;
  214. this.draw();
  215. }
  216. // invertColor(hex){
  217. // function padZero(str, len) {
  218. // len = len || 2;
  219. // let zeros = new Array(len).join('0');
  220. // return (zeros + str).slice(-len);
  221. // }
  222. // if (hex.indexOf('#') === 0) {
  223. // hex = hex.slice(1);
  224. // }
  225. // // convert 3-digit hex to 6-digits.
  226. // if (hex.length === 3) {
  227. // hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  228. // }
  229. // if (hex.length !== 6) {
  230. // throw new Error('Invalid HEX color.');
  231. // }
  232. // // invert color components
  233. // let r = (255 - parseInt(hex.slice(0, 2), 16)).toString(16),
  234. // g = (255 - parseInt(hex.slice(2, 4), 16)).toString(16),
  235. // b = (255 - parseInt(hex.slice(4, 6), 16)).toString(16);
  236. // // pad each with zeros and return
  237. // return '#' + padZero(r) + padZero(g) + padZero(b);
  238. // }
  239. draw(selected){
  240. ctx.beginPath();
  241. ctx.moveTo(this.x, this.y);
  242. ctx.fillStyle = this.color;
  243. ctx.fillRect(this.x, this.y, this.width, this.height);
  244. if (selected){
  245. ctx.rect(this.x, this.y, this.width, this.height);
  246. ctx.lineWidth = 2;
  247. ctx.stroke();
  248. }
  249. ctx.closePath();
  250. }
  251. in(x,y){
  252. if (this.width > 0 && this.height > 0) {
  253. if (this.x < x && this.x + this.width > x &&
  254. this.y < y && this.y + this.height > y) {
  255. // console.log('попал', x, y, this.x, this.y, this.width, this.height)
  256. return true
  257. } else {
  258. // console.log('мимо', x, y, this.x, this.y, this.width, this.height)
  259. return false
  260. }
  261. }
  262. if (this.width < 0 && this.height < 0) {
  263. if (this.x > x && this.x + this.width < x &&
  264. this.y > y && this.y + this.height < y) {
  265. return true
  266. } else {
  267. return false
  268. }
  269. }
  270. if (this.width > 0 && this.height < 0) {
  271. if (this.x < x && this.x + this.width > x &&
  272. this.y > y && this.y + this.height < y) {
  273. return true
  274. } else {
  275. return false
  276. }
  277. }
  278. if (this.width < 0 && this.height > 0) {
  279. if (this.x > x && this.x + this.width < x &&
  280. this.y < y && this.y + this.height > y) {
  281. return true
  282. } else {
  283. return false
  284. }
  285. }
  286. }
  287. }
  288. class Ellipse extends Drawable {
  289. #width = 0
  290. #height = 0
  291. set width(newWidth){
  292. if (newWidth < 0){
  293. this.#width = -newWidth
  294. this.x += newWidth/2
  295. }
  296. else
  297. this.#width = newWidth
  298. }
  299. set height(newHeight){
  300. if (newHeight < 0){
  301. this.#height = -newHeight
  302. this.y += newHeight/2
  303. }
  304. else
  305. this.#height = newHeight
  306. }
  307. get width(){
  308. return this.#width
  309. }
  310. get height(){
  311. return this.#height
  312. }
  313. get rx(){
  314. return this.#width/2
  315. }
  316. get ry(){
  317. return this.#height/2
  318. }
  319. constructor(x,y, width, height, color){
  320. super()
  321. this.x = x;
  322. this.y = y;
  323. this.width = width;
  324. this.height = height;
  325. this.color = color;
  326. this.draw();
  327. }
  328. draw(selected){
  329. ctx.beginPath();
  330. ctx.ellipse(this.x + this.rx, this.y + this.ry, this.rx, this.ry, 0, 0, 2 * Math.PI);
  331. ctx.closePath();
  332. ctx.fillStyle = this.color;
  333. if (selected){
  334. ctx.lineWidth = 2
  335. ctx.strokeStyle = '#000000'
  336. ctx.stroke()
  337. }
  338. ctx.fill()
  339. }
  340. in(x,y){
  341. return (( (x - (this.x + this.rx))**2 / this.rx**2 ) + ( (y - (this.y + this.ry))**2 / this.ry**2 )) <= 1
  342. }
  343. }
  344. color.onchange = () => {
  345. selection.forEach(c => c.color = color.value)
  346. Drawable.drawAll(selection)
  347. }
  348. document.getElementById('delete').onclick = () =>{
  349. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  350. selection = []
  351. Drawable.drawAll()
  352. }
  353. undo.onclick = function(){
  354. Drawable.instances.pop()
  355. Drawable.drawAll()
  356. }
  357. //new Line(0,0,100,100, "red")
  358. ////new Circle(30,30,10, "red")
  359. ////canvas.onmousemove = function(e){
  360. ////}