index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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(){
  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. ctx.strokeStyle = this.color;
  220. ctx.lineWidth = this.lineWidth
  221. ctx.stroke();
  222. }
  223. }
  224. class Rectangle extends Drawable {
  225. constructor(x, y, width, height, color) {
  226. super()
  227. this.x = x;
  228. this.y = y;
  229. this.width = width;
  230. this.height = height;
  231. this.color = color;;
  232. }
  233. draw(selected) {
  234. ctx.beginPath();
  235. ctx.rect(this.x, this.y, this.width, this.height);
  236. ctx.closePath();
  237. ctx.fillStyle = this.color;
  238. if (selected) {
  239. ctx.lineWidth = 5
  240. ctx.strokeStyle = 'yellow'
  241. ctx.stroke();
  242. }
  243. ctx.fill();
  244. }
  245. in(x,y){
  246. return x > this.x && y > this.y && x < this.x + this.width && y < this.y + this.height
  247. }
  248. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  249. return this.x >= x && this.x + this.width <= x + w &&
  250. this.y >= y && this.y + this.height <= y + h
  251. }
  252. }
  253. class RectangleForSelect extends Rectangle {
  254. constructor(x, y, width, height) {
  255. super(x, y, width, height)
  256. }
  257. draw() {
  258. ctx.beginPath();
  259. ctx.rect(this.x, this.y, this.width, this.height);
  260. ctx.closePath();
  261. ctx.strokeStyle = 'black'
  262. ctx.lineWidth = '2'
  263. ctx.stroke();
  264. }
  265. }
  266. class Ellipse extends Drawable {
  267. constructor(x, y, radiusX, radiusY, rotation, startAngle, endAngle, color){
  268. super()
  269. this.x = x;
  270. this.y = y;
  271. this.radiusX = radiusX
  272. this.radiusY = radiusY
  273. this.rotation = rotation;
  274. this.startAngle = startAngle
  275. this.endAngle = endAngle
  276. this.color = color;
  277. this.draw();
  278. }
  279. draw(selected){
  280. ctx.beginPath();
  281. ctx.ellipse(this.x, this.y, this.radiusX, this.radiusY, this.rotation, this.startAngle, this.endAngle);
  282. ctx.closePath();
  283. ctx.fillStyle = this.color;
  284. if (selected){
  285. ctx.lineWidth = 5
  286. ctx.strokeStyle = 'yellow'
  287. ctx.stroke();
  288. }
  289. ctx.fill();
  290. }
  291. in(x,y){
  292. return (Math.abs(y - this.y) < this.radiusX && Math.abs(x - this.x) < this.radiusY)
  293. }
  294. inBounds(x,y,w,h){ // x = 100, this.x = 102, w = 5
  295. return this.x >= x && this.x <= x + w &&
  296. this.y >= y && this.y <= y + h
  297. }
  298. }
  299. color.onchange = () => {
  300. selection.forEach(c => c.color = color.value)
  301. Drawable.drawAll(selection)
  302. }
  303. document.getElementById('delete').onclick = () =>{
  304. Drawable.instances = Drawable.instances.filter(item => !selection.includes(item))
  305. selection = []
  306. Drawable.drawAll()
  307. }
  308. undo.onclick = function(){
  309. Drawable.instances.pop()
  310. Drawable.drawAll()
  311. }