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/11/23 01:24:56 UTC

[echarts] branch graphic-animation updated: feat(animation): add keyframe based animation

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

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


The following commit(s) were added to refs/heads/graphic-animation by this push:
     new e449a87  feat(animation): add keyframe based animation
e449a87 is described below

commit e449a871a316805376ffa25e18e716ee49ac37de
Author: pissang <bm...@gmail.com>
AuthorDate: Tue Nov 23 09:24:11 2021 +0800

    feat(animation): add keyframe based animation
---
 src/animation/customGraphicKeyframeAnimation.ts |  73 +++++++++++++++
 src/animation/customGraphicTransition.ts        |  39 +++-----
 src/component/graphic/GraphicModel.ts           |  12 ++-
 src/component/graphic/GraphicView.ts            |   5 ++
 src/util/types.ts                               | 115 +++++++++++++-----------
 test/graphic-animation.html                     |  95 ++++++++++++++++++++
 test/graphic-transition.html                    |  55 ++++++++----
 7 files changed, 294 insertions(+), 100 deletions(-)

diff --git a/src/animation/customGraphicKeyframeAnimation.ts b/src/animation/customGraphicKeyframeAnimation.ts
new file mode 100644
index 0000000..64e82c1
--- /dev/null
+++ b/src/animation/customGraphicKeyframeAnimation.ts
@@ -0,0 +1,73 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*   http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied.  See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+import { AnimationEasing } from 'zrender/src/animation/easing';
+import Element from 'zrender/src/Element';
+import { keys, filter, each } from 'zrender/src/core/util';
+import { ELEMENT_ANIMATABLE_PROPS } from './customGraphicTransition';
+import { ElementAnimationOption } from '../util/types';
+
+// Helpers for creating keyframe based animations in custom series and graphic components.
+
+type AnimationKeyframe<T extends Record<string, any>> = T & {
+    easing?: AnimationEasing
+    percent?: number    // 0 - 1
+};
+
+export interface ElementKeyframeAnimationOption<Props extends Record<string, any>> extends ElementAnimationOption {
+    // Animation configuration for keyframe based animation.
+    loop?: boolean
+    keyframes?: AnimationKeyframe<Props>[]
+}
+
+export function applyKeyframeAnimation<T extends Record<string, any>>(
+    el: Element, animationOpts?: ElementKeyframeAnimationOption<T>
+) {
+    const keyframes = animationOpts.keyframes;
+    const duration = animationOpts.duration;
+    if (!keyframes || !duration) {
+        return;
+    }
+
+    function applyKeyframeAnimationOnProp(propName: typeof ELEMENT_ANIMATABLE_PROPS[number]) {
+        const animator = el.animate(propName, animationOpts.loop);
+        each(keyframes, kf => {
+            // Stop current animation.
+            const animators = el.animators;
+            const kfValues = propName ? kf[propName] : kf;
+            let propKeys = keys(kfValues);
+            if (!propName) {
+                // PENDING performance?
+                propKeys = filter(propKeys, key => key !== 'percent' && key !== 'easing');
+            }
+            for (let i = 0; i < animators.length; i++) {
+                if (animators[i] !== animator) {
+                    animators[i].stopTracks(propKeys);
+                }
+            }
+
+            animator.whenWithKeys(duration * kf.percent, kfValues, propKeys, kf.easing);
+        });
+        animator
+            .delay(animationOpts.delay || 0)
+            .start(animationOpts.easing);
+    }
+
+    each(ELEMENT_ANIMATABLE_PROPS, applyKeyframeAnimationOnProp);
+}
\ No newline at end of file
diff --git a/src/animation/customGraphicTransition.ts b/src/animation/customGraphicTransition.ts
index 5bcba64..67b25ce 100644
--- a/src/animation/customGraphicTransition.ts
+++ b/src/animation/customGraphicTransition.ts
@@ -17,11 +17,11 @@
 * under the License.
 */
 
-// Helpers for custom graphic elements in custom series and graphic components.
+// Helpers for creating transitions in custom series and graphic components.
 import Element, { ElementProps } from 'zrender/src/Element';
 
 import { makeInner, normalizeToArray } from '../util/model';
