@remotion/transitions 4.0.465 → 4.0.466

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,393 @@
1
+ // src/html-in-canvas-presentation.tsx
2
+ import { useLayoutEffect, useMemo, useRef, useState, useCallback } from "react";
3
+ import {
4
+ HtmlInCanvas,
5
+ HTML_IN_CANVAS_UNSUPPORTED_MESSAGE,
6
+ useDelayRender
7
+ } from "remotion";
8
+ import { AbsoluteFill, Internals } from "remotion";
9
+ import { jsx } from "react/jsx-runtime";
10
+ var HtmlInCanvasPresentation = ({
11
+ children,
12
+ onElementImage,
13
+ onUnmount,
14
+ presentationProgress,
15
+ presentationDirection,
16
+ shader,
17
+ effects,
18
+ passedProps,
19
+ bothEnteringAndExiting
20
+ }) => {
21
+ if (!HtmlInCanvas.isSupported()) {
22
+ throw new Error(HTML_IN_CANVAS_UNSUPPORTED_MESSAGE);
23
+ }
24
+ const canvasRef = useRef(null);
25
+ const canvasSubtreeStyle = useMemo(() => {
26
+ return {
27
+ width: "100%",
28
+ height: "100%",
29
+ position: "absolute",
30
+ top: 0,
31
+ left: 0,
32
+ right: 0,
33
+ bottom: 0
34
+ };
35
+ }, []);
36
+ const [offscreenCanvas] = useState(() => new OffscreenCanvas(1, 1));
37
+ const passedPropsRef = useRef(passedProps);
38
+ passedPropsRef.current = passedProps;
39
+ const memoizedEffects = Internals.useMemoizedEffects({
40
+ effects: effects ?? [],
41
+ overrideId: null
42
+ });
43
+ const effectsRef = useRef(memoizedEffects);
44
+ effectsRef.current = memoizedEffects;
45
+ const [instance] = useState(() => shader(offscreenCanvas));
46
+ useLayoutEffect(() => {
47
+ return () => {
48
+ instance.cleanup();
49
+ };
50
+ }, [offscreenCanvas, instance]);
51
+ const chainState = Internals.useEffectChainState();
52
+ const { delayRender, continueRender } = useDelayRender();
53
+ const draw = useCallback(async (prevImage, nextImage, progress) => {
54
+ if (!canvasRef.current) {
55
+ throw new Error("Canvas not found");
56
+ }
57
+ const handle = delayRender("onPaint");
58
+ if (!prevImage && !nextImage) {
59
+ continueRender(handle);
60
+ instance.clear();
61
+ return;
62
+ }
63
+ const width = prevImage?.width ?? nextImage?.width ?? 0;
64
+ const height = prevImage?.height ?? nextImage?.height ?? 0;
65
+ if (width === 0 || height === 0) {
66
+ continueRender(handle);
67
+ instance.clear();
68
+ return;
69
+ }
70
+ offscreenCanvas.width = width;
71
+ offscreenCanvas.height = height;
72
+ instance.draw({
73
+ prevImage,
74
+ nextImage,
75
+ width,
76
+ height,
77
+ time: progress,
78
+ passedProps: passedPropsRef.current
79
+ });
80
+ await Internals.runEffectChain({
81
+ state: chainState.get(width, height),
82
+ source: offscreenCanvas,
83
+ effects: effectsRef.current ?? [],
84
+ width,
85
+ height,
86
+ output: canvasRef.current
87
+ });
88
+ continueRender(handle);
89
+ }, [chainState, instance, offscreenCanvas, continueRender, delayRender]);
90
+ const passThrough = bothEnteringAndExiting && presentationDirection === "exiting";
91
+ useLayoutEffect(() => {
92
+ if (passThrough) {
93
+ return;
94
+ }
95
+ const canvas = canvasRef.current;
96
+ if (!canvas) {
97
+ throw new Error("Canvas not found");
98
+ }
99
+ canvas.layoutSubtree = true;
100
+ const onPaint = () => {
101
+ const firstChild = canvas.firstChild;
102
+ if (!firstChild) {
103
+ return;
104
+ }
105
+ const elementImage = canvas.captureElementImage(firstChild);
106
+ onElementImage(elementImage, draw);
107
+ };
108
+ canvas.addEventListener("paint", onPaint);
109
+ return () => {
110
+ canvas.removeEventListener("paint", onPaint);
111
+ };
112
+ }, [onElementImage, presentationDirection, draw, passThrough]);
113
+ useLayoutEffect(() => {
114
+ if (passThrough) {
115
+ return;
116
+ }
117
+ const canvas = canvasRef.current;
118
+ if (!canvas) {
119
+ throw new Error("Canvas not found");
120
+ }
121
+ canvas.requestPaint?.();
122
+ }, [presentationProgress, passThrough, memoizedEffects]);
123
+ useLayoutEffect(() => {
124
+ if (passThrough) {
125
+ return;
126
+ }
127
+ return () => {
128
+ onUnmount();
129
+ };
130
+ }, [onUnmount, passThrough]);
131
+ useLayoutEffect(() => {
132
+ if (passThrough) {
133
+ return;
134
+ }
135
+ const canvas = canvasRef.current;
136
+ if (!canvas) {
137
+ return;
138
+ }
139
+ const observer = new ResizeObserver(([entry]) => {
140
+ canvas.width = entry.devicePixelContentBoxSize[0].inlineSize;
141
+ canvas.height = entry.devicePixelContentBoxSize[0].blockSize;
142
+ });
143
+ observer.observe(canvas, { box: "device-pixel-content-box" });
144
+ }, [passThrough]);
145
+ if (passThrough) {
146
+ return children;
147
+ }
148
+ return /* @__PURE__ */ jsx(AbsoluteFill, {
149
+ children: /* @__PURE__ */ jsx("canvas", {
150
+ ref: canvasRef,
151
+ style: canvasSubtreeStyle,
152
+ children
153
+ })
154
+ });
155
+ };
156
+ var makeHtmlInCanvasPresentation = (shader) => {
157
+ const CompWithShader = (props) => {
158
+ const { passedProps, ...otherProps } = props;
159
+ const { effects, ...restPassedProps } = props.passedProps;
160
+ return /* @__PURE__ */ jsx(HtmlInCanvasPresentation, {
161
+ shader,
162
+ passedProps: restPassedProps,
163
+ effects,
164
+ ...otherProps
165
+ });
166
+ };
167
+ return (props) => {
168
+ return {
169
+ component: CompWithShader,
170
+ props
171
+ };
172
+ };
173
+ };
174
+
175
+ // src/presentations/swap.tsx
176
+ var DEFAULT_REFLECTION = 0.4;
177
+ var DEFAULT_PERSPECTIVE = 0.2;
178
+ var DEFAULT_DEPTH = 3;
179
+ var VERTEX_SHADER = `#version 300 es
180
+ in vec2 a_pos;
181
+ out vec2 v_uv;
182
+ void main() {
183
+ v_uv = vec2(a_pos.x * 0.5 + 0.5, 0.5 - a_pos.y * 0.5);
184
+ gl_Position = vec4(a_pos, 0.0, 1.0);
185
+ }`;
186
+ var FRAGMENT_SHADER = `#version 300 es
187
+ precision highp float;
188
+
189
+ uniform sampler2D u_prev;
190
+ uniform sampler2D u_next;
191
+ uniform float u_time;
192
+ uniform float u_reflection;
193
+ uniform float u_perspective;
194
+ uniform float u_depth;
195
+
196
+ in vec2 v_uv;
197
+ out vec4 outColor;
198
+
199
+ const vec4 black = vec4(0.0, 0.0, 0.0, 1.0);
200
+ const vec2 boundMin = vec2(0.0, 0.0);
201
+ const vec2 boundMax = vec2(1.0, 1.0);
202
+
203
+ bool inBounds(vec2 p) {
204
+ return all(lessThan(boundMin, p)) && all(lessThan(p, boundMax));
205
+ }
206
+
207
+ vec2 project(vec2 p) {
208
+ return p * vec2(1.0, -1.2) + vec2(0.0, 2.22);
209
+ }
210
+
211
+ vec4 bgColor(vec2 p, vec2 pfr, vec2 pto) {
212
+ vec4 c = black;
213
+ pfr = project(pfr);
214
+ if (inBounds(pfr)) {
215
+ c += mix(black, texture(u_prev, pfr), u_reflection * mix(1.0, 0.0, pfr.y));
216
+ }
217
+ pto = project(pto);
218
+ if (inBounds(pto)) {
219
+ c += mix(black, texture(u_next, pto), u_reflection * mix(1.0, 0.0, pto.y));
220
+ }
221
+ return c;
222
+ }
223
+
224
+ vec4 transition(vec2 p, float progress) {
225
+ vec2 pfr;
226
+ vec2 pto = vec2(-1.0);
227
+
228
+ float size = mix(1.0, u_depth, progress);
229
+ float persp = u_perspective * progress;
230
+ pfr = (p + vec2(-0.0, -0.5)) * vec2(
231
+ size / (1.0 - u_perspective * progress),
232
+ size / (1.0 - size * persp * p.x)
233
+ ) + vec2(0.0, 0.5);
234
+
235
+ size = mix(1.0, u_depth, 1.0 - progress);
236
+ persp = u_perspective * (1.0 - progress);
237
+ pto = (p + vec2(-1.0, -0.5)) * vec2(
238
+ size / (1.0 - u_perspective * (1.0 - progress)),
239
+ size / (1.0 - size * persp * (0.5 - p.x))
240
+ ) + vec2(1.0, 0.5);
241
+
242
+ if (progress < 0.5) {
243
+ if (inBounds(pfr)) {
244
+ return texture(u_prev, pfr);
245
+ }
246
+ if (inBounds(pto)) {
247
+ return texture(u_next, pto);
248
+ }
249
+ }
250
+ if (inBounds(pto)) {
251
+ return texture(u_next, pto);
252
+ }
253
+ if (inBounds(pfr)) {
254
+ return texture(u_prev, pfr);
255
+ }
256
+ return bgColor(p, pfr, pto);
257
+ }
258
+
259
+ void main() {
260
+ float progress = 1.0 - u_time;
261
+ outColor = transition(v_uv, progress);
262
+ }`;
263
+ var compileShader = (gl, source, type) => {
264
+ const shader = gl.createShader(type);
265
+ if (!shader) {
266
+ throw new Error("Failed to create shader");
267
+ }
268
+ gl.shaderSource(shader, source);
269
+ gl.compileShader(shader);
270
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
271
+ const log = gl.getShaderInfoLog(shader);
272
+ gl.deleteShader(shader);
273
+ throw new Error(`Failed to compile shader: ${log}`);
274
+ }
275
+ return shader;
276
+ };
277
+ var createProgram = (gl) => {
278
+ const program = gl.createProgram();
279
+ if (!program) {
280
+ throw new Error("Failed to create WebGL program");
281
+ }
282
+ const vs = compileShader(gl, VERTEX_SHADER, gl.VERTEX_SHADER);
283
+ const fs = compileShader(gl, FRAGMENT_SHADER, gl.FRAGMENT_SHADER);
284
+ gl.attachShader(program, vs);
285
+ gl.attachShader(program, fs);
286
+ gl.linkProgram(program);
287
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
288
+ const log = gl.getProgramInfoLog(program);
289
+ gl.deleteProgram(program);
290
+ throw new Error(`Failed to link program: ${log}`);
291
+ }
292
+ return program;
293
+ };
294
+ var createTexture = (gl) => {
295
+ const tex = gl.createTexture();
296
+ if (!tex) {
297
+ throw new Error("Failed to create texture");
298
+ }
299
+ gl.bindTexture(gl.TEXTURE_2D, tex);
300
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
301
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
302
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
303
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
304
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 0]));
305
+ return tex;
306
+ };
307
+ var swapShader = (canvas) => {
308
+ const gl = canvas.getContext("webgl2", { premultipliedAlpha: true });
309
+ if (!gl) {
310
+ throw new Error("Failed to create WebGL2 context");
311
+ }
312
+ const program = createProgram(gl);
313
+ const prevTex = createTexture(gl);
314
+ const nextTex = createTexture(gl);
315
+ const vao = gl.createVertexArray();
316
+ gl.bindVertexArray(vao);
317
+ const buffer = gl.createBuffer();
318
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
319
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
320
+ const aPos = gl.getAttribLocation(program, "a_pos");
321
+ gl.enableVertexAttribArray(aPos);
322
+ gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
323
+ const uTime = gl.getUniformLocation(program, "u_time");
324
+ const uPrev = gl.getUniformLocation(program, "u_prev");
325
+ const uNext = gl.getUniformLocation(program, "u_next");
326
+ const uReflection = gl.getUniformLocation(program, "u_reflection");
327
+ const uPerspective = gl.getUniformLocation(program, "u_perspective");
328
+ const uDepth = gl.getUniformLocation(program, "u_depth");
329
+ const cleanup = () => {
330
+ gl.deleteProgram(program);
331
+ gl.deleteTexture(prevTex);
332
+ gl.deleteTexture(nextTex);
333
+ };
334
+ const clear = () => {
335
+ gl.clearColor(0, 0, 0, 0);
336
+ gl.clear(gl.COLOR_BUFFER_BIT);
337
+ };
338
+ const draw = ({
339
+ prevImage,
340
+ nextImage,
341
+ width,
342
+ height,
343
+ time,
344
+ passedProps
345
+ }) => {
346
+ const {
347
+ reflection = DEFAULT_REFLECTION,
348
+ perspective = DEFAULT_PERSPECTIVE,
349
+ depth = DEFAULT_DEPTH
350
+ } = passedProps;
351
+ if (!prevImage && !nextImage) {
352
+ return;
353
+ }
354
+ if (prevImage && (prevImage.width === 0 || prevImage.height === 0)) {
355
+ return;
356
+ }
357
+ if (nextImage && (nextImage.width === 0 || nextImage.height === 0)) {
358
+ return;
359
+ }
360
+ const effectiveTime = !prevImage ? 0 : !nextImage ? 1 : time;
361
+ gl.viewport(0, 0, width, height);
362
+ gl.clearColor(0, 0, 0, 0);
363
+ gl.clear(gl.COLOR_BUFFER_BIT);
364
+ gl.useProgram(program);
365
+ gl.activeTexture(gl.TEXTURE0);
366
+ gl.bindTexture(gl.TEXTURE_2D, prevTex);
367
+ if (prevImage) {
368
+ gl.texElementImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, prevImage);
369
+ }
370
+ gl.uniform1i(uPrev, 0);
371
+ gl.activeTexture(gl.TEXTURE1);
372
+ gl.bindTexture(gl.TEXTURE_2D, nextTex);
373
+ if (nextImage) {
374
+ gl.texElementImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, nextImage);
375
+ }
376
+ gl.uniform1i(uNext, 1);
377
+ gl.uniform1f(uTime, effectiveTime);
378
+ gl.uniform1f(uReflection, reflection);
379
+ gl.uniform1f(uPerspective, perspective);
380
+ gl.uniform1f(uDepth, depth);
381
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
382
+ };
383
+ return {
384
+ clear,
385
+ cleanup,
386
+ draw
387
+ };
388
+ };
389
+ var swap = makeHtmlInCanvasPresentation(swapShader);
390
+ export {
391
+ swapShader,
392
+ swap
393
+ };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  export { linearTiming } from './timings/linear-timing.js';
2
2
  export { springTiming } from './timings/spring-timing.js';
