script.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. function Figure(x,y,color) {
  2. this.x = x;
  3. this.y = y;
  4. this.color = color;
  5. };
  6. function Circle(x,y,r,color) {
  7. Figure.call(this,x,y,color);
  8. this.r = r;
  9. };
  10. Circle.prototype.draw = function(par) {
  11. par.fillStyle = this.color;
  12. par.arc(this.x, this.y, this.r, 0, Math.PI*2, true);
  13. par.fill();
  14. };
  15. function Rect(x,y,w,h,color) {
  16. Figure.call(this,x,y,color);
  17. this.w = w;
  18. this.h = h;
  19. };
  20. Rect.prototype.draw = function(par) {
  21. par.fillStyle = this.color;
  22. par.fillRect(this.x, this.y, this.x+this.w, this.y+this.h);
  23. };
  24. function Line(x1,y1,x2,y2,color) {
  25. Figure.call(this,x1,y1,color);
  26. this.x2 = x2;
  27. this.y2 = y2;
  28. };
  29. Line.prototype.draw = function(par) {
  30. par.strokeStyle = this.color;
  31. par.moveTo(this.x,this.y);
  32. par.lineTo(this.x2,this.y2);
  33. par.stroke();
  34. };
  35. function Canvas(id) {
  36. this.add = function(figure) {
  37. var canvas = document.getElementById(id),
  38. ctx = canvas.getContext("2d");
  39. figure.draw(ctx);
  40. };
  41. };
  42. //------------------//
  43. var line = new Line(25, 75, 275, 75, 'red'); // x1, y1, x2, y2, color
  44. var circle = new Circle(120, 100, 50, 'green'); // x, y, r, color
  45. var rect = new Rect(100, 30, 50, 100, 'blue'); // x, y, w, h, color
  46. var drawArea = new Canvas('canvasLine');
  47. drawArea.add(line);
  48. var drawArea = new Canvas('canvasCircle');
  49. drawArea.add(circle);
  50. var drawArea = new Canvas('canvasRect');
  51. drawArea.add(rect);