motion-dom 12.4.11 → 12.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ import { MotionGlobalConfig } from 'motion-utils';
2
+ import { stepsOrder } from './order.mjs';
3
+ import { createRenderStep } from './render-step.mjs';
4
+
5
+ const maxElapsed = 40;
6
+ function createRenderBatcher(scheduleNextBatch, allowKeepAlive) {
7
+ let runNextFrame = false;
8
+ let useDefaultElapsed = true;
9
+ const state = {
10
+ delta: 0.0,
11
+ timestamp: 0.0,
12
+ isProcessing: false,
13
+ };
14
+ const flagRunNextFrame = () => (runNextFrame = true);
15
+ const steps = stepsOrder.reduce((acc, key) => {
16
+ acc[key] = createRenderStep(flagRunNextFrame, allowKeepAlive ? key : undefined);
17
+ return acc;
18
+ }, {});
19
+ const { read, resolveKeyframes, update, preRender, render, postRender } = steps;
20
+ const processBatch = () => {
21
+ const timestamp = MotionGlobalConfig.useManualTiming
22
+ ? state.timestamp
23
+ : performance.now();
24
+ runNextFrame = false;
25
+ if (!MotionGlobalConfig.useManualTiming) {
26
+ state.delta = useDefaultElapsed
27
+ ? 1000 / 60
28
+ : Math.max(Math.min(timestamp - state.timestamp, maxElapsed), 1);
29
+ }
30
+ state.timestamp = timestamp;
31
+ state.isProcessing = true;
32
+ // Unrolled render loop for better per-frame performance
33
+ read.process(state);
34
+ resolveKeyframes.process(state);
35
+ update.process(state);
36
+ preRender.process(state);
37
+ render.process(state);
38
+ postRender.process(state);
39
+ state.isProcessing = false;
40
+ if (runNextFrame && allowKeepAlive) {
41
+ useDefaultElapsed = false;
42
+ scheduleNextBatch(processBatch);
43
+ }
44
+ };
45
+ const wake = () => {
46
+ runNextFrame = true;
47
+ useDefaultElapsed = true;
48
+ if (!state.isProcessing) {
49
+ scheduleNextBatch(processBatch);
50
+ }
51
+ };
52
+ const schedule = stepsOrder.reduce((acc, key) => {
53
+ const step = steps[key];
54
+ acc[key] = (process, keepAlive = false, immediate = false) => {
55
+ if (!runNextFrame)
56
+ wake();
57
+ return step.schedule(process, keepAlive, immediate);
58
+ };
59
+ return acc;
60
+ }, {});
61
+ const cancel = (process) => {
62
+ for (let i = 0; i < stepsOrder.length; i++) {
63
+ steps[stepsOrder[i]].cancel(process);
64
+ }
65
+ };
66
+ return { schedule, cancel, state, steps };
67
+ }
68
+
69
+ export { createRenderBatcher };
@@ -0,0 +1,6 @@
1
+ import { noop } from 'motion-utils';
2
+ import { createRenderBatcher } from './batcher.mjs';
3
+
4
+ const { schedule: frame, cancel: cancelFrame, state: frameData, steps: frameSteps, } = /* @__PURE__ */ createRenderBatcher(typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : noop, true);
5
+
6
+ export { cancelFrame, frame, frameData, frameSteps };
@@ -0,0 +1,6 @@
1
+ import { createRenderBatcher } from './batcher.mjs';
2
+
3
+ const { schedule: microtask, cancel: cancelMicrotask } =
4
+ /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
5
+
6
+ export { cancelMicrotask, microtask };
@@ -0,0 +1,10 @@
1
+ const stepsOrder = [
2
+ "read", // Read
3
+ "resolveKeyframes", // Write/Read/Write/Read
4
+ "update", // Compute
5
+ "preRender", // Compute
6
+ "render", // Write
7
+ "postRender", // Compute
8
+ ];
9
+
10
+ export { stepsOrder };
@@ -0,0 +1,92 @@
1
+ import { statsBuffer } from '../stats/buffer.mjs';
2
+
3
+ function createRenderStep(runNextFrame, stepName) {
4
+ /**
5
+ * We create and reuse two queues, one to queue jobs for the current frame
6
+ * and one for the next. We reuse to avoid triggering GC after x frames.
7
+ */
8
+ let thisFrame = new Set();
9
+ let nextFrame = new Set();
10
+ /**
11
+ * Track whether we're currently processing jobs in this step. This way
12
+ * we can decide whether to schedule new jobs for this frame or next.
13
+ */
14
+ let isProcessing = false;
15
+ let flushNextFrame = false;
16
+ /**
17
+ * A set of processes which were marked keepAlive when scheduled.
18
+ */
19
+ const toKeepAlive = new WeakSet();
20
+ let latestFrameData = {
21
+ delta: 0.0,
22
+ timestamp: 0.0,
23
+ isProcessing: false,
24
+ };
25
+ let numCalls = 0;
26
+ function triggerCallback(callback) {
27
+ if (toKeepAlive.has(callback)) {
28
+ step.schedule(callback);
29
+ runNextFrame();
30
+ }
31
+ numCalls++;
32
+ callback(latestFrameData);
33
+ }
34
+ const step = {
35
+ /**
36
+ * Schedule a process to run on the next frame.
37
+ */
38
+ schedule: (callback, keepAlive = false, immediate = false) => {
39
+ const addToCurrentFrame = immediate && isProcessing;
40
+ const queue = addToCurrentFrame ? thisFrame : nextFrame;
41
+ if (keepAlive)
42
+ toKeepAlive.add(callback);
43
+ if (!queue.has(callback))
44
+ queue.add(callback);
45
+ return callback;
46
+ },
47
+ /**
48
+ * Cancel the provided callback from running on the next frame.
49
+ */
50
+ cancel: (callback) => {
51
+ nextFrame.delete(callback);
52
+ toKeepAlive.delete(callback);
53
+ },
54
+ /**
55
+ * Execute all schedule callbacks.
56
+ */
57
+ process: (frameData) => {
58
+ latestFrameData = frameData;
59
+ /**
60
+ * If we're already processing we've probably been triggered by a flushSync
61
+ * inside an existing process. Instead of executing, mark flushNextFrame
62
+ * as true and ensure we flush the following frame at the end of this one.
63
+ */
64
+ if (isProcessing) {
65
+ flushNextFrame = true;
66
+ return;
67
+ }
68
+ isProcessing = true;
69
+ [thisFrame, nextFrame] = [nextFrame, thisFrame];
70
+ // Execute this frame
71
+ thisFrame.forEach(triggerCallback);
72
+ /**
73
+ * If we're recording stats then
74
+ */
75
+ if (stepName && statsBuffer.value) {
76
+ statsBuffer.value.frameloop[stepName].push(numCalls);
77
+ }
78
+ numCalls = 0;
79
+ // Clear the frame so no callbacks remain. This is to avoid
80
+ // memory leaks should this render step not run for a while.
81
+ thisFrame.clear();
82
+ isProcessing = false;
83
+ if (flushNextFrame) {
84
+ flushNextFrame = false;
85
+ step.process(frameData);
86
+ }
87
+ },
88
+ };
89
+ return step;
90
+ }
91
+
92
+ export { createRenderStep };
@@ -0,0 +1,31 @@
1
+ import { MotionGlobalConfig } from 'motion-utils';
2
+ import { frameData } from './frame.mjs';
3
+
4
+ let now;
5
+ function clearTime() {
6
+ now = undefined;
7
+ }
8
+ /**
9
+ * An eventloop-synchronous alternative to performance.now().
10
+ *
11
+ * Ensures that time measurements remain consistent within a synchronous context.
12
+ * Usually calling performance.now() twice within the same synchronous context
13
+ * will return different values which isn't useful for animations when we're usually
14
+ * trying to sync animations to the same frame.
15
+ */
16
+ const time = {
17
+ now: () => {
18
+ if (now === undefined) {
19
+ time.set(frameData.isProcessing || MotionGlobalConfig.useManualTiming
20
+ ? frameData.timestamp
21
+ : performance.now());
22
+ }
23
+ return now;
24
+ },
25
+ set: (newTime) => {
26
+ now = newTime;
27
+ queueMicrotask(clearTime);
28
+ },
29
+ };
30
+
31
+ export { time };
package/dist/es/index.mjs CHANGED
@@ -7,16 +7,24 @@ export { NativeAnimationControls } from './animation/waapi/NativeAnimationContro
7
7
  export { attachTimeline } from './animation/waapi/utils/attach-timeline.mjs';
