Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Module Shapes
- var Shapes = (function () {
- // Shape - base class, all other figures are inherited from base class Shape
- var Shape = (function () {
- function Shape(x, y, color) {
- this._x = x;
- this._y = y;
- this._color = color;
- }
- Shape.prototype = {
- serialize: function () {
- var serializedShape = {
- x: this._x,
- y: this._y,
- color: this.color
- };
- return serializedShape;
- },
- canvas: function () {
- var canvas = {
- element: document.getElementById("shapesContainer").getContext("2d")
- };
- return canvas;
- }
- }
- Shape.prototype.toString = function () {
- return this.serialize();
- }
- return Shape;
- }());
- // Circle
- var Circle = (function () {
- function validateRadius() {
- if (this._radius < 0) {
- return false;
- }
- return true;
- }
- function Circle(x, y, color, radius) {
- Shape.call(this, x, y, color);
- if (!validateRadius.call(this)) {
- throw new Error('Invalid radius, must be positive value!');
- }
- this._radius = radius;
- }
- Circle.prototype = new Shape();
- Circle.prototype.serialize = function () {
- var serializedCircle = Shape.prototype.serialize.call(this);
- serializedCircle.radius = this._radius;
- return serializedCircle;
- };
- Circle.prototype.draw = function () {
- this.canvas().element.beginPath();
- this.canvas().element.arc(this.serialize().x, this.serialize().y, this.serialize().radius, 0, 2 * Math.PI, false);
- this.canvas().element.fillStyle = this.serialize().color;
- this.canvas().element.fill();
- this.canvas().element.lineWidth = 5;
- this.canvas().element.strokeStyle = this.serialize().color;
- this.canvas().element.stroke();
- };
- Circle.prototype.toString = function () {
- return "Circle: " + JSON.stringify(this.serialize());
- };
- return Circle;
- }());
- // Point
- var Point = (function () {
- function Point(x, y, color) {
- Shape.call(this, x, y, color);
- }
- Point.prototype = new Shape();
- Point.prototype.serialize = function () {
- var serializedPoint = Shape.prototype.serialize.call(this);
- return serializedPoint;
- };
- Point.prototype.draw = function () {
- this.canvas().element.beginPath();
- this.canvas().element.fillStyle = this.serialize().color;
- this.canvas().element.fillRect(this.serialize().x, this.serialize().y, 3, 3);
- };
- Point.prototype.toString = function () {
- return "Point: " + JSON.stringify(this.serialize());
- };
- return Point;
- }());
- // Rectangle
- var Rectangle = (function () {
- var BORDERS = {
- LEFT: 0,
- RIGHT: 1000,
- TOP: 0,
- BOTTOM: 1000
- };
- function validatePosition() {
- if (this._x < BORDERS.LEFT || BORDERS.RIGHT < this._x ||
- this._y < BORDERS.TOP || BORDERS.BOTTOM < this._y) {
- return false;
- }
- return true;
- }
- function Rectangle(x, y, color, width, height) {
- Shape.call(this, x, y, color);
- if (!validatePosition.call(this)) {
- throw new Error('Invalid Rect position');
- }
- this._width = width;
- this._height = height;
- }
- Rectangle.prototype = new Shape();
- Rectangle.prototype.serialize = function () {
- var serializedRect = Shape.prototype.serialize.call(this);
- serializedRect.width = this._width;
- serializedRect.height = this._height;
- return serializedRect;
- };
- Rectangle.prototype.draw = function () {
- this.canvas().element.beginPath();
- this.canvas().element.fillStyle = this.serialize().color;
- this.canvas().element.fillRect(this.serialize().x,
- this.serialize().y, this.serialize().width, this.serialize().height);
- };
- Rectangle.prototype.toString = function () {
- return "Rectangle: " + JSON.stringify(this.serialize());
- };
- return Rectangle;
- }());
- // Triangle
- var Triangle = (function () {
- var SIDES = {
- a: Math.sqrt((this._x2 - this._x1) * (this._x2 - this._x1) + (this._y2 - this._y1) * (this._y2 - this._y1)),
- b: Math.sqrt((this._x3 - this._x2) * (this._x3 - this._x2) + (this._y3 - this._y2) * (this._y3 - this._y2)),
- c: Math.sqrt((this._x3 - this._x1) * (this._x3 - this._x1) + (this._y3 - this._y1) * (this._y3 - this._y1)),
- };
- function validateTriangle() {
- if (SIDES.a + SIDES.b > SIDES.c &&
- SIDES.b + SIDES.c > SIDES.a &&
- SIDES.a + SIDES.c > SIDES.b) {
- return true;
- }
- return false;
- }
- function Triangle(x, y, color, x2, y2, x3, y3) {
- Shape.call(this, x, y, color);
- if (!validateTriangle.call(this)) {
- throw new Error('Invalid Triangle');
- }
- this._x2 = x2;
- this._y2 = y2;
- this._x3 = x3;
- this._y3 = y3;
- }
- Triangle.prototype = new Shape();
- Triangle.prototype.serialize = function () {
- var serializedTriangle = Shape.prototype.serialize.call(this);
- serializedTriangle.x2 = this._x2;
- serializedTriangle.y2 = this._y2;
- serializedTriangle.x3 = this._x3;
- serializedTriangle.y3 = this._y3;
- return serializedTriangle;
- };
- Triangle.prototype.draw = function () {
- this.canvas().element.beginPath();
- this.canvas().element.fillStyle = this.serialize().color;
- this.canvas().element.moveTo(this.serialize().x, this.serialize().y);
- this.canvas().element.lineTo(this.serialize().x2 + this.serialize().x, this.serialize().y2 + this.serialize().y);
- this.canvas().element.lineTo(this.serialize().x3 + this.serialize().x, this.serialize().y3 + this.serialize().y);
- this.canvas().element.fill();
- };
- Triangle.prototype.toString = function () {
- return "Triangle: " + JSON.stringify(this.serialize());
- };
- return Triangle;
- }());
- // Segment
- var Segment = (function () {
- function Segment(x, y, color, x2, y2) {
- Shape.call(this, x, y, color);
- this._x2 = x2;
- this._y2 = y2;
- }
- Segment.prototype = new Shape();
- Segment.prototype.serialize = function () {
- var serializedSegment = Shape.prototype.serialize.call(this);
- serializedSegment.x2 = this._x2;
- serializedSegment.y2 = this._y2;
- return serializedSegment;
- };
- Segment.prototype.draw = function () {
- this.canvas().element.beginPath();
- this.canvas().element.moveTo(this.serialize().x, this.serialize().y);
- this.canvas().element.lineTo(this.serialize().x2 + this.serialize().x, this.serialize().y2 + this.serialize().y);
- this.canvas().element.strokeStyle = this.serialize().color;
- this.canvas().element.stroke();
- };
- Segment.prototype.toString = function () {
- return "Segment: " + JSON.stringify(this.serialize());
- };
- return Segment;
- }());
- return {
- Shape: Shape,
- Circle: Circle,
- Rectangle: Rectangle,
- Point: Point,
- Triangle: Triangle,
- Segment: Segment,
- };
- }());
- var rectangle = new Shapes.Rectangle(2, 1000, "red", 200, 300);
- console.log(rectangle.toString());
- var point = new Shapes.Point(1, 2, "blue");
- console.log(point.toString());
- var triangle = new Shapes.Triangle(1, 2, "red", 2, 0, 3, 3);
- console.log(triangle.toString());
- var segment = new Shapes.Segment(1, 2, "red", 22, 45);
- console.log(segment.toString());
- var circle = new Shapes.Circle(1, 2, "blue", 10);
- console.log(circle.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement