canvasLast.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <meta name="viewport" content="width=device-width">
  7. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.3/papaparse.min.js"></script>
  9. <script src="http://gitlab.a-level.com.ua/gitgod/nanobind/raw/master/static/nb.js"></script>
  10. </head>
  11. <body>
  12. <canvas id='canvas' width=400 height=400></canvas>
  13. <button id='undo'>UNDO</button>
  14. <input type='color' id='color'>
  15. <select id='tool'>
  16. <option value='graffity'>Graffity</option>
  17. <option value='circle'>Circle</option>
  18. <option value='line'>Line</option>
  19. <option value='select'>Select</option>
  20. <option value='rectangle'>Rectangle</option>
  21. <option value='ellipse'>Ellipse</option>
  22. </select>
  23. <input type='number' id='size' value=10>
  24. <button id='delete'>Delete...</button>
  25. <!-- <canvas id="canv" width="400" height="400"></canvas>
  26. <input type="submit" value="Click" id="btn">
  27. <input type="text" placeholder="color" id="color">
  28. <input type="submit" value="Delete" id="del"> -->
  29. <script>
  30. /*let canv=document.getElementById('canv');
  31. let ctx=canv.getContext('2d');
  32. del.onclick=()=>{
  33. ctx.fillStyle='white';
  34. ctx.fill();
  35. }
  36. btn.onclick=()=>{
  37. ctx.beginPath();
  38. ctx.arc(50, 50, 50, -Math.PI/6, Math.PI/6,true);
  39. ctx.lineTo(50, 50)
  40. ctx.closePath()
  41. ctx.fillStyle=color.value;
  42. ctx.fill();
  43. }*/
  44. const canvas = document.getElementById('canvas')
  45. const ctx = canvas.getContext('2d')
  46. const width = canvas.width;
  47. const height = canvas.height;
  48. let current;
  49. let selection = []
  50. const tools = {
  51. graffity: {
  52. mousemove(e){
  53. (e.buttons & 1) && new Circle(e.clientX, e.clientY, +size.value, color.value)
  54. }
  55. },
  56. circle: {
  57. mousedown(e){
  58. current = new Circle(e.clientX, e.clientY, 1, color.value)
  59. },
  60. mousemove(e){
  61. if (!current) return;
  62. current.radius = current.distanceTo(e.clientX, e.clientY)
  63. Drawable.drawAll()
  64. },
  65. mouseup(e){
  66. current = null
  67. }
  68. },
  69. line: {
  70. mousedown(e){
  71. current = new Line(e.clientX, e.clientY, 0, 0, color.value, +size.value)
  72. },
  73. mousemove(e){
  74. if (!current) return;
  75. current.width = e.clientX - current.x
  76. current.height = e.clientY - current.y
  77. Drawable.drawAll()
  78. },
  79. mouseup(e){
  80. current = null
  81. }
  82. },
  83. select: {
  84. click(e){
  85. console.log(e)
  86. let found = Drawable.instances.filter(c => c.in && c.in(e.clientX, e.clientY))
  87. if (found.length){
  88. if (e.altKey){
  89. selection.push(found.pop())
  90. }
  91. else {
  92. selection = [found.pop()]
  93. }
  94. }
  95. else {
  96. if (!e.altKey) selection = []
  97. }
  98. Drawable.drawAll(selection)
  99. },
  100. mousedown(e){},
  101. mousemove(e){},
  102. mouseup(e){},
  103. },
  104. rectangle: {
  105. mousedown(e){
  106. current = new Rectangle(e.clientX, e.clientY, 0, 0, color.value)
  107. },
  108. mousemove(e){
  109. if (!current) return;
  110. current.width = e.clientX - current.x
  111. current.height = e.clientY - current.y
  112. Drawable.drawAll()
  113. },
  114. mouseup(e){
  115. current = null
  116. }
  117. },
  118. ellipse: {
  119. mousedown(e){
  120. current = new Elipse(e.clientX,e.clientY, 1,1, color.value)
  121. },
  122. mousemove(e){
  123. if (!current) return;
  124. var width = e.clientX - current.x;
  125. var height = e.clientY - current.y;
  126. current.width = width ? width : width * -1;
  127. current.height = height ? height : height * -1;
  128. Drawable.drawAll()
  129. },
  130. mouseup(e){
  131. current = null
  132. }
  133. }
  134. }
  135. function superHandler(evt){
  136. let t = tools[tool.value]
  137. if (typeof t[evt.type] === 'function')
  138. t[evt.type].call(this, evt)
  139. }
  140. canvas.onmousemove = superHandler
  141. canvas.onmouseup = superHandler
  142. canvas.onmousedown = superHandler
  143. canvas.onclick = superHandler
  144. function Drawable(){
  145. Drawable.addInstance(this);
  146. }
  147. const distance = (x1, y1, x2, y2) => ((x1-x2)**2 + (y1-y2)**2)**0.5
  148. Drawable.prototype.draw = function(){};
  149. Drawable.prototype.distanceTo = function(x,y){
  150. if (typeof this.x !== 'number' ||
  151. typeof this.y !== 'number'){
  152. return NaN
  153. }
  154. return distance(this.x, this.y, x, y)
  155. };
  156. Drawable.instances = [];
  157. Drawable.addInstance = function(item){
  158. Drawable.instances.push(item);
  159. }
  160. Drawable.drawAll = function(selection=[]){
  161. ctx.clearRect(0,0,width,height);
  162. Drawable.forAll(item => item.draw())
  163. selection.forEach(item => item.draw(true))
  164. }
  165. Drawable.forAll = function(callback){
  166. for(var i = 0; i<Drawable.instances.length;i++){
  167. callback(Drawable.instances[i])
  168. }
  169. }
  170. class Circle extends Drawable {
  171. constructor(x,y,radius, color){
  172. super()
  173. this.x = x
  174. this.y = y
  175. this.radius = radius
  176. this.color = color
  177. this.draw()
  178. }
  179. draw(selected){
  180. ctx.beginPath()
  181. ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
  182. ctx.closePath()
  183. ctx.fillStyle = this.color
  184. if (selected){
  185. ctx.lineWidth = 2
  186. ctx.stroke()
  187. }
  188. ctx.fill()
  189. }
  190. in(x, y){
  191. return this.distanceTo(x, y) < this.radius
  192. }
  193. inBounds(x, y, w, h){
  194. return this.x >= x && this.x <= x + w && this.y >= y && this.y <= y + h
  195. }
  196. }
  197. class Line extends Drawable {
  198. constructor(x,y, width, height, color, lineWidth){
  199. super()
  200. this.x = x
  201. this.y = y
  202. this.width = width
  203. this.height = height
  204. this.color = color
  205. this.lineWidth = lineWidth
  206. this.draw()
  207. }
  208. draw(selected){
  209. ctx.beginPath()
  210. ctx.moveTo(this.x, this.y)
  211. ctx.lineTo(this.x + this.width, this.y + this.height)
  212. ctx.closePath()
  213. ctx.strokeStyle = this.color
  214. ctx.lineWidth = this.lineWidth
  215. if (selected){
  216. ctx.strokeStyle = '#f0f'
  217. ctx.stroke()
  218. }
  219. ctx.stroke()
  220. }
  221. in(x, y) {
  222. let rotateLine = Math.atan2(this.height, this.width)
  223. let rotCursor = Math.atan2(y - this.y, x - this.x)
  224. let distanceToPoint = distance(x, y, this.x, this.y)
  225. let angleLine = rotCursor - rotateLine
  226. let rotateX = Math.cos(angleLine) * distanceToPoint
  227. let rotateY = Math.sin(angleLine) * distanceToPoint
  228. return rotateX >= 0 && rotateX <= this.length && rotateY <= this.lineWidth/2 && rotateY >= -this.lineWidth/2
  229. }
  230. get length(){
  231. return this.distanceTo(this.x + this.width, this.y + this.height)
  232. }
  233. }
  234. //------------------------------------------------------------------
  235. class Rectangle extends Drawable{
  236. constructor(x, y, width, height, color){
  237. super()
  238. this.x = x
  239. this.y = y
  240. this.width = width
  241. this.height = height
  242. this.color = color
  243. this.draw()
  244. }
  245. draw(selected){
  246. ctx.beginPath()
  247. ctx.moveTo(this.x, this.y)
  248. ctx.fillRect(this.x, this.y, this.width, this.height)
  249. if (selected){
  250. ctx.rect(this.x, this.y, this.width, this.height)
  251. ctx.strokeStyle = '#f0f'
  252. ctx.stroke()
  253. }
  254. ctx.fillStyle = this.color
  255. ctx.stroke()
  256. }
  257. in(x, y){
  258. return x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height
  259. }
  260. }
  261. //--------------------------------------------------------------
  262. class Elipse extends Drawable {
  263. constructor(x,y,width,height,color){
  264. super()
  265. this.x = x ? x : x*-1;
  266. this.y = y ? y : y*-1;
  267. this.width = width;
  268. this.height = height;
  269. this.color = color;
  270. this.draw();
  271. }
  272. draw(){
  273. this.width = this.width > 0 ? this.width : this.width * (-1);
  274. this.height = this.height > 0 ? this.height : this.height * (-1);
  275. ctx.beginPath();
  276. ctx.moveTo(this.x, this.y);
  277. ctx.ellipse(this.x, this.y, this.height, this.width, Math.PI / 2, 0, 2 * Math.PI);
  278. ctx.closePath();
  279. ctx.fillStyle = this.color;
  280. ctx.fill();
  281. }
  282. in(x,y){
  283. return this.distanceTo(x,y) < this.radius
  284. }
  285. inBounds(x,y,w,h){
  286. return this.x >= x && this.x <= x + w &&
  287. this.y >= y && this.y <= y + h
  288. }
  289. }
  290. //-------------------------------------------------------------------
  291. color.onchange = () => {
  292. selection.forEach(c => c.color = color.value)
  293. Drawable.drawAll(selection)
  294. }
  295. document.getElementById('delete').onclick = () =>{
  296. Drawable.instances = []
  297. Drawable.drawAll()
  298. }
  299. undo.onclick = function(){
  300. Drawable.instances.pop()
  301. Drawable.drawAll()
  302. }
  303. </script>
  304. </body>
  305. </html>