UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

391 lines (390 loc) 12.1 kB
import { __extends } from "../../../tslib/tslib.es6.mjs"; import Displayable, { DEFAULT_COMMON_STYLE, DEFAULT_COMMON_ANIMATION_PROPS } from "./Displayable.mjs"; import PathProxy from "../core/PathProxy.mjs"; import { containStroke, contain } from "../contain/path.mjs"; import { defaults, keys, extend, isString, createObject, clone } from "../core/util.mjs"; import { lum } from "../tool/color.mjs"; import { DARK_LABEL_COLOR, LIGHTER_LABEL_COLOR, LIGHT_LABEL_COLOR, DARK_MODE_THRESHOLD } from "../config.mjs"; import { REDRAW_BIT, SHAPE_CHANGED_BIT, STYLE_CHANGED_BIT } from "./constants.mjs"; import { TRANSFORMABLE_PROPS } from "../core/Transformable.mjs"; var DEFAULT_PATH_STYLE = defaults({ fill: "#000", stroke: null, strokePercent: 1, fillOpacity: 1, strokeOpacity: 1, lineDashOffset: 0, lineWidth: 1, lineCap: "butt", miterLimit: 10, strokeNoScale: false, strokeFirst: false }, DEFAULT_COMMON_STYLE); var DEFAULT_PATH_ANIMATION_PROPS = { style: defaults({ fill: true, stroke: true, strokePercent: true, fillOpacity: true, strokeOpacity: true, lineDashOffset: true, lineWidth: true, miterLimit: true }, DEFAULT_COMMON_ANIMATION_PROPS.style) }; var pathCopyParams = TRANSFORMABLE_PROPS.concat([ "invisible", "culling", "z", "z2", "zlevel", "parent" ]); var Path = function(_super) { __extends(Path2, _super); function Path2(opts) { return _super.call(this, opts) || this; } Path2.prototype.update = function() { var _this = this; _super.prototype.update.call(this); var style = this.style; if (style.decal) { var decalEl = this._decalEl = this._decalEl || new Path2(); if (decalEl.buildPath === Path2.prototype.buildPath) { decalEl.buildPath = function(ctx) { _this.buildPath(ctx, _this.shape); }; } decalEl.silent = true; var decalElStyle = decalEl.style; for (var key in style) { if (decalElStyle[key] !== style[key]) { decalElStyle[key] = style[key]; } } decalElStyle.fill = style.fill ? style.decal : null; decalElStyle.decal = null; decalElStyle.shadowColor = null; style.strokeFirst && (decalElStyle.stroke = null); for (var i = 0; i < pathCopyParams.length; ++i) { decalEl[pathCopyParams[i]] = this[pathCopyParams[i]]; } decalEl.__dirty |= REDRAW_BIT; } else if (this._decalEl) { this._decalEl = null; } }; Path2.prototype.getDecalElement = function() { return this._decalEl; }; Path2.prototype._init = function(props) { var keysArr = keys(props); this.shape = this.getDefaultShape(); var defaultStyle = this.getDefaultStyle(); if (defaultStyle) { this.useStyle(defaultStyle); } for (var i = 0; i < keysArr.length; i++) { var key = keysArr[i]; var value = props[key]; if (key === "style") { if (!this.style) { this.useStyle(value); } else { extend(this.style, value); } } else if (key === "shape") { extend(this.shape, value); } else { _super.prototype.attrKV.call(this, key, value); } } if (!this.style) { this.useStyle({}); } }; Path2.prototype.getDefaultStyle = function() { return null; }; Path2.prototype.getDefaultShape = function() { return {}; }; Path2.prototype.canBeInsideText = function() { return this.hasFill(); }; Path2.prototype.getInsideTextFill = function() { var pathFill = this.style.fill; if (pathFill !== "none") { if (isString(pathFill)) { var fillLum = lum(pathFill, 0); if (fillLum > 0.5) { return DARK_LABEL_COLOR; } else if (fillLum > 0.2) { return LIGHTER_LABEL_COLOR; } return LIGHT_LABEL_COLOR; } else if (pathFill) { return LIGHT_LABEL_COLOR; } } return DARK_LABEL_COLOR; }; Path2.prototype.getInsideTextStroke = function(textFill) { var pathFill = this.style.fill; if (isString(pathFill)) { var zr = this.__zr; var isDarkMode = !!(zr && zr.isDarkMode()); var isDarkLabel = lum(textFill, 0) < DARK_MODE_THRESHOLD; if (isDarkMode === isDarkLabel) { return pathFill; } } }; Path2.prototype.buildPath = function(ctx, shapeCfg, inBatch) { }; Path2.prototype.pathUpdated = function() { this.__dirty &= ~SHAPE_CHANGED_BIT; }; Path2.prototype.getUpdatedPathProxy = function(inBatch) { !this.path && this.createPathProxy(); this.path.beginPath(); this.buildPath(this.path, this.shape, inBatch); return this.path; }; Path2.prototype.createPathProxy = function() { this.path = new PathProxy(false); }; Path2.prototype.hasStroke = function() { var style = this.style; var stroke = style.stroke; return !(stroke == null || stroke === "none" || !(style.lineWidth > 0)); }; Path2.prototype.hasFill = function() { var style = this.style; var fill = style.fill; return fill != null && fill !== "none"; }; Path2.prototype.getBoundingRect = function() { var rect = this._rect; var style = this.style; var needsUpdateRect = !rect; if (needsUpdateRect) { var firstInvoke = false; if (!this.path) { firstInvoke = true; this.createPathProxy(); } var path = this.path; if (firstInvoke || this.__dirty & SHAPE_CHANGED_BIT) { path.beginPath(); this.buildPath(path, this.shape, false); this.pathUpdated(); } rect = path.getBoundingRect(); } this._rect = rect; if (this.hasStroke() && this.path && this.path.len() > 0) { var rectStroke = this._rectStroke || (this._rectStroke = rect.clone()); if (this.__dirty || needsUpdateRect) { rectStroke.copy(rect); var lineScale = style.strokeNoScale ? this.getLineScale() : 1; var w = style.lineWidth; if (!this.hasFill()) { var strokeContainThreshold = this.strokeContainThreshold; w = Math.max(w, strokeContainThreshold == null ? 4 : strokeContainThreshold); } if (lineScale > 1e-10) { rectStroke.width += w / lineScale; rectStroke.height += w / lineScale; rectStroke.x -= w / lineScale / 2; rectStroke.y -= w / lineScale / 2; } } return rectStroke; } return rect; }; Path2.prototype.contain = function(x, y) { var localPos = this.transformCoordToLocal(x, y); var rect = this.getBoundingRect(); var style = this.style; x = localPos[0]; y = localPos[1]; if (rect.contain(x, y)) { var pathProxy = this.path; if (this.hasStroke()) { var lineWidth = style.lineWidth; var lineScale = style.strokeNoScale ? this.getLineScale() : 1; if (lineScale > 1e-10) { if (!this.hasFill()) { lineWidth = Math.max(lineWidth, this.strokeContainThreshold); } if (containStroke(pathProxy, lineWidth / lineScale, x, y)) { return true; } } } if (this.hasFill()) { return contain(pathProxy, x, y); } } return false; }; Path2.prototype.dirtyShape = function() { this.__dirty |= SHAPE_CHANGED_BIT; if (this._rect) { this._rect = null; } if (this._decalEl) { this._decalEl.dirtyShape(); } this.markRedraw(); }; Path2.prototype.dirty = function() { this.dirtyStyle(); this.dirtyShape(); }; Path2.prototype.animateShape = function(loop) { return this.animate("shape", loop); }; Path2.prototype.updateDuringAnimation = function(targetKey) { if (targetKey === "style") { this.dirtyStyle(); } else if (targetKey === "shape") { this.dirtyShape(); } else { this.markRedraw(); } }; Path2.prototype.attrKV = function(key, value) { if (key === "shape") { this.setShape(value); } else { _super.prototype.attrKV.call(this, key, value); } }; Path2.prototype.setShape = function(keyOrObj, value) { var shape = this.shape; if (!shape) { shape = this.shape = {}; } if (typeof keyOrObj === "string") { shape[keyOrObj] = value; } else { extend(shape, keyOrObj); } this.dirtyShape(); return this; }; Path2.prototype.shapeChanged = function() { return !!(this.__dirty & SHAPE_CHANGED_BIT); }; Path2.prototype.createStyle = function(obj) { return createObject(DEFAULT_PATH_STYLE, obj); }; Path2.prototype._innerSaveToNormal = function(toState) { _super.prototype._innerSaveToNormal.call(this, toState); var normalState = this._normalState; if (toState.shape && !normalState.shape) { normalState.shape = extend({}, this.shape); } }; Path2.prototype._applyStateObj = function(stateName, state, normalState, keepCurrentStates, transition, animationCfg) { _super.prototype._applyStateObj.call(this, stateName, state, normalState, keepCurrentStates, transition, animationCfg); var needsRestoreToNormal = !(state && keepCurrentStates); var targetShape; if (state && state.shape) { if (transition) { if (keepCurrentStates) { targetShape = state.shape; } else { targetShape = extend({}, normalState.shape); extend(targetShape, state.shape); } } else { targetShape = extend({}, keepCurrentStates ? this.shape : normalState.shape); extend(targetShape, state.shape); } } else if (needsRestoreToNormal) { targetShape = normalState.shape; } if (targetShape) { if (transition) { this.shape = extend({}, this.shape); var targetShapePrimaryProps = {}; var shapeKeys = keys(targetShape); for (var i = 0; i < shapeKeys.length; i++) { var key = shapeKeys[i]; if (typeof targetShape[key] === "object") { this.shape[key] = targetShape[key]; } else { targetShapePrimaryProps[key] = targetShape[key]; } } this._transitionState(stateName, { shape: targetShapePrimaryProps }, animationCfg); } else { this.shape = targetShape; this.dirtyShape(); } } }; Path2.prototype._mergeStates = function(states) { var mergedState = _super.prototype._mergeStates.call(this, states); var mergedShape; for (var i = 0; i < states.length; i++) { var state = states[i]; if (state.shape) { mergedShape = mergedShape || {}; this._mergeStyle(mergedShape, state.shape); } } if (mergedShape) { mergedState.shape = mergedShape; } return mergedState; }; Path2.prototype.getAnimationStyleProps = function() { return DEFAULT_PATH_ANIMATION_PROPS; }; Path2.prototype.isZeroArea = function() { return false; }; Path2.extend = function(defaultProps) { var Sub = function(_super2) { __extends(Sub2, _super2); function Sub2(opts) { var _this = _super2.call(this, opts) || this; defaultProps.init && defaultProps.init.call(_this, opts); return _this; } Sub2.prototype.getDefaultStyle = function() { return clone(defaultProps.style); }; Sub2.prototype.getDefaultShape = function() { return clone(defaultProps.shape); }; return Sub2; }(Path2); for (var key in defaultProps) { if (typeof defaultProps[key] === "function") { Sub.prototype[key] = defaultProps[key]; } } return Sub; }; Path2.initDefaultProps = function() { var pathProto = Path2.prototype; pathProto.type = "path"; pathProto.strokeContainThreshold = 5; pathProto.segmentIgnoreThreshold = 0; pathProto.subPixelOptimize = false; pathProto.autoBatch = false; pathProto.__dirty = REDRAW_BIT | STYLE_CHANGED_BIT | SHAPE_CHANGED_BIT; }(); return Path2; }(Displayable); var Path$1 = Path; export { DEFAULT_PATH_ANIMATION_PROPS, DEFAULT_PATH_STYLE, Path$1 as default };