3
3
  export { TransitionSeries } from './TransitionSeries.js';
4
- export { TransitionPresentation, TransitionPresentationComponentProps, TransitionSeriesOverlayProps, TransitionTiming, } from './types.js';
5
- export { TransitionState, useTransitionProgress, } from './use-transition-progress.js';
6
- export { HtmlInCanvasShader, HtmlInCanvasShaderDraw, HtmlInCanvasShaderDrawParams, makeHtmlInCanvasPresentation, } from './html-in-canvas-presentation.js';
4
+ export type { TransitionPresentation, TransitionPresentationComponentProps, TransitionSeriesOverlayProps, TransitionTiming, } from './types.js';
5
+ export { useTransitionProgress } from './use-transition-progress.js';
6
+ export type { TransitionState } from './use-transition-progress.js';
7
+ export { makeHtmlInCanvasPresentation } from './html-in-canvas-presentation.js';
8
+ export type { HtmlInCanvasShader, HtmlInCanvasShaderDraw, HtmlInCanvasShaderDrawParams, } from './html-in-canvas-presentation.js';
9
+ export { linearBlur } from './presentations/linear-blur.js';
10
+ export type { LinearBlurProps } from './presentations/linear-blur.js';
11
+ export { dreamyZoom } from './presentations/dreamy-zoom.js';
12
+ export type { DreamyZoomProps } from './presentations/dreamy-zoom.js';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeHtmlInCanvasPresentation = exports.useTransitionProgress = exports.TransitionSeries = exports.springTiming = exports.linearTiming = void 0;
3
+ exports.dreamyZoom = exports.linearBlur = exports.makeHtmlInCanvasPresentation = exports.useTransitionProgress = exports.TransitionSeries = exports.springTiming = exports.linearTiming = void 0;
4
4
  // Timings
