index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. let xxx
  8. let yyy
  9. let selectWidth
  10. let selectHeight
  11. let cyka
  12. let blyat
  13. const tools = {
  14. graffity: {
  15. mousemove(e){ //e.buttons 0b00000x11 & 0b00000100 == x
  16. (e.buttons & 1) && new Circle(e.clientX, e.clientY, +size.value, color.value)
  17. }
  18. },
  19. circle: {
  20. mousedown(e){
  21. current = new Circle(e.clientX,e.clientY, 1, color.value)
  22. },
  23. mousemove(e){
  24. if (!current) return;
  25. current.radius = current.distanceTo(e.clientX, e.clientY)
  26. Drawable.drawAll()
  27. },
  28. mouseup(e){
  29. current = null
  30. }
  31. },
  32. ellipse: {
  33. mousedown(e) {
  34. current = new Ellipse(e.clientX, e.clientY, 5, 5, Math.PI / 2, 0, 2 * Math.PI, color.value);
  35. },
  36. mousemove(e) {
  37. if (!current) return;
  38. current.radiusX = Math.abs(e.clientY - current.y)
  39. current.radiusY = Math.abs(e.clientX - current.x)
  40. Drawable.drawAll();
  41. },
  42. mouseup(e) {
  43. current = null;
  44. },
  45. },
  46. line: {
  47. mousedown(e){
  48. current = new Line(e.clientX, e.clientY, 0, 0, color.value, +size.value)
  49. },
  50. mousemove(e){
  51. if (!current) return;
  52. current.width = e.clientX - current.x
  53. current.height = e.clientY - current.y
  54. Drawable.drawAll()
  55. },
  56. mouseup(e){
  57. current = null
  58. }
  59. },
  60. rect: {
  61. mousedown(e) {
  62. current = new Rectangle(e.clientX, e.clientY, 0, 0, color.value);
  63. },
  64. mousemove(e) {
  65. if (!current) return;
  66. current.width = e.clientX - current.x;
  67. current.height = e.clientY - current.y;
  68. Drawable.drawAll();
  69. },
  70. mouseup(e) {
  71. if (current.width < 0) {
  72. current.x = current.x + current.width
  73. current.width = Math.abs(current.width)
  74. }
  75. if (current.height < 0) {
  76. current.y = current.y + current.height
  77. current.height = Math.abs(current.height)
  78. }
  79. current = null;
  80. },
  81. },
  82. select: {
  83. click(e) {
  84. if (!blyat) {
  85. let found = Drawable.instances.filter(c => c.in && c.in(e.clientX, e.clientY))
  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. Drawable.drawAll(selection)
  98. }
  99. cyka = false
  100. blyat = false
  101. },
  102. mousedown(e) {
  103. xxx = e.clientX,
  104. yyy = e.clientY
  105. cyka = true
  106. current = new RectangleForSelect(xxx, yyy, selectWidth, selectHeight)
  107. },
  108. mousemove(e) {
  109. if (cyka && current) {
  110. blyat = true
  111. current.width = e.clientX - current.x;
  112. current.height = e.clientY - current.y;
  113. selectWidth = current.width
  114. selectHeight = current.height
  115. Drawable.drawAll(selection)
  116. }
  117. },
  118. mouseup(e) {
  119. if (current.width < 0) {
  120. xxx += current.width
  121. selectWidth = Math.abs(current.width)
  122. }
  123. if (current.height < 0) {
  124. yyy += current.height
  125. selectHeight = Math.abs(current.height)
  126. }
  127. Drawable.instances.pop()
  128. current = null
  129. let found = Drawable.instances.filter(c => c.inBounds && c.inBounds(xxx, yyy, selectWidth, selectHeight))
  130. if (found.length) {
  131. selection.push(...found)
  132. } else {
  133. selection = [...selection]
  134. }
  135. Drawable.drawAll(selection)
  136. },
  137. }
  138. }
  139. function superHandler(evt){
  140. let t = tools[tool.value]
  141. if (typeof t[evt.type] === 'function')
  142. t[evt.type].call(this, evt)
  143. }
  144. canvas.onmousemove = superHandler
  145. canvas.onmouseup = superHandler
  146. canvas.onmousedown = superHandler
  147. canvas.onclick = superHandler
  148. function Drawable(){
  149. Drawable.addInstance(this);
  150. }
  151. const distance = (x1,y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  152. Drawable.prototype.draw = function(){};
  153. Drawable.prototype.distanceTo = function(x,y){
  154. if (typeof this.x !== 'number' ||
  155. typeof this.y !== 'number'){
  156. return NaN
  157. }
  158. return distance(this.x, this.y, x, y)
  159. };
  160. Drawable.instances = [];
  161. Drawable.addInstance = function(item){
  162. Drawable.instances.push(item);
  163. }
  164. Drawable.drawAll = function(selection=[]){
  165. ctx.clearRect(0,0,width,height);
  166. Drawable.forAll(item => item.draw())
  167. selection.forEach(item => item.draw(true))
  168. }
  169. Drawable.forAll = function(callback){
  170. for(var i = 0; i<Drawable.instances.length;i++){
  171. callback(Drawable.instances[i])
  172. }
  173. }
  174. class Circle extends Drawable {
  175. constructor(x,y,radius, color){
  176. super()
  177. this.x = x;
  178. this.y = y;
  179. this.radius = radius;
  180. this.color = color;
  181. this.draw();
  182. }
  183. draw(selected){
  184. ctx.beginPath();
  185. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  186. ctx.closePath();
  187. ctx.fillStyle = this.color;
  188. if (selected){
  189. ctx.lineWidth = 5
  190. ctx.strokeStyle = 'yellow'
  191. ctx.stroke();
  192. }
  193. ctx.fill();
  194. }
  195. in(x,y){
  196. return this.distanceTo(x,y) < this.radius
  197. }
  198. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  199. return this.x >= x && this.x <= x + w &&
  200. this.y >= y && this.y <= y + h
  201. }
  202. }
  203. class Line extends Drawable {
  204. constructor(x, y, width, height, color, lineWidth){
  205. super()
  206. this.x = x;
  207. this.y = y;
  208. this.width = width;
  209. this.height = height;
  210. this.color = color;
  211. this.lineWidth = lineWidth;
  212. this.draw();
  213. }
  214. draw(selected){
  215. ctx.beginPath();
  216. ctx.moveTo(this.x, this.y);
  217. ctx.lineTo(this.x + this.width, this.y + this.height);
  218. ctx.closePath();
  219. if (selected){
  220. ctx.strokeStyle = 'yellow'
  221. ctx.stroke();
  222. } else {
  223. ctx.strokeStyle = this.color;
  224. ctx.lineWidth = this.lineWidth
  225. ctx.stroke();
  226. }
  227. }
  228. in(x, y) {
  229. let angle1 = Math.atan2(this.height, this.width)
  230. let angle2 = Math.atan2(y - this.y, x - this.x)
  231. let angle3 = angle2 - angle1
  232. let line = distance(this.x, this.y, x, y)
  233. let redWidth = Math.cos(angle3) * line
  234. let redHeight = Math.sin(angle3) * line
  235. return (redWidth < distance(this.x, this.y, this.x + this.width, this.y + this.height)) && (Math.abs(redHeight) < (this.lineWidth / 2))
  236. }
  237. }
  238. class Rectangle extends Drawable {
  239. constructor(x, y, width, height, color) {
  240. super()
  241. this.x = x;
  242. this.y = y;
  243. this.width = width;
  244. this.height = height;
  245. this.color = color;;
  246. }
  247. draw(selected) {
  248. ctx.beginPath();
  249. ctx.rect(this.x, this.y, this.width, this.height);
  250. ctx.closePath();
  251. ctx.fillStyle = this.color;
  252. if (selected) {
  253. ctx.lineWidth = 5
  254. ctx.strokeStyle = 'yellow'
  255. ctx.stroke();
  256. }
  257. ctx.fill();
  258. }
  259. in(x,y){
  260. return x > this.x && y > this.y && x < this.x + this.width && y < this.y + this.height
  261. }
  262. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  263. return this.x >= x && this.x + this.width <= x + w &&
  264. this.y >= y && this.y + this.height <= y + h
  265. }
  266. }
  267. class RectangleForSelect extends Rectangle {
  268. constructor(x, y, width, height) {
  269. super(x, y, width, height)
  270. }
  271. draw() {
  272. ctx.beginPath();
  273. ctx.rect(this.x, this.y, this.width, this.height);
  274. ctx.closePath();
  275. ctx.strokeStyle = 'black'
  276. ctx.lineWidth = '2'
  277. ctx.stroke();
  278. }
  279. }
  280. class Ellipse extends Drawable {
  281. constructor(x, y, radiusX, radiusY, rotation, startAngle, endAngle, color){
  282. super()
  283. this.x = x;
  284. this.y = y;
  285. this.radiusX = radiusX
  286. this.radiusY = radiusY
  287. this.rotation = rotation;
  288. this.startAngle = startAngle
  289. this.endAngle = endAngle
  290. this.color = color;
  291. this.draw();
  292. }
  293. draw(selected){
  294. ctx.beginPath();
  295. ctx.ellipse(this.x, this.y, this.radiusX, this.radiusY, this.rotation, this.startAngle, this.endAngle);
  296. ctx.closePath();
  297. ctx.fillStyle = this.color;
  298. if (selected){
  299. ctx.lineWidth = 5
  300. ctx.strokeStyle = 'yellow'
  301. ctx.stroke();
  302. }
  303. ctx.fill();
  304. }
  305. in(x,y){
  306. return (Math.abs(y - this.y) < this.radiusX && Math.abs(x - this.x) < this.radiusY)
  307. }
  308. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  309. return this.x >= x && this.x <= x + w &&
  310. this.y >= y && this.y <= y + h
  311. }
  312. }
  313. color.onchange = () => {
  314. selection.forEach(c => c.color = color.value)
  315. Drawable.drawAll(selection)
  316. }
  317. document.getElementById('delete').onclick = () =>{
  318. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  319. selection = []
  320. Drawable.drawAll()
  321. }
  322. undo.onclick = function(){
  323. Drawable.instances.pop()
  324. Drawable.drawAll()
  325. }