customGraphicKeyframeAnimation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { keys, filter, each, isArray, indexOf } from 'zrender/lib/core/util.js';
  41. import { ELEMENT_ANIMATABLE_PROPS } from './customGraphicTransition.js';
  42. import { getAnimationConfig } from './basicTransition.js';
  43. import { warn } from '../util/log.js';
  44. import { makeInner } from '../util/model.js';
  45. var getStateToRestore = makeInner();
  46. var KEYFRAME_EXCLUDE_KEYS = ['percent', 'easing', 'shape', 'style', 'extra'];
  47. /**
  48. * Stop previous keyframe animation and restore the attributes.
  49. * Avoid new keyframe animation starts with wrong internal state when the percent: 0 is not set.
  50. */
  51. export function stopPreviousKeyframeAnimationAndRestore(el) {
  52. // Stop previous keyframe animation.
  53. el.stopAnimation('keyframe');
  54. // Restore
  55. el.attr(getStateToRestore(el));
  56. }
  57. export function applyKeyframeAnimation(el, animationOpts, animatableModel) {
  58. if (!animatableModel.isAnimationEnabled() || !animationOpts) {
  59. return;
  60. }
  61. if (isArray(animationOpts)) {
  62. each(animationOpts, function (singleAnimationOpts) {
  63. applyKeyframeAnimation(el, singleAnimationOpts, animatableModel);
  64. });
  65. return;
  66. }
  67. var keyframes = animationOpts.keyframes;
  68. var duration = animationOpts.duration;
  69. if (animatableModel && duration == null) {
  70. // Default to use duration of config.
  71. // NOTE: animation config from payload will be ignored because they are mainly for transitions.
  72. var config = getAnimationConfig('enter', animatableModel, 0);
  73. duration = config && config.duration;
  74. }
  75. if (!keyframes || !duration) {
  76. return;
  77. }
  78. var stateToRestore = getStateToRestore(el);
  79. each(ELEMENT_ANIMATABLE_PROPS, function (targetPropName) {
  80. if (targetPropName && !el[targetPropName]) {
  81. return;
  82. }
  83. var animator;
  84. var endFrameIsSet = false;
  85. // Sort keyframes by percent.
  86. keyframes.sort(function (a, b) {
  87. return a.percent - b.percent;
  88. });
  89. each(keyframes, function (kf) {
  90. // Stop current animation.
  91. var animators = el.animators;
  92. var kfValues = targetPropName ? kf[targetPropName] : kf;
  93. if (process.env.NODE_ENV !== 'production') {
  94. if (kf.percent >= 1) {
  95. endFrameIsSet = true;
  96. }
  97. }
  98. if (!kfValues) {
  99. return;
  100. }
  101. var propKeys = keys(kfValues);
  102. if (!targetPropName) {
  103. // PENDING performance?
  104. propKeys = filter(propKeys, function (key) {
  105. return indexOf(KEYFRAME_EXCLUDE_KEYS, key) < 0;
  106. });
  107. }
  108. if (!propKeys.length) {
  109. return;
  110. }
  111. if (!animator) {
  112. animator = el.animate(targetPropName, animationOpts.loop, true);
  113. animator.scope = 'keyframe';
  114. }
  115. for (var i = 0; i < animators.length; i++) {
  116. // Stop all other animation that is not keyframe.
  117. if (animators[i] !== animator && animators[i].targetName === animator.targetName) {
  118. animators[i].stopTracks(propKeys);
  119. }
  120. }
  121. targetPropName && (stateToRestore[targetPropName] = stateToRestore[targetPropName] || {});
  122. var savedTarget = targetPropName ? stateToRestore[targetPropName] : stateToRestore;
  123. each(propKeys, function (key) {
  124. // Save original value.
  125. savedTarget[key] = ((targetPropName ? el[targetPropName] : el) || {})[key];
  126. });
  127. animator.whenWithKeys(duration * kf.percent, kfValues, propKeys, kf.easing);
  128. });
  129. if (!animator) {
  130. return;
  131. }
  132. if (process.env.NODE_ENV !== 'production') {
  133. if (!endFrameIsSet) {
  134. warn('End frame with percent: 1 is missing in the keyframeAnimation.', true);
  135. }
  136. }
  137. animator.delay(animationOpts.delay || 0).duration(duration).start(animationOpts.easing);
  138. });
  139. }