8
8
  export { cubicBezierAsString, isWaapiSupportedEasing, mapEasingToNativeEasing, supportedWaapiEasing } from './animation/waapi/utils/easing.mjs';
9
9
  export { generateLinearEasing } from './animation/waapi/utils/linear.mjs';
10
+ export { cancelFrame, frame, frameData, frameSteps } from './frameloop/frame.mjs';
11
+ export { createRenderBatcher } from './frameloop/batcher.mjs';
12
+ export { cancelMicrotask, microtask } from './frameloop/microtask.mjs';
13
+ export { time } from './frameloop/sync-time.mjs';
14
+ export { isDragActive, isDragging } from './gestures/drag/state/is-active.mjs';
15
+ export { setDragLock } from './gestures/drag/state/set-active.mjs';
10
16
  export { hover } from './gestures/hover.mjs';
11
17
  export { press } from './gestures/press/index.mjs';
12
18
  export { capturePointer } from './gestures/utils/capture-pointer.mjs';
19
+ export { isNodeOrChild } from './gestures/utils/is-node-or-child.mjs';
20
+ export { isPrimaryPointer } from './gestures/utils/is-primary-pointer.mjs';
21
+ export { recordStats } from './stats/index.mjs';
22
+ export { activeAnimations } from './stats/animation-count.mjs';
23
+ export { statsBuffer } from './stats/buffer.mjs';
13
24
  export { isBezierDefinition } from './utils/is-bezier-definition.mjs';
