70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.RegularPolygon = void 0;
|
|
const Factory_1 = require("../Factory");
|
|
const Shape_1 = require("../Shape");
|
|
const Validators_1 = require("../Validators");
|
|
const Global_1 = require("../Global");
|
|
class RegularPolygon extends Shape_1.Shape {
|
|
_sceneFunc(context) {
|
|
const points = this._getPoints();
|
|
context.beginPath();
|
|
context.moveTo(points[0].x, points[0].y);
|
|
for (var n = 1; n < points.length; n++) {
|
|
context.lineTo(points[n].x, points[n].y);
|
|
}
|
|
context.closePath();
|
|
context.fillStrokeShape(this);
|
|
}
|
|
_getPoints() {
|
|
const sides = this.attrs.sides;
|
|
const radius = this.attrs.radius || 0;
|
|
const points = [];
|
|
for (var n = 0; n < sides; n++) {
|
|
points.push({
|
|
x: radius * Math.sin((n * 2 * Math.PI) / sides),
|
|
y: -1 * radius * Math.cos((n * 2 * Math.PI) / sides),
|
|
});
|
|
}
|
|
return points;
|
|
}
|
|
getSelfRect() {
|
|
const points = this._getPoints();
|
|
var minX = points[0].x;
|
|
var maxX = points[0].y;
|
|
var minY = points[0].x;
|
|
var maxY = points[0].y;
|
|
points.forEach((point) => {
|
|
minX = Math.min(minX, point.x);
|
|
maxX = Math.max(maxX, point.x);
|
|
minY = Math.min(minY, point.y);
|
|
maxY = Math.max(maxY, point.y);
|
|
});
|
|
return {
|
|
x: minX,
|
|
y: minY,
|
|
width: maxX - minX,
|
|
height: maxY - minY,
|
|
};
|
|
}
|
|
getWidth() {
|
|
return this.radius() * 2;
|
|
}
|
|
getHeight() {
|
|
return this.radius() * 2;
|
|
}
|
|
setWidth(width) {
|
|
this.radius(width / 2);
|
|
}
|
|
setHeight(height) {
|
|
this.radius(height / 2);
|
|
}
|
|
}
|
|
exports.RegularPolygon = RegularPolygon;
|
|
RegularPolygon.prototype.className = 'RegularPolygon';
|
|
RegularPolygon.prototype._centroid = true;
|
|
RegularPolygon.prototype._attrsAffectingSize = ['radius'];
|
|
(0, Global_1._registerNode)(RegularPolygon);
|
|
Factory_1.Factory.addGetterSetter(RegularPolygon, 'radius', 0, (0, Validators_1.getNumberValidator)());
|
|
Factory_1.Factory.addGetterSetter(RegularPolygon, 'sides', 0, (0, Validators_1.getNumberValidator)());
|