5
5
  const linear_timing_js_1 = require("./timings/linear-timing.js");
6
6
  Object.defineProperty(exports, "linearTiming", { enumerable: true, get: function () { return linear_timing_js_1.linearTiming; } });
@@ -15,3 +15,7 @@ Object.defineProperty(exports, "useTransitionProgress", { enumerable: true, get:
15
15
  // HTML-in-canvas
16
16
  const html_in_canvas_presentation_js_1 = require("./html-in-canvas-presentation.js");
17
17
  Object.defineProperty(exports, "makeHtmlInCanvasPresentation", { enumerable: true, get: function () { return html_in_canvas_presentation_js_1.makeHtmlInCanvasPresentation; } });
18
+ const linear_blur_js_1 = require("./presentations/linear-blur.js");
19
+ Object.defineProperty(exports, "linearBlur", { enumerable: true, get: function () { return linear_blur_js_1.linearBlur; } });
20
+ const dreamy_zoom_js_1 = require("./presentations/dreamy-zoom.js");
21
+ Object.defineProperty(exports, "dreamyZoom", { enumerable: true, get: function () { return dreamy_zoom_js_1.dreamyZoom; } });
@@ -0,0 +1,14 @@
1
+ export type BookFlipDirection = 'from-left' | 'from-right' | 'from-top' | 'from-bottom';
2
+ export type BookFlipProps = {
3
+ direction?: BookFlipDirection;
4
+ };
5
+ export declare const bookFlipShader: (canvas: OffscreenCanvas) => {
6
+ clear: () => void;
7
+ cleanup: () => void;
8
+ draw: import("..").HtmlInCanvasShaderDraw<BookFlipProps>;
9
+ };
10
+ export declare const bookFlip: (props: BookFlipProps & {
11
+ effects?: import("remotion").EffectsProp | undefined;
12
+ }) => import("..").TransitionPresentation<BookFlipProps & {
13
+ effects?: import("remotion").EffectsProp | undefined;
14
+ }>;
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bookFlip = exports.bookFlipShader = void 0;
4
+ const html_in_canvas_presentation_1 = require("../html-in-canvas-presentation");
5
+ const DIRECTION_FROM_LEFT = 0;
6
+ const DIRECTION_FROM_RIGHT = 1;
7
+ const DIRECTION_FROM_TOP = 2;
8
+ const DIRECTION_FROM_BOTTOM = 3;
9
+ const DEFAULT_DIRECTION = 'from-right';
10
+ const VERTEX_SHADER = `#version 300 es
11
+ in vec2 a_pos;
12
+ out vec2 v_uv;
13
+ void main() {
14
+ v_uv = vec2(a_pos.x * 0.5 + 0.5, 0.5 - a_pos.y * 0.5);
15
+ gl_Position = vec4(a_pos, 0.0, 1.0);
16
+ }`;
17
+ // Adapted from https://gl-transitions.com/editor/BookFlip
18
+ // Author: hong · License: MIT
19
+ const FRAGMENT_SHADER = `#version 300 es
20
+ precision highp float;
21
+
22
+ uniform sampler2D u_prev;
23
+ uniform sampler2D u_next;
24
+ uniform float u_time;
25
+ uniform float u_direction;
26
+
27
+ in vec2 v_uv;
28
+ out vec4 outColor;
29
+
30
+ const float EPSILON = 0.0001;
31
+
32
+ float avoidZero(float value) {
33
+ if (abs(value) < EPSILON) {
34
+ return value < 0.0 ? -EPSILON : EPSILON;
35
+ }
36
+
37
+ return value;
38
+ }
39
+
40
+ vec2 skewRight(vec2 p, float progress) {
41
+ float skewX = (p.x - progress) / avoidZero(0.5 - progress) * 0.5;
42
+ float skewY =
43
+ (p.y - 0.5) /
44
+ avoidZero(0.5 + progress * (p.x - 0.5) / 0.5) *
45
+ 0.5 +
46
+ 0.5;
47
+ return vec2(skewX, skewY);
48
+ }
49
+
50
+ vec2 skewLeft(vec2 p, float progress) {
51
+ float skewX = (p.x - 0.5) / avoidZero(progress - 0.5) * 0.5 + 0.5;
52
+ float skewY =
53
+ (p.y - 0.5) /
54
+ avoidZero(0.5 + (1.0 - progress) * (0.5 - p.x) / 0.5) *
55
+ 0.5 +
56
+ 0.5;
57
+ return vec2(skewX, skewY);
58
+ }
59
+
60
+ vec4 addShade(float progress) {
61
+ float shadeVal = max(0.7, abs(progress - 0.5) * 2.0);
62
+ return vec4(vec3(shadeVal), 1.0);
63
+ }
64
+
65
+ vec2 toCanonicalUv(vec2 p) {
66
+ if (u_direction < 0.5) {
67
+ return p;
68
+ }
69
+
70
+ if (u_direction < 1.5) {
71
+ return vec2(1.0 - p.x, p.y);
72
+ }
73
+
74
+ if (u_direction < 2.5) {
75
+ return vec2(p.y, 1.0 - p.x);
76
+ }
77
+
78
+ return vec2(1.0 - p.y, p.x);
79
+ }
80
+
81
+ vec2 fromCanonicalUv(vec2 p) {
82
+ if (u_direction < 0.5) {
83
+ return p;
84
+ }
85
+
86
+ if (u_direction < 1.5) {
87
+ return vec2(1.0 - p.x, p.y);
88
+ }
89
+
90
+ if (u_direction < 2.5) {
91
+ return vec2(1.0 - p.y, p.x);
92
+ }
93
+
94
+ return vec2(p.y, 1.0 - p.x);
95
+ }
96
+
97
+ vec4 samplePrev(vec2 p) {
98
+ return texture(u_prev, fromCanonicalUv(p));
99
+ }
100
+
101
+ vec4 sampleNext(vec2 p) {
102
+ return texture(u_next, fromCanonicalUv(p));
103
+ }
104
+
105
+ vec4 transition(vec2 p, float progress) {
106
+ float pr = step(1.0 - progress, p.x);
107
+
108
+ if (p.x < 0.5) {
109
+ return mix(
110
+ samplePrev(p),
111
+ sampleNext(skewLeft(p, progress)) * addShade(progress),
112
+ pr
113
+ );
114
+ }
115
+
116
+ return mix(
117
+ samplePrev(skewRight(p, progress)) * addShade(progress),
118
+ sampleNext(p),
119
+ pr
120
+ );
121
+ }
122
+
123
+ void main() {
124
+ vec2 p = toCanonicalUv(v_uv);
125
+ float progress = 1.0 - u_time;
126
+ outColor = transition(p, progress);
127
+ }`;
128
+ const compileShader = (gl, source, type) => {
129
+ const shader = gl.createShader(type);
130
+ if (!shader) {
131
+ throw new Error('Failed to create shader');
132
+ }
133
+ gl.shaderSource(shader, source);
134
+ gl.compileShader(shader);
135
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
136
+ const log = gl.getShaderInfoLog(shader);
137
+ gl.deleteShader(shader);
138
+ throw new Error(`Failed to compile shader: ${log}`);
139
+ }
140
+ return shader;
141
+ };
142
+ const createProgram = (gl) => {
143
+ const program = gl.createProgram();
144
+ if (!program) {
145
+ throw new Error('Failed to create WebGL program');
146
+ }
147
+ const vs = compileShader(gl, VERTEX_SHADER, gl.VERTEX_SHADER);
148
+ const fs = compileShader(gl, FRAGMENT_SHADER, gl.FRAGMENT_SHADER);
149
+ gl.attachShader(program, vs);
150
+ gl.attachShader(program, fs);
151
+ gl.linkProgram(program);
152
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
153
+ const log = gl.getProgramInfoLog(program);
154
+ gl.deleteProgram(program);
155
+ throw new Error(`Failed to link program: ${log}`);
156
+ }
157
+ return program;
158
+ };
159
+ const createTexture = (gl) => {
160
+ const tex = gl.createTexture();
161
+ if (!tex) {
162
+ throw new Error('Failed to create texture');
163
+ }
164
+ gl.bindTexture(gl.TEXTURE_2D, tex);
165
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
166
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
167
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
168
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
169
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 0]));
170
+ return tex;
171
+ };
172
+ const getDirectionConstant = (direction) => {
173
+ switch (direction) {
174
+ case 'from-left':
175
+ return DIRECTION_FROM_LEFT;
176
+ case 'from-right':
177
+ return DIRECTION_FROM_RIGHT;
178
+ case 'from-top':
179
+ return DIRECTION_FROM_TOP;
180
+ case 'from-bottom':
181
+ return DIRECTION_FROM_BOTTOM;
182
+ default:
183
+ return DIRECTION_FROM_RIGHT;
184
+ }
185
+ };
186
+ const bookFlipShader = (canvas) => {
187
+ const gl = canvas.getContext('webgl2', { premultipliedAlpha: true });
188
+ if (!gl) {
189
+ throw new Error('Failed to create WebGL2 context');
190
+ }
191
+ const program = createProgram(gl);
192
+ const prevTex = createTexture(gl);
193
+ const nextTex = createTexture(gl);
194
+ const vao = gl.createVertexArray();
195
+ gl.bindVertexArray(vao);
196
+ const buffer = gl.createBuffer();
197
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
198
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
199
+ const aPos = gl.getAttribLocation(program, 'a_pos');
200
+ gl.enableVertexAttribArray(aPos);
201
+ gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
202
+ const uTime = gl.getUniformLocation(program, 'u_time');
203
+ const uPrev = gl.getUniformLocation(program, 'u_prev');
204
+ const uNext = gl.getUniformLocation(program, 'u_next');
205
+ const uDirection = gl.getUniformLocation(program, 'u_direction');
206
+ const cleanup = () => {
207
+ gl.deleteProgram(program);
208
+ gl.deleteTexture(prevTex);
209
+ gl.deleteTexture(nextTex);
210
+ };
211
+ const clear = () => {
212
+ gl.clearColor(0, 0, 0, 0);
213
+ gl.clear(gl.COLOR_BUFFER_BIT);
214
+ };
215
+ const draw = ({ prevImage, nextImage, width, height, time, passedProps, }) => {
216
+ const { direction = DEFAULT_DIRECTION } = passedProps;
217
+ if (!prevImage && !nextImage) {
218
+ return;
219
+ }
220
+ if (prevImage && (prevImage.width === 0 || prevImage.height === 0)) {
221
+ return;
222
+ }
223
+ if (nextImage && (nextImage.width === 0 || nextImage.height === 0)) {
224
+ return;
225
+ }
226
+ // At time=0 the shader outputs nextImage. At time=1 the shader outputs prevImage.
227
+ const effectiveTime = !prevImage ? 0 : !nextImage ? 1 : time;
228
+ gl.viewport(0, 0, width, height);
229
+ gl.clearColor(0, 0, 0, 0);
230
+ gl.clear(gl.COLOR_BUFFER_BIT);
231
+ gl.useProgram(program);
232
+ gl.activeTexture(gl.TEXTURE0);
233
+ gl.bindTexture(gl.TEXTURE_2D, prevTex);
234
+ if (prevImage) {
235
+ gl.texElementImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, prevImage);
236
+ }
237
+ gl.uniform1i(uPrev, 0);
238
+ gl.activeTexture(gl.TEXTURE1);
239
+ gl.bindTexture(gl.TEXTURE_2D, nextTex);
240
+ if (nextImage) {
241
+ gl.texElementImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, nextImage);
242
+ }
243
+ gl.uniform1i(uNext, 1);
244
+ gl.uniform1f(uTime, effectiveTime);
245
+ gl.uniform1f(uDirection, getDirectionConstant(direction));
246
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
247
+ };
248
+ return {
249
+ clear,
250
+ cleanup,
251
+ draw,
252
+ };
253
+ };
254
+ exports.bookFlipShader = bookFlipShader;
255
+ exports.bookFlip = (0, html_in_canvas_presentation_1.makeHtmlInCanvasPresentation)(exports.bookFlipShader);
@@ -0,0 +1,11 @@
1
+ export type CrosswarpProps = Record<string, never>;
2
+ export declare const crosswarpShader: (canvas: OffscreenCanvas) => {
3
+ clear: () => void;
4
+ cleanup: () => void;
5
+ draw: import("..").HtmlInCanvasShaderDraw<CrosswarpProps>;
6
+ };
7
+ export declare const crosswarp: (props: CrosswarpProps & {
8
+ effects?: import("remotion").EffectsProp | undefined;
9
+ }) => import("..").TransitionPresentation<CrosswarpProps & {
10
+ effects?: import("remotion").EffectsProp | undefined;
11
+ }>;