Skip to content

Commit f73cbd7

Browse files
committed
Latest
1 parent d743b79 commit f73cbd7

22 files changed

Lines changed: 1032 additions & 510 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ Undocumented APIs should be considered internal and may change without warning.
88

99
### Added
1010

11-
- `animate` for Three.js supporting objects, materials, shader uniforms, TSL uniform nodes and more.
12-
- `animate` for vgpu supporting shared uniforms, `Effect`/`Draw`/`Compute` bindings (`"params.time"`), scene nodes, cameras, lights, materials, orbit controls, CSS colors and vector components.
11+
- `animate.addEffect()` for registering effects that lets `animate` drive non-DOM subjects alongside DOM elements.
12+
- `threeEffect` (`motion/three`) supporting Three.js objects, materials, shader uniforms, TSL uniform nodes and more.
13+
- `vgpuEffect` (`motion/vgpu`) supporting shared uniforms, `Effect`/`Draw`/`Compute` bindings (`"params.time"`), scene nodes, cameras, lights, materials, orbit controls, CSS colors and vector components.
14+
- `createEffect` now accepts `test`, `read` and `step` options and exposes bound motion values via `effect.get()`.
1315

1416
### Changed
1517

‎dev/html/src/three-animate.js‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { cancelFrame, frame } from "motion"
2-
import { animate } from "motion/three"
1+
import { animate, cancelFrame, frame } from "motion"
2+
import { threeEffect } from "motion/three"
33
import * as THREE from "three"
44
import "./gpu-adapters.css"
55