14
25
  export { resolveElements } from './utils/resolve-elements.mjs';
15
26
  export { supportsFlags } from './utils/supports/flags.mjs';
16
27
  export { supportsLinearEasing } from './utils/supports/linear-easing.mjs';
17
28
  export { supportsScrollTimeline } from './utils/supports/scroll-timeline.mjs';
29
+ export { MotionValue, collectMotionValues, motionValue } from './value/index.mjs';
18
30
  export { ViewTransitionBuilder, view } from './view/index.mjs';
19
- export { isDragActive, isDragging } from './gestures/drag/state/is-active.mjs';
20
- export { setDragLock } from './gestures/drag/state/set-active.mjs';
21
- export { isNodeOrChild } from './gestures/utils/is-node-or-child.mjs';
22
- export { isPrimaryPointer } from './gestures/utils/is-primary-pointer.mjs';
@@ -0,0 +1,7 @@
1
+ const activeAnimations = {
2
+ layout: 0,
3
+ mainThread: 0,
4
+ waapi: 0,
5
+ };
6
+
7
+ export { activeAnimations };
@@ -0,0 +1,6 @@
1
+ const statsBuffer = {
2
+ value: null,
3
+ addProjectionMetrics: null,
4
+ };
5
+
6
+ export { statsBuffer };
@@ -0,0 +1,113 @@
1
+ import { frame, cancelFrame, frameData } from '../frameloop/frame.mjs';
2
+ import { activeAnimations } from './animation-count.mjs';
3
+ import { statsBuffer } from './buffer.mjs';
4
+
5
+ function record() {
6
+ const { value } = statsBuffer;
7
+ if (value === null) {
8
+ cancelFrame(record);
9
+ return;
10
+ }
11
+ value.frameloop.rate.push(frameData.delta);
12
+ value.animations.mainThread.push(activeAnimations.mainThread);
13
+ value.animations.waapi.push(activeAnimations.waapi);
14
+ value.animations.layout.push(activeAnimations.layout);
15
+ }
16
+ function mean(values) {
17
+ return values.reduce((acc, value) => acc + value, 0) / values.length;
18
+ }
19
+ function summarise(values, calcAverage = mean) {
20
+ if (values.length === 0) {
21
+ return {
22
+ min: 0,
23
+ max: 0,
24
+ avg: 0,
25
+ };
26
+ }
27
+ return {
28
+ min: Math.min(...values),
29
+ max: Math.max(...values),
30
+ avg: calcAverage(values),
31
+ };
32
+ }
33
+ const msToFps = (ms) => Math.round(1000 / ms);
34
+ function clearStatsBuffer() {
35
+ statsBuffer.value = null;
36
+ statsBuffer.addProjectionMetrics = null;
37
+ }
38
+ function reportStats() {
39
+ const { value } = statsBuffer;
40
+ if (!value) {
41
+ throw new Error("Stats are not being measured");
42
+ }
43
+ clearStatsBuffer();
44
+ cancelFrame(record);
45
+ const summary = {
46
+ frameloop: {
47
+ rate: summarise(value.frameloop.rate),
48
+ read: summarise(value.frameloop.read),
49
+ resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
50
+ update: summarise(value.frameloop.update),
51
+ preRender: summarise(value.frameloop.preRender),
52
+ render: summarise(value.frameloop.render),
53
+ postRender: summarise(value.frameloop.postRender),
54
+ },
55
+ animations: {
56
+ mainThread: summarise(value.animations.mainThread),
57
+ waapi: summarise(value.animations.waapi),
58
+ layout: summarise(value.animations.layout),
59
+ },
60
+ layoutProjection: {
61
+ nodes: summarise(value.layoutProjection.nodes),
62
+ calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
63
+ calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
64
+ },
65
+ };
66
+ /**
67
+ * Convert the rate to FPS
68
+ */
69
+ const { rate } = summary.frameloop;
70
+ rate.min = msToFps(rate.min);
71
+ rate.max = msToFps(rate.max);
72
+ rate.avg = msToFps(rate.avg);
73
+ [rate.min, rate.max] = [rate.max, rate.min];
74
+ return summary;
75
+ }
76
+ function recordStats() {
77
+ if (statsBuffer.value) {
78
+ clearStatsBuffer();
79
+ throw new Error("Stats are already being measured");
80
+ }
81
+ const newStatsBuffer = statsBuffer;
82
+ newStatsBuffer.value = {
83
+ frameloop: {
84
+ rate: [],
85
+ read: [],
86
+ resolveKeyframes: [],
87
+ update: [],
88
+ preRender: [],
89
+ render: [],
90
+ postRender: [],
91
+ },
92
+ animations: {
93
+ mainThread: [],
94
+ waapi: [],
95
+ layout: [],
96
+ },
97
+ layoutProjection: {
98
+ nodes: [],
99
+ calculatedTargetDeltas: [],
100
+ calculatedProjections: [],
101
+ },
102
+ };
103
+ newStatsBuffer.addProjectionMetrics = (metrics) => {
104
+ const { layoutProjection } = newStatsBuffer.value;
105
+ layoutProjection.nodes.push(metrics.nodes);
106
+ layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
107
+ layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
108
+ };
109
+ frame.postRender(record, true);
110
+ return reportStats;
111
+ }
112
+
113
+ export { recordStats };
@@ -1,5 +1,5 @@
1
1
  import { memo } from 'motion-utils';
2
2
 
3
- const supportsScrollTimeline = memo(() => window.ScrollTimeline !== undefined);
3
+ const supportsScrollTimeline = /* @__PURE__ */ memo(() => window.ScrollTimeline !== undefined);
4
4
 
5
5
  export { supportsScrollTimeline };