konva/src/shapes/RegularPolygon.ts

130 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-05-05 22:54:03 +08:00
import { Factory } from '../Factory';
import { Shape, ShapeConfig } from '../Shape';
2023-08-28 22:23:57 +08:00
import { GetSet, Vector2d } from '../types';
2021-05-05 22:54:03 +08:00
import { getNumberValidator } from '../Validators';
import { _registerNode } from '../Global';
import { Context } from '../Context';
2019-01-02 04:59:27 +08:00
export interface RegularPolygonConfig extends ShapeConfig {
sides: number;
radius: number;
}
2019-01-02 04:59:27 +08:00
/**
2019-01-06 16:01:20 +08:00
* RegularPolygon constructor. Examples include triangles, squares, pentagons, hexagons, etc.
2019-01-02 04:59:27 +08:00
* @constructor
* @memberof Konva
* @augments Konva.Shape
* @param {Object} config
* @param {Number} config.sides
* @param {Number} config.radius
* @@shapeParams
* @@nodeParams
* @example
* var hexagon = new Konva.RegularPolygon({
* x: 100,
* y: 200,
* sides: 6,
* radius: 70,
* fill: 'red',
* stroke: 'black',
* strokeWidth: 4
* });
*/
export class RegularPolygon extends Shape<RegularPolygonConfig> {
_sceneFunc(context: Context) {
const points = this._getPoints();
2019-01-02 04:59:27 +08:00
context.beginPath();
context.moveTo(points[0].x, points[0].y);
2019-01-02 04:59:27 +08:00
2024-10-05 22:18:32 +08:00
for (let n = 1; n < points.length; n++) {
context.lineTo(points[n].x, points[n].y);
2019-01-02 04:59:27 +08:00
}
2019-01-02 04:59:27 +08:00
context.closePath();
context.fillStrokeShape(this);
}
_getPoints() {
2023-08-28 22:23:57 +08:00
const sides = this.attrs.sides as number;
const radius = this.attrs.radius || 0;
2023-08-28 22:23:57 +08:00
const points: Vector2d[] = [];
2024-10-05 22:18:32 +08:00
for (let 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();
2024-10-05 22:18:32 +08:00
let minX = points[0].x;
let maxX = points[0].y;
let minY = points[0].x;
let 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,
};
}
2019-01-02 04:59:27 +08:00
getWidth() {
return this.radius() * 2;
}
getHeight() {
return this.radius() * 2;
}
setWidth(width: number) {
this.radius(width / 2);
2019-01-02 04:59:27 +08:00
}
setHeight(height: number) {
this.radius(height / 2);
2019-01-02 04:59:27 +08:00
}
radius: GetSet<number, this>;
sides: GetSet<number, this>;
}
2019-01-06 16:01:20 +08:00
RegularPolygon.prototype.className = 'RegularPolygon';
RegularPolygon.prototype._centroid = true;
RegularPolygon.prototype._attrsAffectingSize = ['radius'];
_registerNode(RegularPolygon);
2019-01-06 16:01:20 +08:00
2019-01-02 04:59:27 +08:00
/**
* get/set radius
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.RegularPolygon#radius
2019-01-02 04:59:27 +08:00
* @param {Number} radius
* @returns {Number}
* @example
* // get radius
* var radius = shape.radius();
*
* // set radius
* shape.radius(10);
*/
2019-02-25 01:06:04 +08:00
Factory.addGetterSetter(RegularPolygon, 'radius', 0, getNumberValidator());
2019-01-02 04:59:27 +08:00
/**
* get/set sides
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.RegularPolygon#sides
2019-01-02 04:59:27 +08:00
* @param {Number} sides
* @returns {Number}
* @example
* // get sides
* var sides = shape.sides();
*
* // set sides
* shape.sides(10);
*/
2019-02-25 01:06:04 +08:00
Factory.addGetterSetter(RegularPolygon, 'sides', 0, getNumberValidator());