script.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // ООП в функциональном стиле
  2. // Оценочное время выподнения 8 ч
  3. // Реальное время 6+ ч
  4. var $canv = $('<canvas id="canvas" width="1024px" height="1024px"></canvas>');
  5. $($canv).css({
  6. display: 'flex',
  7. 'justify-content': 'center',
  8. margin: '0 auto'
  9. }).prependTo('body');
  10. function Figure(x, y, color) {
  11. this.x = x;
  12. this.y = x;
  13. this.color = color;
  14. }
  15. function Line(x, y, x2, y2, color) {
  16. Figure.call(this);
  17. this.x = x;
  18. this.y = y;
  19. this.x2 = x2;
  20. this.y2 = y2;
  21. this.color = color;
  22. this.drow = function() {
  23. var line = canvas.getContext("2d");
  24. line.beginPath();
  25. line.moveTo(this.x, this.y);
  26. line.lineTo(this.x2, this.y2);
  27. line.strokeStyle = this.color;
  28. line.stroke();
  29. }
  30. }
  31. function Rect(x, y, width, height, color) {
  32. Figure.call(this);
  33. this.x = x;
  34. this.y = y;
  35. this.width = width;
  36. this.height = height;
  37. this.color = color;
  38. this.drow = function() {
  39. var rect = canvas.getContext('2d');
  40. rect.beginPath();
  41. rect.rect(this.x, this.y, this.width, this.height);
  42. rect.strokeStyle = this.color;
  43. rect.stroke();
  44. }
  45. }
  46. function Circle(x, y, r, color) {
  47. Figure.call(this);
  48. this.x = x;
  49. this.y = y;
  50. this.r = r;
  51. this.color = color;
  52. this.drow = function() {
  53. var cirle = canvas.getContext('2d');
  54. var centerX = this.x;
  55. var centerY = this.y;
  56. var radius = this.r;
  57. cirle.beginPath();
  58. cirle.arc(centerX, centerY, radius, 0, 2 * Math.PI);
  59. cirle.strokeStyle = this.color;
  60. cirle.stroke();
  61. }
  62. }
  63. var line = new Line(21, 43, 210, 250, 'red');
  64. var rect = new Rect(260, 130, 60, 120, 'green');
  65. var circle = new Circle(190, 120, 50, 'blue');
  66. function Canvac() {
  67. this.add = function(){
  68. for (var i = 0; i < arguments.length; i++) {
  69. var canvas = document.getElementById('canvas');
  70. arguments[i].drow();
  71. }
  72. }
  73. }
  74. var drowArea = new Canvac();
  75. drowArea.add(line, circle);
  76. drowArea.add(rect);