UNPKG

1.34 kBJavaScriptView Raw
1/**
2 * @category Shape Drawers
3 */
4export class PolygonDrawerBase {
5 getSidesCount(particle) {
6 var _a, _b;
7 const polygon = particle.shapeData;
8 return (_b = (_a = polygon === null || polygon === void 0 ? void 0 : polygon.sides) !== null && _a !== void 0 ? _a : polygon === null || polygon === void 0 ? void 0 : polygon.nb_sides) !== null && _b !== void 0 ? _b : 5;
9 }
10 draw(context, particle, radius) {
11 const start = this.getCenter(particle, radius);
12 const side = this.getSidesData(particle, radius);
13 // By Programming Thomas - https://programmingthomas.wordpress.com/2013/04/03/n-sided-shapes/
14 const sideCount = side.count.numerator * side.count.denominator;
15 const decimalSides = side.count.numerator / side.count.denominator;
16 const interiorAngleDegrees = (180 * (decimalSides - 2)) / decimalSides;
17 const interiorAngle = Math.PI - (Math.PI * interiorAngleDegrees) / 180; // convert to radians
18 if (!context) {
19 return;
20 }
21 context.beginPath();
22 context.translate(start.x, start.y);
23 context.moveTo(0, 0);
24 for (let i = 0; i < sideCount; i++) {
25 context.lineTo(side.length, 0);
26 context.translate(side.length, 0);
27 context.rotate(interiorAngle);
28 }
29 }
30}