UNPKG

@visactor/vrender-core

Version:

```typescript import { xxx } from '@visactor/vrender-core'; ```

83 lines (74 loc) 3.95 kB
import { Graphic, GRAPHIC_UPDATE_TAG_KEY, NOWORK_ANIMATE_ATTR } from "./graphic"; import { getTheme } from "./theme"; import { application } from "../application"; import { CustomPath2D } from "../common/custom-path2d"; import { STAR_NUMBER_TYPE } from "./constants"; const STAR_UPDATE_TAG_KEY = [ "width", "height", "spikes", "thickness", ...GRAPHIC_UPDATE_TAG_KEY ]; export class Star extends Graphic { constructor(params) { super(params), this.type = "star", this._cachedPoints = [], this.numberType = 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 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.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.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 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; } } Star.NOWORK_ANIMATE_ATTR = NOWORK_ANIMATE_ATTR; export function createStar(attributes) { return new Star(attributes); } //# sourceMappingURL=star.js.map