6+
animate.addEffect(threeEffect)
7+
68
const canvas = document.querySelector("canvas")
79
const status = document.querySelector("#status")
810
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true })
@@ -95,16 +97,14 @@ document.querySelector("#uniform").addEventListener("click", () => {
9597
uniformActive = !uniformActive
9698
status.textContent = "Animating shader uniforms"
9799

98-
Promise.all([
99-
animate(uniforms.progress, uniformActive ? 1 : 0, {
100-
duration: 1,
101-
ease: "easeInOut",
102-
}),
103-
animate(uniforms.tint, uniformActive ? "#fbbf24" : "#22d3ee", {
104-
duration: 1,
105-
ease: "easeInOut",
106-
}),
107-
]).then(() => {
100+
animate(
101+
uniforms,
102+
{
103+
progress: uniformActive ? 1 : 0,
104+
tint: uniformActive ? "#fbbf24" : "#22d3ee",
105+
},
106+
{ duration: 1, ease: "easeInOut" }
107+
).then(() => {
108108
status.textContent = "Uniform animation complete"
109109
})
110110
})

‎dev/html/src/three-effects.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { cancelFrame, frame, motionValue, transformValue } from "motion"
2-
import { animate, objectEffect, uniformEffect } from "motion/three"
1+
import {
2+
animate,
3+
cancelFrame,
4+
frame,
5+
motionValue,
6+
transformValue,
7+
} from "motion"
8+
import { threeEffect } from "motion/three"
39
import * as THREE from "three"
410
import "./gpu-adapters.css"
511

12+
animate.addEffect(threeEffect)
13+
614
const canvas = document.querySelector("canvas")
715
const positionInput = document.querySelector("#position")
816
const progressInput = document.querySelector("#progress")
@@ -55,8 +63,8 @@ const rotateY = motionValue(0)
5563
const progress = motionValue(0)
5664
const scale = transformValue(() => 0.9 + progress.get() * 0.3)
5765

58-
const cancelObjectEffect = objectEffect(mesh, { x, rotateY, scale })
59-
const cancelUniformEffect = uniformEffect(uniforms, { progress })
66+
const cancelObjectEffect = threeEffect(mesh, { x, rotateY, scale })
67+
const cancelUniformEffect = threeEffect(uniforms, { progress })
6068

6169
function updateStatus() {
6270
status.textContent = `x ${x.get().toFixed(2)} · progress ${progress

‎dev/html/src/three-tsl.js‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { cancelFrame, frame } from "motion"
2-
import { animate } from "motion/three"
1+
import { animate, cancelFrame, frame, motionValue } from "motion"
2+
import { threeEffect } from "motion/three"
33
import * as THREE from "three"
44
import { color, mix, positionLocal, sin, uniform, uv, vec3 } from "three/tsl"
55
import { MeshBasicNodeMaterial, MeshStandardNodeMaterial, WebGPURenderer } from "three/webgpu"
@@ -9,6 +9,8 @@ const stage = document.querySelector(".stage")
99
const canvas = document.querySelector("canvas")
1010
const status = document.querySelector("#status")
1111

12+
animate.addEffect(threeEffect)
13+
1214
async function start() {
1315
const renderer = new WebGPURenderer({ canvas, antialias: true })
1416
await renderer.init()
@@ -44,10 +46,12 @@ async function start() {
4446

4547
/**
4648
* GPU fan-out: a single progress uniform drives colour and vertex
47-
* displacement across the whole plane in-shader. Motion animates one
48-
* scalar per frame; TSL amplifies it per vertex/fragment.
49+
* displacement across the whole plane in-shader. A motion value wraps
50+
* the uniform node via threeEffect; TSL amplifies it per vertex/fragment.
4951
*/
5052
const progress = uniform(0)
53+
const progressValue = motionValue(0)
54+
threeEffect(progress, { value: progressValue })
5155
const fanoutMaterial = new MeshBasicNodeMaterial()
5256
const wave = sin(uv().x.mul(9).add(progress.mul(6)))
5357
fanoutMaterial.colorNode = mix(
@@ -102,7 +106,7 @@ async function start() {
102106
fanoutActive = !fanoutActive
103107
status.textContent = "Springing progress uniform node"
104108

105-
animate(progress, fanoutActive ? 1 : 0, {
109+
animate(progressValue, fanoutActive ? 1 : 0, {
106110
type: "spring",
107111
visualDuration: 0.8,
108112
bounce: 0.4,

‎dev/html/src/vgpu-scene.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { cancelFrame, frame as motionFrame } from "motion"
2-
import { animate } from "motion/vgpu"
1+
import { animate, cancelFrame, frame as motionFrame } from "motion"
2+
import { vgpuEffect } from "motion/vgpu"
33
import {
44
clock,
55
draw,
@@ -22,6 +22,8 @@ import {
2222
} from "vgpu/scene"
2323
import "./gpu-adapters.css"
2424

25+
animate.addEffect(vgpuEffect)
26+
2527
const stage = document.querySelector(".stage")
2628
const canvas = document.querySelector("canvas")
2729
const status = document.querySelector("#status")

‎dev/html/src/vgpu.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { cancelFrame, frame as motionFrame, motionValue } from "motion"
2-
import { animate, uniformEffect } from "motion/vgpu"
1+
import { animate, cancelFrame, frame as motionFrame, motionValue } from "motion"
2+
import { vgpuEffect } from "motion/vgpu"
33
import { effect, frame as vgpuFrame, init, surface, uniforms } from "vgpu"
44
import "./gpu-adapters.css"
55

6+
animate.addEffect(vgpuEffect)
7+
68
const stage = document.querySelector(".stage")
79
const canvas = document.querySelector("canvas")
810
const intensityInput = document.querySelector("#intensity")
@@ -73,7 +75,7 @@ async function start() {
7375
)
7476

7577
const intensity = motionValue(1)
76-
const cancelUniformEffect = uniformEffect(globals, { intensity })
78+
const cancelUniformEffect = vgpuEffect(globals, { intensity })
7779

7880
intensityInput.addEventListener("input", () => {
7981
intensity.set(Number(intensityInput.value))

‎dev/html/three-effects.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div>
1212
<h1>Three.js effects</h1>
1313
<p>
14-
objectEffect(), uniformEffect() and shared MotionValues.
14+
threeEffect() bindings and shared MotionValues.
1515
</p>
1616
</div>
1717
<nav>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { createEffect, frame, motionValue } from "motion-dom"
2+
import { animate } from ".."
3+
4+
async function nextFrame() {
5+
return new Promise<void>((resolve) => {
6+
frame.postRender(() => resolve())
7+
})
8+
}
9+
10+
interface Subject {
11+
isSubject: true
12+
values: Record<string, number | string>
13+
}
14+
15+
const createSubject = (values: Subject["values"] = {}): Subject => ({
16+
isSubject: true,
17+
values,
18+
})
19+
20+
const subjectEffect = createEffect<Subject>(
21+
(subject, state, key, value) =>
22+
state.set(
23+
key,
24+
value,
25+
() => {
26+
subject.values[key] = state.latest[key]
27+
},
28+
undefined,
29+
false
30+
),
31+
{
32+
test: (subject): subject is Subject =>
33+
Boolean((subject as Subject)?.isSubject),
34+
read: (subject, key) => subject.values[key],
35+
}
36+
)
37+
38+
describe("animate.addEffect", () => {
39+
beforeEach(() => animate.addEffect(subjectEffect))
40+
afterEach(() => animate.removeEffect(subjectEffect))
41+
42+
test("animates subjects claimed by a registered effect", async () => {
43+
const subject = createSubject({ x: 0 })
44+
45+
await animate(subject, { x: 100 }, { duration: 0.01 })
46+
await nextFrame()
47+
48+
expect(subject.values.x).toBe(100)
49+
})
50+
51+
test("falls back to plain objects when no effect claims the subject", async () => {
52+
const subject = { x: 0 }
53+
54+
await animate(subject, { x: 100 }, { duration: 0.01 })
55+
56+
expect(subject.x).toBe(100)
57+
})
58+
59+
test("animates DOM elements even when an effect claims everything", async () => {
60+
const greedyEffect = createEffect<any>(() => () => {}, {
61+
test: (_subject: unknown): _subject is any => true,
62+
read: () => 0,
63+
})
64+
animate.addEffect(greedyEffect)
65+
66+
const element = document.createElement("div")
67+
await animate(element, { opacity: 0.5 }, { duration: 0.01 })
68+
await nextFrame()
69+
70+
animate.removeEffect(greedyEffect)
71+
72+
expect(element.style.opacity).toBe("0.5")
73+
})
74+
75+
test("animates arrays of effect subjects with stagger", async () => {
76+
const a = createSubject({ x: 0 })
77+
const b = createSubject({ x: 0 })
78+
79+
await animate(
80+
[a, b],
81+
{ x: 100 },
82+
{
83+
duration: 0.01,
84+
delay: (i: number) => i * 0.01,
85+
}
86+
)
87+
await nextFrame()
88+
89+
expect(a.values.x).toBe(100)
90+
expect(b.values.x).toBe(100)
91+
})
92+
93+
test("animates mixed DOM and effect subjects in a sequence", async () => {
94+
const element = document.createElement("div")
95+
const subject = createSubject({ x: 0 })
96+
97+
await animate(
98+
[
99+
[element, { opacity: 0.5 }, { duration: 0.01 }],
100+
[subject, { x: 100 }, { duration: 0.01 }],
101+
],
102+
{ duration: 0.01 }
103+
)
104+
await nextFrame()
105+
106+
expect(element.style.opacity).toBe("0.5")
107+
expect(subject.values.x).toBe(100)
108+
})
109+
110+
test("reuses motion values bound via the effect", async () => {
111+
const subject = createSubject()
112+
const x = motionValue(0)
113+
subjectEffect(subject, { x })
114+
115+
await animate(subject, { x: 100 }, { duration: 0.01 })
116+
117+
expect(x.get()).toBe(100)
118+
})
119+
})

‎packages/framer-motion/src/animation/animate/index.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
UnresolvedValueKeyframe,
99
ValueAnimationTransition,
1010
} from "motion-dom"
11-
import { GroupAnimationWithThen } from "motion-dom"
11+
import { addEffect, GroupAnimationWithThen, removeEffect } from "motion-dom"
1212
import { removeItem } from "motion-utils"
1313
import {
1414
AnimationSequence,
@@ -160,4 +160,13 @@ export function createScopedAnimate(options: ScopedAnimateOptions = {}) {
160160
return scopedAnimate
161161
}
162162

163-
export const animate = createScopedAnimate()
163+
export const animate = Object.assign(createScopedAnimate(), {
164+
/**
165+
* Register an effect so `animate()` can animate the subjects it
166+
* claims, for instance `animate.addEffect(threeEffect)`. The most
167+
* recently added effect is tested first. DOM elements are always
168+
* animated directly.
169+
*/
170+
addEffect,
171+
removeEffect,
172+
})

‎packages/framer-motion/src/animation/animate/subject.ts‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import {
2+
animateEffectSubject,
23
animateTarget,
34
AnimationPlaybackControlsWithThen,
45
AnimationScope,
56
AnyResolvedKeyframe,
67
DOMKeyframesDefinition,
78
AnimationOptions as DynamicAnimationOptions,
9+
EffectKeyframes,
10+
EffectTransition,
811
ElementOrSelector,
12+
findEffect,
913
isMotionValue,
1014
MotionValue,
1115
TargetAndTransition,
@@ -128,17 +132,6 @@ export function animateSubject<O extends Object>(
128132

129133
for (let i = 0; i < numSubjects; i++) {
130134
const thisSubject = subjects[i]
131-
132-
const createVisualElement =
133-
thisSubject instanceof Element
134-
? createDOMVisualElement
135-
: createObjectVisualElement
136-
137-
if (!visualElementStore.has(thisSubject)) {
138-
createVisualElement(thisSubject as any)
139-
}
140-
141-
const visualElement = visualElementStore.get(thisSubject)!
142135
const transition = { ...options }
143136

144137
/**
@@ -151,6 +144,36 @@ export function animateSubject<O extends Object>(
151144
transition.delay = transition.delay(i, numSubjects)
152145
}
153146

147+
const isElement = thisSubject instanceof Element
148+
149+
/**
150+
* Registered effects (animate.addEffect) claim non-DOM subjects
151+
* before we fall back to treating them as plain objects.
152+
*/
153+
const effect = isElement ? undefined : findEffect(thisSubject)
154+
155+
if (effect) {
156+
animations.push(
157+
...animateEffectSubject(
158+
effect,
159+
thisSubject as object,
160+
keyframes as EffectKeyframes,
161+
transition as EffectTransition
162+
)
163+
)
164+
continue
165+
}
166+
167+
const createVisualElement = isElement
168+
? createDOMVisualElement
169+
: createObjectVisualElement
170+
171+
if (!visualElementStore.has(thisSubject)) {
172+
createVisualElement(thisSubject as any)
173+
}
174+
175+
const visualElement = visualElementStore.get(thisSubject)!
176+
154177
animations.push(
155178
...animateTarget(
156179
visualElement,

0 commit comments

Comments
 (0)