customGraphicKeyframeAnimation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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'); // Restore
  54. el.attr(getStateToRestore(el));
  55. }
  56. export function applyKeyframeAnimation(el, animationOpts, animatableModel) {
  57. if (!animatableModel.isAnimationEnabled() || !animationOpts) {
  58. return;
  59. }
  60. if (isArray(animationOpts)) {
  61. each(animationOpts, function (singleAnimationOpts) {
  62. applyKeyframeAnimation(el, singleAnimationOpts, animatableModel);
  63. });
  64. return;
  65. }
  66. var keyframes = animationOpts.keyframes;
  67. var duration = animationOpts.duration;
  68. if (animatableModel && duration == null) {
  69. // Default to use duration of config.
  70. // NOTE: animation config from payload will be ignored because they are mainly for transitions.
  71. var config = getAnimationConfig('enter', animatableModel, 0);
  72. duration = config && config.duration;
  73. }
  74. if (!keyframes || !duration) {
  75. return;
  76. }
  77. var stateToRestore = getStateToRestore(el);
  78. each(ELEMENT_ANIMATABLE_PROPS, function (targetPropName) {
  79. if (targetPropName && !el[targetPropName]) {
  80. return;
  81. }
  82. var animator;
  83. var endFrameIsSet = false; // Sort keyframes by percent.
  84. keyframes.sort(function (a, b) {
  85. return a.percent - b.percent;
  86. });
  87. each(keyframes, function (kf) {
  88. // Stop current animation.
  89. var animators = el.animators;
  90. var kfValues = targetPropName ? kf[targetPropName] : kf;
  91. if (process.env.NODE_ENV !== 'production') {
  92. if (kf.percent >= 1) {
  93. endFrameIsSet = true;
  94. }
  95. }
  96. if (!kfValues) {
  97. return;
  98. }
  99. var propKeys = keys(kfValues);
  100. if (!targetPropName) {
  101. // PENDING performance?
  102. propKeys = filter(propKeys, function (key) {
  103. return indexOf(KEYFRAME_EXCLUDE_KEYS, key) < 0;
  104. });
  105. }
  106. if (!propKeys.length) {
  107. return;
  108. }
  109. if (!animator) {
  110. animator = el.animate(targetPropName, animationOpts.loop, true);
  111. animator.scope = 'keyframe';
  112. }
  113. for (var i = 0; i < animators.length; i++) {
  114. // Stop all other animation that is not keyframe.
  115. if (animators[i] !== animator && animators[i].targetName === animator.targetName) {
  116. animators[i].stopTracks(propKeys);
  117. }
  118. }
  119. targetPropName && (stateToRestore[targetPropName] = stateToRestore[targetPropName] || {});
  120. var savedTarget = targetPropName ? stateToRestore[targetPropName] : stateToRestore;
  121. each(propKeys, function (key) {
  122. // Save original value.
  123. savedTarget[key] = ((targetPropName ? el[targetPropName] : el) || {})[key];
  124. });
  125. animator.whenWithKeys(duration * kf.percent, kfValues, propKeys, kf.easing);
  126. });
  127. if (!animator) {
  128. return;
  129. }
  130. if (process.env.NODE_ENV !== 'production') {
  131. if (!endFrameIsSet) {
  132. warn('End frame with percent: 1 is missing in the keyframeAnimation.', true);
  133. }
  134. }
  135. animator.delay(animationOpts.delay || 0).duration(duration).start(animationOpts.easing);
  136. });
  137. }