You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2021/01/23 12:26:36 UTC

[echarts] branch enhance-morph updated: refact: fix single morphing

This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch enhance-morph
in repository https://gitbox.apache.org/repos/asf/echarts.git


The following commit(s) were added to refs/heads/enhance-morph by this push:
     new 9019ed8  refact: fix single morphing
9019ed8 is described below

commit 9019ed80bd04ca4db1f5ab61a237c1561fa0e8a8
Author: pissang <bm...@gmail.com>
AuthorDate: Sat Jan 23 20:25:24 2021 +0800

    refact: fix single morphing
---
 src/chart/custom/CustomView.ts       |  46 +++++++--------
 src/chart/custom/prepare.ts          | 106 +++++++----------------------------
 src/chart/custom/transitionHelper.ts |  27 ++++++++-
 3 files changed, 70 insertions(+), 109 deletions(-)

diff --git a/src/chart/custom/CustomView.ts b/src/chart/custom/CustomView.ts
index db5d4da..7958fa9 100644
--- a/src/chart/custom/CustomView.ts
+++ b/src/chart/custom/CustomView.ts
@@ -93,7 +93,7 @@ import CustomSeriesModel, {
     LooseElementProps,
     PrepareCustomInfo
 } from './CustomSeries';
-import {getPathList, applyMorphAnimation } from './transitionHelper';
+import {getPathList, applyMorphAnimation, isPath, isDisplayable, copyElement } from './transitionHelper';
 import {
     prepareShapeOrExtraAllPropsFinal,
     prepareShapeOrExtraTransitionFrom,
@@ -101,7 +101,6 @@ import {
     prepareTransformAllPropsFinal,
     prepareTransformTransitionFrom
 } from './prepare';
-import { isMorphing } from 'zrender/src/tool/morphPath';
 
 const transformPropNamesStr = keys(TRANSFORM_PROPS).join(', ');
 
@@ -265,7 +264,7 @@ export default class CustomChartView extends ChartView {
                 const oldElList = [];
                 for (let i = 0; i < oldIndices.length; i++) {
                     const oldEl = oldData.getItemGraphicEl(oldIndices[i]);
-                    removeElementDirectly(oldEl, group);
+                    group.remove(oldEl);
                     oldElList.push(oldEl);
                 }
 
@@ -286,7 +285,7 @@ export default class CustomChartView extends ChartView {
             .updateOneToMany(function (newIndices, oldIdx) {
                 const newLen = newIndices.length;
                 const oldEl = oldData.getItemGraphicEl(oldIdx);
-                removeElementDirectly(oldEl, group);
+                group.remove(oldEl);
 
                 const oldPathList = getPathList(oldEl);
 
@@ -541,14 +540,20 @@ function updateElNormal(
     const transFromProps = {} as ElementProps;
     const propsToSet = {} as ElementProps;
 
-    // TODO Ignore shape on morphing elements.
-    prepareShapeOrExtraTransitionFrom('shape', el, elOption, transFromProps, isInit);
-    prepareShapeOrExtraAllPropsFinal('shape', elOption, propsToSet);
+    // TODO el may can't be morphed
+    const willMorph = customInnerStore(el).morph;
+    if (willMorph) {
+        (el as graphicUtil.Path).attr('shape', (elOption as CustomZRPathOption).shape);
+    }
+    else {
+        prepareShapeOrExtraTransitionFrom('shape', el, elOption, transFromProps, isInit);
+        prepareShapeOrExtraAllPropsFinal('shape', elOption, propsToSet);
+    }
 
     prepareShapeOrExtraTransitionFrom('extra', el, elOption, transFromProps, isInit);
     prepareShapeOrExtraAllPropsFinal('extra', elOption, propsToSet);
-    prepareTransformTransitionFrom(el, null, elOption, transFromProps, isInit);
-    prepareTransformAllPropsFinal(elOption, propsToSet);
+    prepareTransformTransitionFrom(el, elOption, transFromProps, isInit);
+    prepareTransformAllPropsFinal(el, elOption, propsToSet);
 
     const txCfgOpt = attachedTxInfo && attachedTxInfo.normal.cfg;
     if (txCfgOpt) {
@@ -601,7 +606,7 @@ function applyMiscProps(
     // Merge by default.
     hasOwn(elOption, 'silent') && (el.silent = elOption.silent);
     hasOwn(elOption, 'ignore') && (el.ignore = elOption.ignore);
-    if (el instanceof Displayable) {
+    if (isDisplayable(el)) {
         hasOwn(elOption, 'invisible') && (el.invisible = elOption.invisible);
     }
 
@@ -1262,7 +1267,7 @@ function createOrUpdateItem(
 
     // If `elOption` is `null`/`undefined`/`false` (when `renderItem` returns nothing).
     if (!elOption) {
-        removeElementDirectly(existsEl, group);
+        group.remove(existsEl);
         return;
     }
     const el = doCreateOrUpdateEl(api, existsEl, dataIndex, elOption, seriesModel, group, true);
@@ -1288,6 +1293,7 @@ function doCreateOrUpdateEl(
     }
 
     let toBeReplacedIdx = -1;
+    const oldEl = existsEl;
     if (
         existsEl && (
             doesElNeedRecreate(existsEl, elOption)
@@ -1309,6 +1315,9 @@ function doCreateOrUpdateEl(
 
     if (!el) {
         el = createEl(elOption);
+        if (oldEl) {
+            copyElement(oldEl, el);
+        }
     }
     else {
         // FIMXE:NEXT unified clearState?
@@ -1316,14 +1325,15 @@ function doCreateOrUpdateEl(
         // do not clearState but update cached normal state directly.
         el.clearStates();
     }
-    customInnerStore(el).morph = (el instanceof graphicUtil.Path)
+    const morph = customInnerStore(el).morph = isPath(el)
         && (elOption as CustomZRPathOption).morph
         // Don't need morph if element type are same.
         // shaping transition is enough
         && elIsNewCreated;
 
     // Use update animation when morph is enabled.
-    const isInit = elIsNewCreated;
+    // TODO: can't morph if from element is not valid.
+    const isInit = elIsNewCreated && !morph;
 
     attachedTxInfoTmp.normal.cfg = attachedTxInfoTmp.normal.conOpt =
         attachedTxInfoTmp.emphasis.cfg = attachedTxInfoTmp.emphasis.conOpt =
@@ -1431,7 +1441,7 @@ function doCreateOrUpdateClipPath(
             clipPath = createEl(clipPathOpt) as graphicUtil.Path;
             if (__DEV__) {
                 assert(
-                    clipPath instanceof graphicUtil.Path,
+                    isPath(clipPath),
                     'Only any type of `path` can be used in `clipPath`, rather than ' + clipPath.type + '.'
                 );
             }
@@ -1734,11 +1744,3 @@ function getPathData(shape: CustomSVGPathOption['shape']): string {
 function hasOwnPathData(shape: CustomSVGPathOption['shape']): boolean {
     return shape && (hasOwn(shape, 'pathData') || hasOwn(shape, 'd'));
 }
-
-function isPath(el: Element): el is graphicUtil.Path {
-    return el && el instanceof graphicUtil.Path;
-}
-
-function removeElementDirectly(el: Element, group: ViewRootGroup): void {
-    el && group.remove(el);
-}
diff --git a/src/chart/custom/prepare.ts b/src/chart/custom/prepare.ts
index ba51f04..eb1af0d 100644
--- a/src/chart/custom/prepare.ts
+++ b/src/chart/custom/prepare.ts
@@ -28,13 +28,10 @@ import {
     TRANSFORM_PROPS,
     TransitionAnyOption
 } from './CustomSeries';
-import * as graphicUtil from '../../util/graphic';
 import { normalizeToArray } from '../../util/model';
 import { assert, hasOwn, indexOf, isArrayLike, keys } from 'zrender/src/core/util';
 import { cloneValue } from 'zrender/src/animation/Animator';
 import Displayable from 'zrender/src/graphic/Displayable';
-import * as matrix from 'zrender/src/core/matrix';
-import { isMorphing } from 'zrender/src/tool/morphPath';
 
 const LEGACY_TRANSFORM_PROPS = {
     position: ['x', 'y'],
@@ -43,36 +40,32 @@ const LEGACY_TRANSFORM_PROPS = {
 } as const;
 type LegacyTransformProp = keyof typeof LEGACY_TRANSFORM_PROPS;
 
-const tmpTransformable = new Transformable();
-
 function setLagecyTransformProp(
     elOption: CustomElementOption,
     targetProps: Partial<Pick<Transformable, TransformProp>>,
-    legacyName: LegacyTransformProp,
-    fromTransformable?: Transformable // If provided, retrieve from the element.
+    legacyName: LegacyTransformProp
 ): void {
     const legacyArr = (elOption as any)[legacyName];
     const xyName = LEGACY_TRANSFORM_PROPS[legacyName];
     if (legacyArr) {
-        if (fromTransformable) {
-            targetProps[xyName[0]] = fromTransformable[xyName[0]];
-            targetProps[xyName[1]] = fromTransformable[xyName[1]];
-        }
-        else {
-            targetProps[xyName[0]] = legacyArr[0];
-            targetProps[xyName[1]] = legacyArr[1];
-        }
+        targetProps[xyName[0]] = legacyArr[0];
+        targetProps[xyName[1]] = legacyArr[1];
     }
 }
 
 function setTransformProp(
+    el: Element,
     elOption: CustomElementOption,
     allProps: Partial<Pick<Transformable, TransformProp>>,
     name: TransformProp,
-    fromTransformable?: Transformable // If provided, retrieve from the element.
+    defaultVal: number
 ): void {
     if (elOption[name] != null) {
-        allProps[name] = fromTransformable ? fromTransformable[name] : elOption[name];
+        allProps[name] = elOption[name];
+    }
+    else if (el[name] !== defaultVal) {
+        // Set to default val.
+        allProps[name] = defaultVal;
     }
 }
 
@@ -178,7 +171,6 @@ export function prepareShapeOrExtraAllPropsFinal(
 // See [STRATEGY_TRANSITION].
 export function prepareTransformTransitionFrom(
     el: Element,
-    morphFromEl: graphicUtil.Path,
     elOption: CustomElementOption,
     transFromProps: ElementProps,
     isInit: boolean
@@ -197,19 +189,7 @@ export function prepareTransformTransitionFrom(
     }
 
     if (!isInit) {
-        // If morphing, force transition all transform props.
-        // otherwise might have incorrect morphing animation.
-        if (morphFromEl) {
-            const fromTransformable = calcOldElLocalTransformBasedOnNewElParent(morphFromEl, el);
-            setTransformPropToTransitionFrom(transFromProps, 'x', fromTransformable);
-            setTransformPropToTransitionFrom(transFromProps, 'y', fromTransformable);
-            setTransformPropToTransitionFrom(transFromProps, 'scaleX', fromTransformable);
-            setTransformPropToTransitionFrom(transFromProps, 'scaleY', fromTransformable);
-            setTransformPropToTransitionFrom(transFromProps, 'originX', fromTransformable);
-            setTransformPropToTransitionFrom(transFromProps, 'originY', fromTransformable);
-            setTransformPropToTransitionFrom(transFromProps, 'rotation', fromTransformable);
-        }
-        else if (elOption.transition) {
+        if (elOption.transition) {
             const transitionKeys = normalizeToArray(elOption.transition);
             for (let i = 0; i < transitionKeys.length; i++) {
                 const key = transitionKeys[i];
@@ -247,19 +227,21 @@ export function prepareTransformTransitionFrom(
 }
 
 export function prepareTransformAllPropsFinal(
+    el: Element,
     elOption: CustomElementOption,
     allProps: ElementProps
 ): void {
     setLagecyTransformProp(elOption, allProps, 'position');
     setLagecyTransformProp(elOption, allProps, 'scale');
     setLagecyTransformProp(elOption, allProps, 'origin');
-    setTransformProp(elOption, allProps, 'x');
-    setTransformProp(elOption, allProps, 'y');
-    setTransformProp(elOption, allProps, 'scaleX');
-    setTransformProp(elOption, allProps, 'scaleY');
-    setTransformProp(elOption, allProps, 'originX');
-    setTransformProp(elOption, allProps, 'originY');
-    setTransformProp(elOption, allProps, 'rotation');
+
+    setTransformProp(el, elOption, allProps, 'x', 0);
+    setTransformProp(el, elOption, allProps, 'y', 0);
+    setTransformProp(el, elOption, allProps, 'scaleX', 1);
+    setTransformProp(el, elOption, allProps, 'scaleY', 1);
+    setTransformProp(el, elOption, allProps, 'originX', 0);
+    setTransformProp(el, elOption, allProps, 'originY', 0);
+    setTransformProp(el, elOption, allProps, 'rotation', 0);
 }
 
 // See [STRATEGY_TRANSITION].
@@ -331,54 +313,6 @@ export function prepareStyleTransitionFrom(
     }
 }
 
-/**
- * If make "transform"(x/y/scaleX/scaleY/orient/originX/originY) transition between
- * two path elements that have different hierarchy, before we retrieve the "from" props,
- * we have to calculate the local transition of the "oldPath" based on the parent of
- * the "newPath".
- * At present, the case only happend in "morphing". Without morphing, the transform
- * transition are all between elements in the same hierarchy, where this kind of process
- * is not needed.
- *
- * [CAVEAT]:
- * This method makes sense only if: (very tricky)
- * (1) "newEl" has been added to its final parent.
- * (2) Local transform props of "newPath.parent" are not at their final value but already
- * have been at the "from value".
- *     This is currently ensured by:
- *     (2.1) "graphicUtil.animationFrom", which will set the element to the "from value"
- *     immediately.
- *     (2.2) "morph" option is not allowed to be set on Group, so all of the groups have
- *     been finished their "updateElNormal" when calling this method in morphing process.
- */
-function calcOldElLocalTransformBasedOnNewElParent(oldEl: Element, newEl: Element): Transformable {
-    if (!oldEl || oldEl === newEl || oldEl.parent === newEl.parent) {
-        return oldEl;
-    }
-
-    // Not sure oldEl is rendered (may have "lazyUpdate"),
-    // so always call `getComputedTransform`.
-    const tmpM = tmpTransformable.transform
-        || (tmpTransformable.transform = matrix.identity([]));
-
-    const oldGlobalTransform = oldEl.getComputedTransform();
-    oldGlobalTransform
-        ? matrix.copy(tmpM, oldGlobalTransform)
-        : matrix.identity(tmpM);
-
-    const newParent = newEl.parent;
-    if (newParent) {
-        newParent.getComputedTransform();
-    }
-
-    tmpTransformable.originX = oldEl.originX;
-    tmpTransformable.originY = oldEl.originY;
-    tmpTransformable.parent = newParent;
-    tmpTransformable.decomposeTransform();
-
-    return tmpTransformable;
-}
-
 let checkNonStyleTansitionRefer: (propName: string, optVal: unknown, elVal: unknown) => void;
 if (__DEV__) {
     checkNonStyleTansitionRefer = function (propName: string, optVal: unknown, elVal: unknown): void {
diff --git a/src/chart/custom/transitionHelper.ts b/src/chart/custom/transitionHelper.ts
index 553b435..fe182e7 100644
--- a/src/chart/custom/transitionHelper.ts
+++ b/src/chart/custom/transitionHelper.ts
@@ -30,8 +30,9 @@ import { Path } from '../../util/graphic';
 import { SeriesModel } from '../../export/api';
 import Element, { ElementAnimateConfig } from 'zrender/src/Element';
 import { AnimationEasing } from 'zrender/src/animation/easing';
-import { Dictionary, PayloadAnimationPart } from '../../util/types';
+import { PayloadAnimationPart } from '../../util/types';
 import { isArray, isFunction } from 'zrender/src/core/util';
+import Displayable from 'zrender/src/graphic/Displayable';
 
 
 type DescendentElements = Element[];
@@ -193,3 +194,27 @@ export function getPathList(
     });
     return pathList;
 }
+
+
+export function isPath(el: Element): el is Path {
+    return el instanceof Path;
+}
+export function isDisplayable(el: Element) : el is Displayable {
+    return el instanceof Displayable;
+}
+
+export function copyElement(sourceEl: Element, targetEl: Element) {
+    targetEl.copyTransform(sourceEl);
+    if (isDisplayable(targetEl) && isDisplayable(sourceEl)) {
+        targetEl.setStyle(sourceEl.style);
+        targetEl.z = sourceEl.z;
+        targetEl.z2 = sourceEl.z2;
+        targetEl.zlevel = sourceEl.zlevel;
+        targetEl.invisible = sourceEl.invisible;
+        targetEl.ignore = sourceEl.ignore;
+
+        if (isPath(targetEl) && isPath(sourceEl)) {
+            targetEl.setShape(sourceEl.shape);
+        }
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org