index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. var canvas = document.getElementById('canvas')
  2. var ctx = canvas.getContext('2d')
  3. var width = canvas.width;
  4. var height = canvas.height;
  5. function Drawable(){
  6. Drawable.prototype.addInstance(this);
  7. }
  8. Drawable.prototype.draw = function(){};
  9. Drawable.prototype.instances = [];
  10. Drawable.prototype.selected = [];
  11. Drawable.prototype.addInstance = function(item){
  12. Drawable.prototype.instances.push(item);
  13. }
  14. Drawable.prototype.drawAll = function(){
  15. ctx.clearRect(0,0,width,height);
  16. for(var i = 0; i<Drawable.prototype.instances.length;i++){
  17. Drawable.prototype.instances[i].draw(true);
  18. }
  19. }
  20. Drawable.prototype.forAll = function(callback){
  21. for(var i = 0; i<Drawable.prototype.instances.length;i++){
  22. callback(Drawable.prototype.instances[i])
  23. }
  24. }
  25. Drawable.prototype.forSelected = function(callback){
  26. for(var i = 0; i<Drawable.prototype.selected.length;i++){
  27. callback(Drawable.prototype.selected[i])
  28. }
  29. }
  30. function Circle(x,y,radius,color, name){
  31. Drawable.apply(this);
  32. this.coords = {x: x || 0, y: y || 0}
  33. this.radius = radius || 10;
  34. this.color = color || "red";
  35. this.name = name;
  36. }
  37. Circle.prototype = Object.create(Drawable.prototype);
  38. Circle.prototype.constructor = Circle;
  39. Circle.prototype.draw = function(){
  40. console.log('DRAW');
  41. ctx.beginPath();
  42. ctx.arc(this.coords.x, this.coords.y, this.radius, 0, 2 * Math.PI, false);
  43. ctx.fillStyle = this.color;
  44. ctx.closePath()
  45. ctx.fill();
  46. }
  47. Circle.prototype.coords = {x: null, y: null};
  48. Circle.prototype.radius = null;
  49. Circle.prototype.color = "black";
  50. function Line(x1,y1,x2,y2,color,lineWidth){
  51. Drawable.apply(this);
  52. this.x1 = x1;
  53. this.y1 = y1;
  54. this.x2 = x2;
  55. this.y2 = y2;
  56. this.lineWidth = lineWidth;
  57. this.color = color || "red";
  58. }
  59. Line.prototype = Object.create(Drawable.prototype);
  60. Line.prototype.constructor = Line;
  61. Line.prototype.draw = function(isShadow){
  62. ctx.beginPath()
  63. ctx.moveTo(this.x1,this.y1);
  64. ctx.lineTo(this.x2,this.y2);
  65. ctx.strokeStyle= this.color;
  66. ctx.lineWidth = this.lineWidth;
  67. ctx.stroke();
  68. ctx.closePath()
  69. }
  70. function Rectangle(x1,y1,x2,y2,color,lineWidth){
  71. Drawable.apply(this)
  72. this.x1 = x1;
  73. this.y1 = y1;
  74. this.x2 = x2;
  75. this.y2 = y2;
  76. this.color = color;
  77. this.lineWidth = lineWidth;
  78. }
  79. Rectangle.prototype = Object.create(Drawable.prototype);
  80. Rectangle.prototype.constructor = Rectangle;
  81. Rectangle.prototype.draw = function(){
  82. ctx.beginPath();
  83. ctx.rect(this.x1,this.y1,this.x2,this.y2);
  84. ctx.fillStyle = this.color;
  85. ctx.fill();
  86. ctx.closePath();
  87. }
  88. //ctx.moveTo(0,0);
  89. //ctx.lineTo(width, height);
  90. //ctx.strokeStyle="#FF0000";
  91. //ctx.stroke();
  92. //ctx.closePath()
  93. //
  94. var lineStartCoords = {x: null, y: null};
  95. function draw(evt){
  96. if(event.ctrlKey){
  97. return;
  98. }
  99. var tool = document.getElementById('tool').value;
  100. var obj;
  101. if (tool == 'line'){
  102. if (evt.type == 'mousedown'){
  103. lineStartCoords = {x: evt.clientX, y: evt.clientY};
  104. }
  105. if (evt.type == 'mouseup'){
  106. obj = new Line(lineStartCoords.x, lineStartCoords.y, evt.clientX, evt.clientY, document.getElementById('color').value, document.getElementById("size").value)
  107. }
  108. if(evt.type == 'mousemove'){
  109. obj = new Line(lineStartCoords.x, lineStartCoords.y, evt.clientX, evt.clientY, document.getElementById('color').value, document.getElementById("size").value)
  110. Line.prototype.instances.splice(-1,1);
  111. Line.prototype.drawAll();
  112. }
  113. }
  114. else if (tool == 'circle'){
  115. obj = new Circle(evt.clientX,evt.clientY,document.getElementById("size").value,document.getElementById('color').value, "circle")
  116. }
  117. else if (tool == 'rectangle'){
  118. if (evt.type == 'mousedown'){
  119. lineStartCoords = {x: evt.clientX, y: evt.clientY};
  120. }
  121. if (evt.type == 'mouseup'){
  122. obj = new Rectangle(lineStartCoords.x, lineStartCoords.y, evt.clientX - lineStartCoords.x, evt.clientY - lineStartCoords.y, document.getElementById('color').value, document.getElementById("size").value)
  123. }
  124. if(evt.type == 'mousemove'){
  125. obj = new Rectangle(lineStartCoords.x, lineStartCoords.y, evt.clientX - lineStartCoords.x, evt.clientY - lineStartCoords.y, document.getElementById('color').value, document.getElementById("size").value)
  126. Line.prototype.instances.splice(-1,1)
  127. Drawable.prototype.drawAll();
  128. }
  129. }
  130. obj.draw()
  131. }
  132. canvas.onmousedown = draw
  133. canvas.onmousemove = function(evt){
  134. var tool = document.getElementById('tool').value;
  135. if (evt.buttons & 1){
  136. draw(evt);
  137. }
  138. }
  139. canvas.onmouseup = draw
  140. //document.getElementById('color').onchange = function(){
  141. //Drawable.prototype.forAll(function(item){
  142. //item.color = document.getElementById('color').value;
  143. //})
  144. //Drawable.prototype.drawAll();
  145. //}
  146. var undo = document.getElementById("undo");
  147. undo.onclick = function(){
  148. Drawable.prototype.instances.splice(-1,1)
  149. Drawable.prototype.drawAll();
  150. console.log(Drawable.prototype.instances)
  151. }
  152. function hexToRgbA(hex){
  153. var c;
  154. if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
  155. c= hex.substring(1).split('');
  156. if(c.length== 3){
  157. c= [c[0], c[0], c[1], c[1], c[2], c[2]];
  158. }
  159. c= '0x'+c.join('');
  160. return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',0.3)';
  161. }
  162. throw new Error('Bad Hex');
  163. }
  164. canvas.onclick = function(event){
  165. if(!event.ctrlKey){
  166. return;
  167. }
  168. Drawable.prototype.forAll(function(obj){
  169. var x = event.clientX;
  170. var y = event.clientY;
  171. if(obj.name === "circle"){
  172. if(obj.coords.x - obj.radius < x && x < obj.coords.x + obj.radius && obj.coords.y - obj.radius < y && y < obj.coords.y + obj.radius)
  173. {
  174. obj.color = "red";
  175. Drawable.prototype.selected.push(obj);
  176. Drawable.prototype.drawAll();
  177. }
  178. }
  179. if((obj.x1 < x && x < obj.x2 + obj.x1 && obj.y1 < y && y < obj.y2 + obj.y1) || (obj.x1 < x && x < obj.x2 && obj.y1 < y && y < obj.y2)){
  180. obj.color = "red";
  181. Drawable.prototype.selected.push(obj);
  182. Drawable.prototype.drawAll();
  183. }
  184. });
  185. }
  186. document.getElementById('color').onchange = function(){
  187. Drawable.prototype.forSelected(function(obj){
  188. obj.color = document.getElementById('color').value;
  189. Drawable.prototype.drawAll();
  190. })
  191. }
  192. document.getElementById("size").onchange = function(){
  193. Drawable.prototype.forSelected(function(obj){
  194. obj.lineWidth = document.getElementById('size').value;
  195. obj.radius = document.getElementById('size').value;
  196. Drawable.prototype.drawAll();
  197. })
  198. }
  199. document.getElementById("delete").onclick = function(event){
  200. for (var i = 0; i < Drawable.prototype.instances.length; i++) {
  201. if(Drawable.prototype.instances[i].color === "red")
  202. delete Drawable.prototype.instances[i];
  203. Drawable.prototype.drawAll()
  204. }
  205. console.log(Drawable.prototype.instances)
  206. console.log(Drawable.prototype.selected)
  207. }