@visactor/vrender-core
Version:
```typescript import { xxx } from '@visactor/vrender-core'; ```
79 lines (73 loc) • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.createStar = exports.Star = void 0;
const graphic_1 = require("./graphic"), theme_1 = require("./theme"), application_1 = require("../application"), custom_path2d_1 = require("../common/custom-path2d"), constants_1 = require("./constants"), STAR_UPDATE_TAG_KEY = [ "width", "height", "spikes", "thickness", ...graphic_1.GRAPHIC_UPDATE_TAG_KEY ];
class Star extends graphic_1.Graphic {
constructor(params) {
super(params), this.type = "star", this._cachedPoints = [], this.numberType = constants_1.STAR_NUMBER_TYPE;
}
isValid() {
return super.isValid() && this._isValid();
}
_isValid() {
const {width: width, height: height, spikes: spikes} = this.attribute;
return (null == width || width > 0) && (null == height || height > 0) && (null == spikes || spikes >= 3 && Number.isInteger(spikes));
}
getGraphicTheme() {
return (0, theme_1.getTheme)(this).star;
}
updateAABBBounds(attribute, rectTheme, aabbBounds) {
if (!this.updatePathProxyAABBBounds(aabbBounds)) {
const {width: width = 0, height: height = 0} = attribute;
(isFinite(width) || isFinite(height)) && aabbBounds.set(0, 0, width, height);
}
const {tb1: tb1, tb2: tb2} = application_1.application.graphicService.updateTempAABBBounds(aabbBounds);
return aabbBounds.union(tb1), tb1.setValue(tb2.x1, tb2.y1, tb2.x2, tb2.y2), this.widthWithoutTransform = aabbBounds.x2 - aabbBounds.x1,
this.heightWithoutTransform = aabbBounds.y2 - aabbBounds.y1, application_1.application.graphicService.transformAABBBounds(attribute, aabbBounds, rectTheme, !1, this),
aabbBounds;
}
getCachedPoints() {
return this.shouldUpdateShape() && (this._cachedPoints = this.getStarPoints(this.attribute, this.getGraphicTheme()),
this.clearUpdateShapeTag()), this._cachedPoints;
}
getStarPoints(attribute, starTheme) {
const {width: width = starTheme.width, height: height = starTheme.height, spikes: spikes = starTheme.spikes, thickness: thickness = starTheme.thickness} = attribute, validSpikes = Math.max(3, Math.floor(spikes)), validThickness = Math.max(0, Math.min(1, thickness)), points = [], outerRadius = Math.min(width, height) / 2, innerRadius = outerRadius * (1 - validThickness), centerX = width / 2, centerY = height / 2;
for (let i = 0; i < 2 * validSpikes; i++) {
const radius = i % 2 == 0 ? outerRadius : innerRadius, angle = Math.PI / validSpikes * i, scaleX = width / (2 * outerRadius), scaleY = height / (2 * outerRadius);
points.push({
x: centerX + Math.sin(angle) * radius * scaleX,
y: centerY - Math.cos(angle) * radius * scaleY
});
}
return this._cachedPoints = points, points;
}
_interpolate(key, ratio, lastStepVal, nextStepVal, nextAttributes) {
"width" !== key && "height" !== key && "spikes" !== key && "thickness" !== key || (nextAttributes[key] = lastStepVal + (nextStepVal - lastStepVal) * ratio);
}
needUpdateTags(keys) {
return super.needUpdateTags(keys, STAR_UPDATE_TAG_KEY);
}
needUpdateTag(key) {
return super.needUpdateTag(key, STAR_UPDATE_TAG_KEY);
}
toCustomPath() {
let path = super.toCustomPath();
if (path) return path;
const starTheme = this.getGraphicTheme(), points = this.getStarPoints(this.attribute, starTheme);
return path = new custom_path2d_1.CustomPath2D, points.forEach(((point, index) => {
0 === index ? path.moveTo(point.x, point.y) : path.lineTo(point.x, point.y);
})), path.closePath(), path;
}
clone() {
return new Star(Object.assign({}, this.attribute));
}
getNoWorkAnimateAttr() {
return Star.NOWORK_ANIMATE_ATTR;
}
}
function createStar(attributes) {
return new Star(attributes);
}
exports.Star = Star, Star.NOWORK_ANIMATE_ATTR = graphic_1.NOWORK_ANIMATE_ATTR, exports.createStar = createStar;
//# sourceMappingURL=star.js.map