UNPKG

@visactor/vrender-core

Version:
99 lines (88 loc) 3.94 kB
import { isArray } from "@visactor/vutils"; 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 { SYMBOL_NUMBER_TYPE } from "./constants"; import { updateBoundsOfSymbolOuterBorder } from "./graphic-service/symbol-outer-border-bounds"; const SYMBOL_UPDATE_TAG_KEY = [ "symbolType", "size", ...GRAPHIC_UPDATE_TAG_KEY ]; export class Symbol extends Graphic { constructor(params = { symbolType: "circle" }) { super(params), this.type = "symbol", this.numberType = SYMBOL_NUMBER_TYPE; } getParsedPath() { return this.shouldUpdateShape() && (this._parsedPath = this.doUpdateParsedPath(), this.clearUpdateShapeTag()), this._parsedPath; } getParsedPath2D(x = 0, y = 0, size = 1) { let path = null; try { path = new Path2D; } catch (err) { return null; } const parsedPath = this.getParsedPath(); if (!parsedPath) return null; parsedPath.draw(path, size, x, y); } isValid() { return super.isValid() && this._isValid(); } _isValid() { const {size: size} = this.attribute; return isArray(size) ? 2 === size.length && size.every(this._validNumber) : this._validNumber(size); } doUpdateParsedPath() { const {symbolType: symbolType = "circle"} = this.attribute; return super.parsePath(symbolType); } getGraphicTheme() { return getTheme(this).symbol; } updateAABBBounds(attribute, symbolTheme, aabbBounds, full) { this.updatePathProxyAABBBounds(aabbBounds) || (full ? this.updateSymbolAABBBoundsImprecise(attribute, symbolTheme, aabbBounds) : this.updateSymbolAABBBoundsAccurate(attribute, symbolTheme, aabbBounds)); const {tb1: tb1, tb2: tb2} = application.graphicService.updateTempAABBBounds(aabbBounds); updateBoundsOfSymbolOuterBorder(attribute, symbolTheme, tb1), aabbBounds.union(tb1), tb1.setValue(tb2.x1, tb2.y1, tb2.x2, tb2.y2); const {lineJoin: lineJoin = symbolTheme.lineJoin} = attribute; return application.graphicService.transformAABBBounds(attribute, aabbBounds, symbolTheme, "miter" === lineJoin, this), aabbBounds; } updateSymbolAABBBoundsImprecise(attribute, symbolTheme, aabbBounds) { const {size: size = symbolTheme.size} = attribute; if (isArray(size)) aabbBounds.set(-size[0] / 2, -size[1] / 2, size[0] / 2, size[1] / 2); else { const halfWH = size / 2; aabbBounds.set(-halfWH, -halfWH, halfWH, halfWH); } return aabbBounds; } updateSymbolAABBBoundsAccurate(attribute, symbolTheme, aabbBounds) { const {size: size = symbolTheme.size} = attribute; return this.getParsedPath().bounds(size, aabbBounds), aabbBounds; } needUpdateTags(keys) { return super.needUpdateTags(keys, SYMBOL_UPDATE_TAG_KEY); } needUpdateTag(key) { return super.needUpdateTag(key, SYMBOL_UPDATE_TAG_KEY); } toCustomPath() { const symbolInstance = this.getParsedPath(), size = this.attribute.size, formattedSize = isArray(size) ? size : [ size, size ]; return symbolInstance.path ? (new CustomPath2D).fromCustomPath2D(symbolInstance.path, 0, 0, formattedSize[0], formattedSize[1]) : (new CustomPath2D).fromString(symbolInstance.pathStr, 0, 0, formattedSize[0], formattedSize[1]); } clone() { return new Symbol(Object.assign({}, this.attribute)); } getNoWorkAnimateAttr() { return Symbol.NOWORK_ANIMATE_ATTR; } } Symbol.NOWORK_ANIMATE_ATTR = Object.assign({ symbolType: 1 }, NOWORK_ANIMATE_ATTR); export function createSymbol(attributes) { return new Symbol(attributes); } //# sourceMappingURL=symbol.js.map