-import { assert, bind, eqNaN, extend, hasOwn, indexOf, isArrayLike, keys } from 'zrender/src/core/util';
+import { assert, bind, each, eqNaN, extend, hasOwn, indexOf, isArrayLike, keys } from 'zrender/src/core/util';
 import { cloneValue } from 'zrender/src/animation/Animator';
 import Displayable, { DisplayableProps } from 'zrender/src/graphic/Displayable';
 import Model from '../model/Model';
@@ -53,7 +53,7 @@ const TRANSFORM_PROPS = keys(TRANSFORM_PROPS_MAP);
 const transformPropNamesStr = TRANSFORM_PROPS.join(', ');
 
 // '' means root
-const ELEMENT_TRANSITION_PROPS = ['', 'style', 'shape', 'extra'] as const;
+export const ELEMENT_ANIMATABLE_PROPS = ['', 'style', 'shape', 'extra'] as const;
 
 export type TransitionProps = string | string[];
 export type ElementRootTransitionProp = TransformProp | 'shape' | 'extra' | 'style';
@@ -147,8 +147,7 @@ export function applyUpdateTransition(
     if (hasAnimation) {
         if (isInit) {
             const enterFromProps: ElementProps = {};
-            for (let i = 0; i < ELEMENT_TRANSITION_PROPS.length; i++) {
-                const propName = ELEMENT_TRANSITION_PROPS[i];
+            each(ELEMENT_ANIMATABLE_PROPS, propName => {
                 const prop: TransitionOptionMixin = propName ? elOption[propName] : elOption;
                 if (prop && prop.enterFrom) {
                     if (propName) {
@@ -156,7 +155,7 @@ export function applyUpdateTransition(
                     }
                     extend(propName ? (enterFromProps as any)[propName] : enterFromProps, prop.enterFrom);
                 }
-            }
+            });
             initProps(el, enterFromProps, animatableModel, {
                 dataIndex: dataIndex || 0, isFrom: true
             });
@@ -174,8 +173,8 @@ export function applyUpdateTransition(
 export function updateLeaveTo(el: Element, elOption: TransitionElementOption) {
     // Try merge to previous set leaveTo
     let leaveToProps: ElementProps = transitionInnerStore(el).leaveToProps;
-    for (let i = 0; i < ELEMENT_TRANSITION_PROPS.length; i++) {
-        const propName = ELEMENT_TRANSITION_PROPS[i];
+    for (let i = 0; i < ELEMENT_ANIMATABLE_PROPS.length; i++) {
+        const propName = ELEMENT_ANIMATABLE_PROPS[i];
         const prop: TransitionOptionMixin = propName ? elOption[propName] : elOption;
         if (prop && prop.leaveTo) {
             if (!leaveToProps) {
@@ -303,8 +302,6 @@ function applyMiscProps(
 // Use it to avoid it be exposed to user.
 const tmpDuringScope = {} as {
     el: Element;
-    isShapeDirty: boolean;
-    isStyleDirty: boolean;
 };
 const transitionDuringAPI: TransitionDuringAPI = {
     // Usually other props do not need to be changed in animation during.
@@ -325,10 +322,10 @@ const transitionDuringAPI: TransitionDuringAPI = {
         if (__DEV__) {
             assertNotReserved(key);
         }
-        const shape = (tmpDuringScope.el as Path).shape
-            || ((tmpDuringScope.el as Path).shape = {});
+        const el = tmpDuringScope.el as Path;
+        const shape = el.shape || (el.shape = {});
         shape[key] = val;
-        tmpDuringScope.isShapeDirty = true;
+        el.dirtyShape && el.dirtyShape();
         return this;
     },
     getShape(key: any): any {
@@ -344,7 +341,8 @@ const transitionDuringAPI: TransitionDuringAPI = {
         if (__DEV__) {
             assertNotReserved(key);
         }
-        const style = (tmpDuringScope.el as Displayable).style;
+        const el = tmpDuringScope.el as Displayable;
+        const style = el.style;
         if (style) {
             if (__DEV__) {
                 if (eqNaN(val)) {
@@ -352,7 +350,7 @@ const transitionDuringAPI: TransitionDuringAPI = {
                 }
             }
             style[key] = val;
-            tmpDuringScope.isStyleDirty = true;
+            el.dirtyStyle && el.dirtyStyle();
         }
         return this;
     },
@@ -427,21 +425,10 @@ function duringCall(
     }
 
     tmpDuringScope.el = el;
-    tmpDuringScope.isShapeDirty = false;
-    tmpDuringScope.isStyleDirty = false;
 
     // Give no `this` to user in "during" calling.
     scopeUserDuring(transitionDuringAPI);
 
-    if (tmpDuringScope.isShapeDirty && (el as Path).dirtyShape) {
-        (el as Path).dirtyShape();
-    }
-    if (tmpDuringScope.isStyleDirty && (el as Displayable).dirtyStyle) {
-        (el as Displayable).dirtyStyle();
-    }
-    // markRedraw() will be called by default in during.
-    // FIXME `this.markRedraw();` directly ?
-
     // FIXME: if in future meet the case that some prop will be both modified in `during` and `state`,
     // consider the issue that the prop might be incorrect when return to "normal" state.
 }
diff --git a/src/component/graphic/GraphicModel.ts b/src/component/graphic/GraphicModel.ts
index 2a64d74..6691324 100644
--- a/src/component/graphic/GraphicModel.ts
+++ b/src/component/graphic/GraphicModel.ts
@@ -32,11 +32,13 @@ import ComponentModel from '../../model/Component';
 import Element, { ElementTextConfig } from 'zrender/src/Element';
 import Displayable from 'zrender/src/graphic/Displayable';
 import { PathProps } from 'zrender/src/graphic/Path';
-import { ImageStyleProps } from 'zrender/src/graphic/Image';
+import { ImageStyleProps, ImageProps } from 'zrender/src/graphic/Image';
+import { TextStyleProps, TextProps } from 'zrender/src/graphic/Text';
 import GlobalModel from '../../model/Global';
-import { TextStyleProps } from 'zrender/src/graphic/Text';
 import { copyLayoutParams, mergeLayoutParam } from '../../util/layout';
 import { ElementTransitionOptionMixin, TransitionOptionMixin } from '../../animation/customGraphicTransition';
+import { ElementKeyframeAnimationOption } from '../../animation/customGraphicKeyframeAnimation';
+import { GroupProps } from 'zrender/src/graphic/Group';
 
 interface GraphicComponentBaseElementOption extends
     Partial<Pick<
@@ -112,6 +114,7 @@ interface GraphicComponentBaseElementOption extends
     $action?: 'merge' | 'replace' | 'remove';
 
     tooltip?: CommonTooltipOption<unknown>;
+
 };
 
 
@@ -147,13 +150,17 @@ export interface GraphicComponentGroupOption
     // TODO: Can only set focus, blur on the root element.
     // children: Omit<GraphicComponentElementOption, 'focus' | 'blurScope'>[];
     children: GraphicComponentElementOption[];
+
+    animation?: ElementKeyframeAnimationOption<GroupProps>
 };
 export interface GraphicComponentZRPathOption extends GraphicComponentDisplayableOption {
     shape?: PathProps['shape'] & TransitionOptionMixin;
+    animation?: ElementKeyframeAnimationOption<PathProps>;
 }
 export interface GraphicComponentImageOption extends GraphicComponentDisplayableOption {
     type?: 'image';
     style?: ImageStyleProps & TransitionOptionMixin;
+    animation?: ElementKeyframeAnimationOption<ImageProps>;
 }
 // TODO: states?
 // interface GraphicComponentImageOptionOnState extends GraphicComponentDisplayableOptionOnState {
@@ -163,6 +170,7 @@ interface GraphicComponentTextOption
     extends Omit<GraphicComponentDisplayableOption, 'textContent' | 'textConfig'> {
     type?: 'text';
     style?: TextStyleProps;
+    animation?: ElementKeyframeAnimationOption<TextProps>;
 }
 export type GraphicComponentElementOption =
     GraphicComponentGroupOption |
diff --git a/src/component/graphic/GraphicView.ts b/src/component/graphic/GraphicView.ts
index 6b8d25f..05e295b 100644
--- a/src/component/graphic/GraphicView.ts
+++ b/src/component/graphic/GraphicView.ts
@@ -45,6 +45,7 @@ import {
     updateLeaveTo
 } from '../../animation/customGraphicTransition';
 import { updateProps } from '../../animation/basicTrasition';
+import { applyKeyframeAnimation } from '../../animation/customGraphicKeyframeAnimation';
 
 const nonShapeGraphicElements = {
     // Reserved but not supported in graphic component.
@@ -226,6 +227,10 @@ export class GraphicComponentView extends ComponentView {
                     itemName: el.name,
                     itemTooltipOption: elOption.tooltip
                 });
+
+                if (elOption.animation) {
+                    applyKeyframeAnimation(el, elOption.animation);
+                }
             }
         });
     }
diff --git a/src/util/types.ts b/src/util/types.ts
index e6ccb58..25ca23c 100644
--- a/src/util/types.ts
+++ b/src/util/types.ts
@@ -756,60 +756,6 @@ export interface ColorPaletteOptionMixin {
     color?: ZRColor | ZRColor[]
     colorLayer?: ZRColor[][]
 }
-
-export interface AriaLabelOption {
-    enabled?: boolean;
-    description?: string;
-    general?: {
-        withTitle?: string;
-        withoutTitle?: string;
-    };
-    series?: {
-        maxCount?: number;
-        single?: {
-            prefix?: string;
-            withName?: string;
-            withoutName?: string;
-        };
-        multiple?: {
-            prefix?: string;
-            withName?: string;
-            withoutName?: string;
-            separator?: {
-                middle?: string;
-                end?: string;
-            }
-        }
-    };
-    data?: {
-        maxCount?: number;
-        allData?: string;
-        partialData?: string;
-        withName?: string;
-        withoutName?: string;
-        separator?: {
-            middle?: string;
-            end?: string;
-        }
-    }
-}
-
-// Extending is for compating ECharts 4
-export interface AriaOption extends AriaLabelOption {
-    mainType?: 'aria';
-
-    enabled?: boolean;
-    label?: AriaLabelOption;
-    decal?: {
-        show?: boolean;
-        decals?: DecalObject | DecalObject[];
-    };
-}
-
-export interface AriaOptionMixin {
-    aria?: AriaOption
-}
-
 /**
  * Mixin of option set to control the box layout of each component.
  */
@@ -865,6 +811,12 @@ export interface AnimationOption {
     delay?: number
     // additive?: boolean
 }
+export interface ElementAnimationOption extends AnimationOption {
+    // Animation configuration of enter, update and leave transition.
+    enter?: AnimationOption
+    update?: AnimationOption
+    leave?: AnimationOption
+}
 /**
  * Mixin of option set to control the animation of series.
  */
@@ -1709,3 +1661,58 @@ export interface SeriesEncodeOptionMixin {
 }
 
 export type SeriesEncodableModel = SeriesModel<SeriesOption & SeriesEncodeOptionMixin>;
+
+
+// TODO Move to aria component
+export interface AriaLabelOption {
+    enabled?: boolean;
+    description?: string;
+    general?: {
+        withTitle?: string;
+        withoutTitle?: string;
+    };
+    series?: {
+        maxCount?: number;
+        single?: {
+            prefix?: string;
+            withName?: string;
+            withoutName?: string;
+        };
+        multiple?: {
+            prefix?: string;
+            withName?: string;
+            withoutName?: string;
+            separator?: {
+                middle?: string;
+                end?: string;
+            }
+        }
+    };
+    data?: {
+        maxCount?: number;
+        allData?: string;
+        partialData?: string;
+        withName?: string;
+        withoutName?: string;
+        separator?: {
+            middle?: string;
+            end?: string;
+        }
+    }
+}
+
+// Extending is for compating ECharts 4
+export interface AriaOption extends AriaLabelOption {
+    mainType?: 'aria';
+
+    enabled?: boolean;
+    label?: AriaLabelOption;
+    decal?: {
+        show?: boolean;
+        decals?: DecalObject | DecalObject[];
+    };
+}
+
+export interface AriaOptionMixin {
+    aria?: AriaOption
+}
diff --git a/test/graphic-animation.html b/test/graphic-animation.html
new file mode 100644
index 0000000..3647909
--- /dev/null
+++ b/test/graphic-animation.html
@@ -0,0 +1,95 @@
+<!DOCTYPE html>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+
+<html>
+    <head>
+        <meta charset="utf-8">
+        <meta name="viewport" content="width=device-width, initial-scale=1" />
+        <script src="lib/simpleRequire.js"></script>
+        <script src="lib/config.js"></script>
+        <script src="lib/jquery.min.js"></script>
+        <script src="lib/facePrint.js"></script>
+        <script src="lib/testHelper.js"></script>
+        <!-- <script src="ut/lib/canteen.js"></script> -->
+        <link rel="stylesheet" href="lib/reset.css" />
+    </head>
+    <body>
+        <style>
+        </style>
+
+
+
+        <div id="main0"></div>
+        <div id="main1"></div>
+        <div id="main2"></div>
+
+
+
+
+        <script>
+        require(['echarts'/*, 'map/js/china' */], function (echarts) {
+            var option = {
+                graphic: {
+                    elements: [{
+                        type: 'circle',
+                        x: 100,
+                        y: 100,
+                        shape: {
+                            cx: 0,
+                            cy: 0,
+                            r: 50
+                        },
+                        animation: {
+                            duration: 1000,
+                            loop: true,
+                            keyframes: [{
+                                percent: 0,
+                                x: 100
+                            }, {
+                                percent: 0.5,
+                                easing: 'sinusoidalInOut',
+                                x: 500
+                            }, {
+                                percent: 1,
+                                easing: 'sinusoidalInOut',
+                                x: 100
+                            }]
+                        },
+                        style: {
+                            fill: 'orange'
+                        }
+                    }]
+                }
+            }
+
+            var chart = testHelper.create(echarts, 'main0', {
+                title: [
+                    'Basic Keyframe Animation'
+                ],
+                option: option
+            });
+
+        });
+        </script>
+
+    </body>
+</html>
+
diff --git a/test/graphic-transition.html b/test/graphic-transition.html
index 497e352..5fe22af 100644
--- a/test/graphic-transition.html
+++ b/test/graphic-transition.html
@@ -234,7 +234,7 @@ under the License.
 
             var chart = testHelper.create(echarts, 'main2', {
                 title: [
-                    'Enter transition'
+                    'Enter / Leave transition'
                 ],
                 option: option,
                 buttons: [
@@ -253,6 +253,13 @@ under the License.
                                         r: 0
                                     }
                                 },
+                                leaveTo: {
+                                    x: 500,
+                                    y: 100,
+                                    shape: {
+                                        r: 0
+                                    }
+                                },
                                 shape: {
                                     cx: 0,
                                     cy: 0,
@@ -266,9 +273,21 @@ under the License.
                         }
                     },
                     {
-                        text: 'Remove',
+                        text: 'Replace',
                         onclick() {
                             elements[1] = {
+                                type: 'circle',
+                                name: 'center',
+                                $action: 'replace',
+                                x: 300,
+                                y: 100,
+                                enterFrom: {
+                                    x: 0,
+                                    y: 100,
+                                    shape: {
+                                        r: 0
+                                    }
+                                },
                                 leaveTo: {
                                     x: 500,
                                     y: 100,
@@ -276,6 +295,22 @@ under the License.
                                         r: 0
                                     }
                                 },
+                                shape: {
+                                    cx: 0,
+                                    cy: 0,
+                                    r: 80
+                                },
+                                style: {
+                                    fill: echarts.color.random()
+                                }
+                            };
+                            chart.setOption(option)
+                        }
+                    },
+                    {
+                        text: 'Remove',
+                        onclick() {
+                            elements[1] = {
                                 $action: 'remove'
                             };
                             chart.setOption(option)
@@ -286,22 +321,6 @@ under the License.
         });
         </script>
 
-        <script>
-        require(['echarts'/*, 'map/js/china' */], function (echarts) {
-            var option;
-
-            // Leave transition
-        });
-        </script>
-
-        <script>
-        require(['echarts'/*, 'map/js/china' */], function (echarts) {
-            var option;
-
-            // Replace transition
-        });
-        </script>
-
 
         <script>
             require(['echarts'/*, 'map/js/china' */], function (echarts) {

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