index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 & 0b001) && new Circle(e.layerX, e.layerY, +size.value, color.value, +size.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. ellipse: {
  27. mousedown(e){
  28. current = new Ellipse(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. line: {
  41. mousedown(e){
  42. current = new Line(e.layerX, e.layerY, 0, 0, color.value, +size.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. rectangle: {
  55. mousedown(e){
  56. current = new Rectangle(e.layerX, e.layerY, 0, 0, color.value, +size.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. selection = [];
  71. e.stopImmediatePropagation();
  72. if (current.width||current.height){
  73. let foundInBounds = Drawable.instances.filter(c => c.inBounds && c.inBounds(current.x, current.y, current.width, current.height))
  74. console.log('foundInBounds:');
  75. console.log(foundInBounds);
  76. if (foundInBounds.length) {
  77. selection = [...foundInBounds];
  78. }
  79. Drawable.instances.pop();
  80. }
  81. else {
  82. if (!selection.length) {
  83. let found = Drawable.instances.filter(c => c.in && c.in(e.layerX, e.layerY))
  84. console.log('found:')
  85. console.log(found)
  86. if (found.length) {
  87. if (e.ctrlKey) {
  88. selection.push(found.pop())
  89. }
  90. else {
  91. selection = [found.pop()]
  92. }
  93. }
  94. else {
  95. if (!e.ctrlKey) selection = []
  96. }
  97. }
  98. }
  99. Drawable.drawAll(selection)
  100. current = null;
  101. },
  102. mousedown(e){
  103. //
  104. current = new BoundsRectangle(e.layerX, e.layerY, 0, 0, 'grey', +size.value)
  105. },
  106. mousemove(e) {
  107. if (!current) return;
  108. current.width = e.layerX - current.x
  109. current.height = e.layerY - current.y
  110. Drawable.drawAll()
  111. },
  112. mouseup(e){
  113. //x,y, w, h прямоугольника
  114. //selection - только те элеменеты Drawable.instances которые в границах прямоугольника.
  115. {
  116. console.log(e);
  117. }
  118. },
  119. }
  120. }
  121. function superHandler(evt){
  122. let t = tools[tool.value]
  123. if (typeof t[evt.type] === 'function')
  124. t[evt.type].call(this, evt)
  125. }
  126. canvas.onmousemove = superHandler
  127. canvas.onmouseup = superHandler
  128. canvas.onmousedown = superHandler
  129. canvas.onclick = superHandler
  130. ////
  131. function Drawable(){
  132. Drawable.addInstance(this);
  133. }
  134. const distance = (x1,y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  135. Drawable.prototype.draw = function(){};
  136. Drawable.prototype.distanceTo = function(x,y){
  137. if (typeof this.x !== 'number' ||
  138. typeof this.y !== 'number'){
  139. return NaN
  140. }
  141. return distance(this.x, this.y, x, y)
  142. };
  143. Drawable.instances = [];
  144. Drawable.addInstance = function(item){
  145. Drawable.instances.push(item);
  146. }
  147. Drawable.drawAll = function(selection=[]){
  148. ctx.clearRect(0,0,width,height);
  149. Drawable.forAll(item => item.draw())
  150. selection.forEach(item => item.draw(true))
  151. }
  152. Drawable.forAll = function(callback){
  153. for(var i = 0; i<Drawable.instances.length;i++){
  154. callback(Drawable.instances[i])
  155. }
  156. }
  157. class Circle extends Drawable {
  158. //__proto__: Drawable.prototype
  159. constructor(x,y,radius, color, lineWidth){
  160. super()
  161. this.x = x;
  162. this.y = y;
  163. this.radius = radius;
  164. this.color = color;
  165. this.lineWidth = lineWidth;
  166. this.draw();
  167. }
  168. draw(selected){
  169. ctx.beginPath();
  170. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  171. ctx.closePath();
  172. ctx.fillStyle = this.color;
  173. ctx.lineWidth = this.lineWidth;
  174. if (selected){
  175. ctx.lineWidth = 2
  176. ctx.stroke();
  177. }
  178. ctx.fill();
  179. }
  180. in(x,y){
  181. return this.distanceTo(x,y) < this.radius
  182. }
  183. inBounds(x, y, w, h) { // x = 100, this.x = 102, w = 5
  184. return ((w>0)?(this.x-this.radius >= x && this.x+this.radius <= x + w):(this.x-this.radius >= x+w && this.x+this.radius <= x)) &&
  185. ((h>0)?(this.y-this.radius >= y && this.y+this.radius <= y + h ):(this.y-this.radius >= y+h && this.y+this.radius <= y ))
  186. }
  187. }
  188. class Ellipse extends Drawable {
  189. //__proto__: Drawable.prototype
  190. constructor(x,y, width, height, color, lineWidth){
  191. super()
  192. this.x = x;
  193. this.y = y;
  194. this.width = width;
  195. this.height = height;
  196. this.color = color;
  197. this.lineWidth = lineWidth;
  198. this.draw();
  199. }
  200. draw(selected){
  201. ctx.beginPath();
  202. ctx.moveTo(this.x, this.y);
  203. ctx.ellipse(this.x, this.y, ((this.width**2)**0.5), ((this.height**2)**0.5), 0, 0, Math.PI * 2)
  204. ctx.closePath();
  205. ctx.strokeStyle = this.color;
  206. ctx.fillStyle = this.color;
  207. ctx.lineWidth = this.lineWidth;
  208. if (selected){
  209. ctx.lineWidth = 2
  210. ctx.stroke();
  211. }
  212. ctx.fill();
  213. }
  214. in (x, y){
  215. return (
  216. ((((x - this.x) ** 2) / ((this.width ) ** 2)) + (((y - this.y) ** 2 )/( (this.height) ** 2))) <= 1
  217. )
  218. }
  219. inBounds(x, y, w, h) {
  220. return ((w>0)?(this.x-this.width >= x && this.x+this.width <= x + w):(this.x-this.width >= x+w && this.x+this.width <= x)) &&
  221. ((h>0)?(this.y-this.height >= y && this.y+this.height <= y + h ):(this.y-this.height >= y+h && this.y+this.height <= y ))
  222. }
  223. }
  224. class Line extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  225. constructor(x,y, width, height, color, lineWidth){
  226. super()
  227. this.x = x;
  228. this.y = y;
  229. this.width = width;
  230. this.height = height;
  231. this.color = color;
  232. this.lineWidth = lineWidth;
  233. this.draw();
  234. }
  235. draw(){
  236. ctx.beginPath();
  237. ctx.moveTo(this.x, this.y);
  238. ctx.lineTo(this.x + this.width, this.y + this.height);
  239. ctx.closePath();
  240. ctx.strokeStyle = this.color;
  241. ctx.lineWidth = this.lineWidth
  242. ctx.stroke();
  243. }
  244. in(x, y) {
  245. const deltaAngle = Math.atan2(y - this.y, x - this.x) - Math.atan2(this.height, this.width) ;
  246. const newX = Math.cos(deltaAngle) * this.distanceTo(x, y);
  247. const newY = Math.sin(deltaAngle) * this.distanceTo(x, y);
  248. return ((newX>=0)&&(newX<=distance(0,0, this.width, this.height))&&(newY>=-this.lineWidth/2)&&(newY<=this.lineWidth/2)
  249. )
  250. }
  251. inBounds(x, y, w, h) {
  252. let areaX = false;
  253. let areaY = false;
  254. const angleX = Math.atan2(this.height, this.width);
  255. const deltaX = Math.sin(angleX) * this.lineWidth/2;
  256. const deltaY = Math.cos(angleX) * this.lineWidth/2;
  257. if (this.width > 0) { areaX=(w > 0) ? (this.x-deltaX >= x && this.x + this.width+deltaX <= x + w):(this.x-deltaX >= x + w && this.x + this.width+deltaX <= x)}
  258. else { areaX= ((w > 0) ? (this.x+ this.width-deltaX >= x && this.x+deltaX <= x + w):(this.x + this.width-deltaX>= x + w && this.x+deltaX <= x)) }
  259. if (this.height > 0) { areaY=(h > 0) ? (this.y-deltaY >= y && this.y + this.height+deltaY <= y + h):(this.y-deltaY >= y + h && this.y + this.height+deltaY <= y)}
  260. else { areaY= (h > 0) ? (this.y+ this.height-deltaY >= y && this.y+deltaY <= y + h):(this.y + this.height-deltaY>= y + h && this.y+deltaY<= y) }
  261. return areaX && areaY;
  262. }
  263. }
  264. color.onchange = () => {
  265. selection.forEach(c => c.color = color.value)
  266. Drawable.drawAll(selection)
  267. }
  268. document.getElementById('delete').onclick = () =>{
  269. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  270. selection = []
  271. Drawable.drawAll()
  272. }
  273. class Rectangle extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  274. constructor(x, y, width, height, color, lineWidth) {
  275. super()
  276. this.x = x;
  277. this.y = y;
  278. this.width = width;
  279. this.height = height;
  280. this.color = color;
  281. this.lineWidth = lineWidth;
  282. this.draw();
  283. }
  284. draw(selected){
  285. ctx.beginPath();
  286. ctx.moveTo(this.x, this.y);
  287. ctx.rect(this.x, this.y, this.width, this.height)
  288. ctx.closePath();
  289. ctx.strokeStyle = this.color;
  290. ctx.fillStyle = this.color;
  291. ctx.lineWidth = this.lineWidth;
  292. if (selected){
  293. ctx.lineWidth = 5
  294. ctx.stroke();
  295. }
  296. ctx.fill();
  297. }
  298. in(x, y) {
  299. return (
  300. ((this.width > 0) ? (this.x <= x && x <= (this.x + this.width)) : (this.x + this.width <= x && x <= this.x)) &&
  301. ( (this.height>0)? (this.y<= y && y <= (this.y + this.height)):( this.y+this.height<= y && y <= this.y))
  302. )
  303. }
  304. inBounds(x, y, w, h) {
  305. let areaX = false;
  306. let areaY = false;
  307. if (this.width > 0) { areaX=(w > 0) ? (this.x >= x && this.x+ this.width <= x + w):(this.x >= x + w && this.x + this.width <= x)}
  308. else { areaX= ((w > 0) ? (this.x+ this.width >= x && this.x <= x + w):(this.x + this.width>= x + w && this.x <= x)) }
  309. if (this.height > 0) { areaY=(h > 0) ? (this.y >= y && this.y + this.height <= y + h):(this.y>= y + h && this.y + this.height <= y)}
  310. else { areaY= (h > 0) ? (this.y+ this.height >= y && this.y <= y + h):(this.y + this.height>= y + h && this.y <= y) }
  311. return areaX && areaY;
  312. }
  313. }
  314. class BoundsRectangle extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  315. constructor(x, y, width, height, color, lineWidth) {
  316. super()
  317. this.x = x;
  318. this.y = y;
  319. this.width = width;
  320. this.height = height;
  321. this.color = 'grey';
  322. this.lineWidth = 2;
  323. this.draw();
  324. }
  325. draw(){
  326. ctx.beginPath();
  327. ctx.moveTo(this.x, this.y);
  328. ctx.rect(this.x, this.y, this.width, this.height)
  329. ctx.closePath();
  330. ctx.strokeStyle = this.color;
  331. ctx.lineWidth = this.lineWidth;
  332. ctx.stroke();
  333. }
  334. }
  335. color.onchange = () => {
  336. selection.forEach(c => c.color = color.value)
  337. Drawable.drawAll(selection)
  338. }
  339. document.getElementById('delete').onclick = () =>{
  340. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  341. selection = []
  342. Drawable.drawAll()
  343. }
  344. //new Line(0,0,100,100, "red")
  345. ////new Circle(30,30,10, "red")
  346. ////canvas.onmousemove = function(e){
  347. ////}
  348. //undo.onclick = function(){
  349. //Drawable.instances.pop()
  350. ////Drawable.instances = []
  351. //Drawable.drawAll()
  352. //}