Blame view

天文台pc/tianwentai-ui/node_modules/framer-motion/dist/es/animation/sequence/create.mjs 11.6 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  import { isMotionValue, defaultOffset, isGenerator, createGeneratorEasing, fillOffset } from 'motion-dom';
  import { progress, secondsToMilliseconds, invariant, getEasingForSegment } from 'motion-utils';
  import { resolveSubjects } from '../animate/resolve-subjects.mjs';
  import { calculateRepeatDuration } from './utils/calc-repeat-duration.mjs';
  import { calcNextTime } from './utils/calc-time.mjs';
  import { addKeyframes } from './utils/edit.mjs';
  import { normalizeTimes } from './utils/normalize-times.mjs';
  import { compareByTime } from './utils/sort.mjs';
  
  const defaultSegmentEasing = "easeInOut";
  const MAX_REPEAT = 20;
  function createAnimationsFromSequence(sequence, { defaultTransition = {}, ...sequenceTransition } = {}, scope, generators) {
      const defaultDuration = defaultTransition.duration || 0.3;
      const animationDefinitions = new Map();
      const sequences = new Map();
      const elementCache = {};
      const timeLabels = new Map();
      let prevTime = 0;
      let currentTime = 0;
      let totalDuration = 0;
      /**
       * Build the timeline by mapping over the sequence array and converting
       * the definitions into keyframes and offsets with absolute time values.
       * These will later get converted into relative offsets in a second pass.
       */
      for (let i = 0; i < sequence.length; i++) {
          const segment = sequence[i];
          /**
           * If this is a timeline label, mark it and skip the rest of this iteration.
           */
          if (typeof segment === "string") {
              timeLabels.set(segment, currentTime);
              continue;
          }
          else if (!Array.isArray(segment)) {
              timeLabels.set(segment.name, calcNextTime(currentTime, segment.at, prevTime, timeLabels));
              continue;
          }
          let [subject, keyframes, transition = {}] = segment;
          /**
           * If a relative or absolute time value has been specified we need to resolve
           * it in relation to the currentTime.
           */
          if (transition.at !== undefined) {
              currentTime = calcNextTime(currentTime, transition.at, prevTime, timeLabels);
          }
          /**
           * Keep track of the maximum duration in this definition. This will be
           * applied to currentTime once the definition has been parsed.
           */
          let maxDuration = 0;
          const resolveValueSequence = (valueKeyframes, valueTransition, valueSequence, elementIndex = 0, numSubjects = 0) => {
              const valueKeyframesAsList = keyframesAsList(valueKeyframes);
              const { delay = 0, times = defaultOffset(valueKeyframesAsList), type = defaultTransition.type || "keyframes", repeat, repeatType, repeatDelay = 0, ...remainingTransition } = valueTransition;
              let { ease = defaultTransition.ease || "easeOut", duration } = valueTransition;
              /**
               * Resolve stagger() if defined.
               */
              const calculatedDelay = typeof delay === "function"
                  ? delay(elementIndex, numSubjects)
                  : delay;
              /**
               * If this animation should and can use a spring, generate a spring easing function.
               */
              const numKeyframes = valueKeyframesAsList.length;
              const createGenerator = isGenerator(type)
                  ? type
                  : generators?.[type || "keyframes"];
              if (numKeyframes <= 2 && createGenerator) {
                  /**
                   * As we're creating an easing function from a spring,
                   * ideally we want to generate it using the real distance
                   * between the two keyframes. However this isn't always
                   * possible - in these situations we use 0-100.
                   */
                  let absoluteDelta = 100;
                  if (numKeyframes === 2 &&
                      isNumberKeyframesArray(valueKeyframesAsList)) {
                      const delta = valueKeyframesAsList[1] - valueKeyframesAsList[0];
                      absoluteDelta = Math.abs(delta);
                  }
                  const springTransition = {
                      ...defaultTransition,
                      ...remainingTransition,
                  };
                  if (duration !== undefined) {
                      springTransition.duration = secondsToMilliseconds(duration);
                  }
                  const springEasing = createGeneratorEasing(springTransition, absoluteDelta, createGenerator);
                  ease = springEasing.ease;
                  duration = springEasing.duration;
              }
              duration ?? (duration = defaultDuration);
              const startTime = currentTime + calculatedDelay;
              /**
               * If there's only one time offset of 0, fill in a second with length 1
               */
              if (times.length === 1 && times[0] === 0) {
                  times[1] = 1;
              }
              /**
               * Fill out if offset if fewer offsets than keyframes
               */
              const remainder = times.length - valueKeyframesAsList.length;
              remainder > 0 && fillOffset(times, remainder);
              /**
               * If only one value has been set, ie [1], push a null to the start of
               * the keyframe array. This will let us mark a keyframe at this point
               * that will later be hydrated with the previous value.
               */
              valueKeyframesAsList.length === 1 &&
                  valueKeyframesAsList.unshift(null);
              /**
               * Handle repeat options
               */
              if (repeat) {
                  invariant(repeat < MAX_REPEAT, "Repeat count too high, must be less than 20", "repeat-count-high");
                  duration = calculateRepeatDuration(duration, repeat);
                  const originalKeyframes = [...valueKeyframesAsList];
                  const originalTimes = [...times];
                  ease = Array.isArray(ease) ? [...ease] : [ease];
                  const originalEase = [...ease];
                  for (let repeatIndex = 0; repeatIndex < repeat; repeatIndex++) {
                      valueKeyframesAsList.push(...originalKeyframes);
                      for (let keyframeIndex = 0; keyframeIndex < originalKeyframes.length; keyframeIndex++) {
                          times.push(originalTimes[keyframeIndex] + (repeatIndex + 1));
                          ease.push(keyframeIndex === 0
                              ? "linear"
                              : getEasingForSegment(originalEase, keyframeIndex - 1));
                      }
                  }
                  normalizeTimes(times, repeat);
              }
              const targetTime = startTime + duration;
              /**
               * Add keyframes, mapping offsets to absolute time.
               */
              addKeyframes(valueSequence, valueKeyframesAsList, ease, times, startTime, targetTime);
              maxDuration = Math.max(calculatedDelay + duration, maxDuration);
              totalDuration = Math.max(targetTime, totalDuration);
          };
          if (isMotionValue(subject)) {
              const subjectSequence = getSubjectSequence(subject, sequences);
              resolveValueSequence(keyframes, transition, getValueSequence("default", subjectSequence));
          }
          else {
              const subjects = resolveSubjects(subject, keyframes, scope, elementCache);
              const numSubjects = subjects.length;
              /**
               * For every element in this segment, process the defined values.
               */
              for (let subjectIndex = 0; subjectIndex < numSubjects; subjectIndex++) {
                  /**
                   * Cast necessary, but we know these are of this type
                   */
                  keyframes = keyframes;
                  transition = transition;
                  const thisSubject = subjects[subjectIndex];
                  const subjectSequence = getSubjectSequence(thisSubject, sequences);
                  for (const key in keyframes) {
                      resolveValueSequence(keyframes[key], getValueTransition(transition, key), getValueSequence(key, subjectSequence), subjectIndex, numSubjects);
                  }
              }
          }
          prevTime = currentTime;
          currentTime += maxDuration;
      }
      /**
       * For every element and value combination create a new animation.
       */
      sequences.forEach((valueSequences, element) => {
          for (const key in valueSequences) {
              const valueSequence = valueSequences[key];
              /**
               * Arrange all the keyframes in ascending time order.
               */
              valueSequence.sort(compareByTime);
              const keyframes = [];
              const valueOffset = [];
              const valueEasing = [];
              /**
               * For each keyframe, translate absolute times into
               * relative offsets based on the total duration of the timeline.
               */
              for (let i = 0; i < valueSequence.length; i++) {
                  const { at, value, easing } = valueSequence[i];
                  keyframes.push(value);
                  valueOffset.push(progress(0, totalDuration, at));
                  valueEasing.push(easing || "easeOut");
              }
              /**
               * If the first keyframe doesn't land on offset: 0
               * provide one by duplicating the initial keyframe. This ensures
               * it snaps to the first keyframe when the animation starts.
               */
              if (valueOffset[0] !== 0) {
                  valueOffset.unshift(0);
                  keyframes.unshift(keyframes[0]);
                  valueEasing.unshift(defaultSegmentEasing);
              }
              /**
               * If the last keyframe doesn't land on offset: 1
               * provide one with a null wildcard value. This will ensure it
               * stays static until the end of the animation.
               */
              if (valueOffset[valueOffset.length - 1] !== 1) {
                  valueOffset.push(1);
                  keyframes.push(null);
              }
              if (!animationDefinitions.has(element)) {
                  animationDefinitions.set(element, {
                      keyframes: {},
                      transition: {},
                  });
              }
              const definition = animationDefinitions.get(element);
              definition.keyframes[key] = keyframes;
              /**
               * Exclude `type` from defaultTransition since springs have been
               * converted to duration-based easing functions in resolveValueSequence.
               * Including `type: "spring"` would cause JSAnimation to error when
               * the merged keyframes array has more than 2 keyframes.
               */
              const { type: _type, ...remainingDefaultTransition } = defaultTransition;
              definition.transition[key] = {
                  ...remainingDefaultTransition,
                  duration: totalDuration,
                  ease: valueEasing,
                  times: valueOffset,
                  ...sequenceTransition,
              };
          }
      });
      return animationDefinitions;
  }
  function getSubjectSequence(subject, sequences) {
      !sequences.has(subject) && sequences.set(subject, {});
      return sequences.get(subject);
  }
  function getValueSequence(name, sequences) {
      if (!sequences[name])
          sequences[name] = [];
      return sequences[name];
  }
  function keyframesAsList(keyframes) {
      return Array.isArray(keyframes) ? keyframes : [keyframes];
  }
  function getValueTransition(transition, key) {
      return transition && transition[key]
          ? {
              ...transition,
              ...transition[key],
          }
          : { ...transition };
  }
  const isNumber = (keyframe) => typeof keyframe === "number";
  const isNumberKeyframesArray = (keyframes) => keyframes.every(isNumber);
  
  export { createAnimationsFromSequence, getValueTransition };
  //# sourceMappingURL=create.mjs.map