index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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)
  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){
  160. super()
  161. this.x = x;
  162. this.y = y;
  163. this.radius = radius;
  164. this.color = color;
  165. this.draw();
  166. }
  167. draw(selected){
  168. ctx.beginPath();
  169. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  170. ctx.closePath();
  171. ctx.fillStyle = this.color;
  172. if (selected){
  173. ctx.lineWidth = 2
  174. ctx.stroke();
  175. }
  176. ctx.fill();
  177. }
  178. in(x,y){
  179. return this.distanceTo(x,y) < this.radius
  180. }
  181. inBounds(x, y, w, h) { // x = 100, this.x = 102, w = 5
  182. 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)) &&
  183. ((h>0)?(this.y-this.radius >= y && this.y+this.radius <= y + h ):(this.y-this.radius >= y+h && this.y+this.radius <= y ))
  184. }
  185. }
  186. class Ellipse extends Drawable {
  187. //__proto__: Drawable.prototype
  188. constructor(x,y, width, height, color, lineWidth){
  189. super()
  190. this.x = x;
  191. this.y = y;
  192. this.width = width;
  193. this.height = height;
  194. this.color = color;
  195. this.lineWidth = lineWidth;
  196. this.draw();
  197. }
  198. draw(selected){
  199. ctx.beginPath();
  200. ctx.moveTo(this.x, this.y);
  201. ctx.ellipse(this.x, this.y, ((this.width**2)**0.5), ((this.height**2)**0.5), 0, 0, Math.PI * 2)
  202. ctx.closePath();
  203. ctx.strokeStyle = this.color;
  204. ctx.fillStyle = this.color;
  205. ctx.lineWidth = this.lineWidth;
  206. if (selected){
  207. ctx.lineWidth = 2
  208. ctx.stroke();
  209. }
  210. ctx.fill();
  211. }
  212. in (x, y){
  213. return (
  214. ((((x - this.x) ** 2) / ((this.width ) ** 2)) + (((y - this.y) ** 2 )/( (this.height) ** 2))) <= 1
  215. )
  216. }
  217. inBounds(x, y, w, h) {
  218. 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)) &&
  219. ((h>0)?(this.y-this.height >= y && this.y+this.height <= y + h ):(this.y-this.height >= y+h && this.y+this.height <= y ))
  220. }
  221. }
  222. class Line extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  223. constructor(x,y, width, height, color, lineWidth){
  224. super()
  225. this.x = x;
  226. this.y = y;
  227. this.width = width;
  228. this.height = height;
  229. this.color = color;
  230. this.lineWidth = lineWidth;
  231. this.draw();
  232. }
  233. draw(){
  234. ctx.beginPath();
  235. ctx.moveTo(this.x, this.y);
  236. ctx.lineTo(this.x + this.width, this.y + this.height);
  237. ctx.closePath();
  238. ctx.strokeStyle = this.color;
  239. ctx.lineWidth = this.lineWidth
  240. ctx.stroke();
  241. }
  242. in(x, y) {
  243. const deltaAngle = Math.atan2(y - this.y, x - this.x) - Math.atan2(this.height, this.width) ;
  244. const newX = Math.cos(deltaAngle) * this.distanceTo(x, y);
  245. const newY = Math.sin(deltaAngle) * this.distanceTo(x, y);
  246. return ((newX>=0)&&(newX<=distance(0,0, this.width, this.height))&&(newY>=-this.lineWidth/2)&&(newY<=this.lineWidth/2)
  247. )
  248. }
  249. inBounds(x, y, w, h) {
  250. let areaX = false;
  251. let areaY = false;
  252. const angleX = Math.atan2(this.height, this.width);
  253. const deltaX = Math.sin(angleX) * this.lineWidth/2;
  254. const deltaY = Math.cos(angleX) * this.lineWidth/2;
  255. 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)}
  256. 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)) }
  257. 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)}
  258. 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) }
  259. return areaX && areaY;
  260. }
  261. }
  262. color.onchange = () => {
  263. selection.forEach(c => c.color = color.value)
  264. Drawable.drawAll(selection)
  265. }
  266. document.getElementById('delete').onclick = () =>{
  267. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  268. selection = []
  269. Drawable.drawAll()
  270. }
  271. class Rectangle extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  272. constructor(x, y, width, height, color, lineWidth) {
  273. super()
  274. this.x = x;
  275. this.y = y;
  276. this.width = width;
  277. this.height = height;
  278. this.color = color;
  279. this.lineWidth = lineWidth;
  280. this.draw();
  281. }
  282. draw(selected){
  283. ctx.beginPath();
  284. ctx.moveTo(this.x, this.y);
  285. ctx.rect(this.x, this.y, this.width, this.height)
  286. ctx.closePath();
  287. ctx.strokeStyle = this.color;
  288. ctx.fillStyle = this.color;
  289. ctx.lineWidth = this.lineWidth;
  290. if (selected){
  291. ctx.lineWidth = 5
  292. ctx.stroke();
  293. }
  294. ctx.fill();
  295. }
  296. in(x, y) {
  297. return (
  298. ((this.width > 0) ? (this.x <= x && x <= (this.x + this.width)) : (this.x + this.width <= x && x <= this.x)) &&
  299. ( (this.height>0)? (this.y<= y && y <= (this.y + this.height)):( this.y+this.height<= y && y <= this.y))
  300. )
  301. }
  302. inBounds(x, y, w, h) {
  303. let areaX = false;
  304. let areaY = false;
  305. 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)}
  306. else { areaX= ((w > 0) ? (this.x+ this.width >= x && this.x <= x + w):(this.x + this.width>= x + w && this.x <= x)) }
  307. 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)}
  308. else { areaY= (h > 0) ? (this.y+ this.height >= y && this.y <= y + h):(this.y + this.height>= y + h && this.y <= y) }
  309. return areaX && areaY;
  310. }
  311. }
  312. class BoundsRectangle extends Drawable { //смочь скопировать в Rectangle и поменять отприсовку
  313. constructor(x, y, width, height, color, lineWidth) {
  314. super()
  315. this.x = x;
  316. this.y = y;
  317. this.width = width;
  318. this.height = height;
  319. this.color = 'grey';
  320. this.lineWidth = 2;
  321. this.draw();
  322. }
  323. draw(){
  324. ctx.beginPath();
  325. ctx.moveTo(this.x, this.y);
  326. ctx.rect(this.x, this.y, this.width, this.height)
  327. ctx.closePath();
  328. ctx.strokeStyle = this.color;
  329. ctx.lineWidth = this.lineWidth;
  330. ctx.stroke();
  331. }
  332. }
  333. color.onchange = () => {
  334. selection.forEach(c => c.color = color.value)
  335. Drawable.drawAll(selection)
  336. }
  337. document.getElementById('delete').onclick = () =>{
  338. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  339. selection = []
  340. Drawable.drawAll()
  341. }
  342. //new Line(0,0,100,100, "red")
  343. ////new Circle(30,30,10, "red")
  344. ////canvas.onmousemove = function(e){
  345. ////}
  346. //undo.onclick = function(){
  347. //Drawable.instances.pop()
  348. ////Drawable.instances = []
  349. //Drawable.drawAll()
  350. //}