Skip to content

Write HTML. Render video. Built for agents.

feat(shader-transitions): support any aspect ratio (vertical, square)

ukimsanov6b84971f6bc5
diff --git a/packages/shader-transitions/src/capture.ts b/packages/shader-transitions/src/capture.ts
index 92c277d6d5..aeb5e92abd 100644
--- a/packages/shader-transitions/src/capture.ts
+++ b/packages/shader-transitions/src/capture.ts
@@ -26,10 +26,15 @@ export function initCapture(): void {
patchCreatePattern();
}
-export function captureScene(sceneEl: HTMLElement, bgColor: string): Promise<HTMLCanvasElement> {
+export function captureScene(
+ sceneEl: HTMLElement,
+ bgColor: string,
+ width: number = 1920,
+ height: number = 1080,
+): Promise<HTMLCanvasElement> {
return html2canvas(sceneEl, {
- width: 1920,
- height: 1080,
+ width,
+ height,
scale: 1,
backgroundColor: bgColor,
logging: false,
@@ -84,6 +89,8 @@ export function captureScene(sceneEl: HTMLElement, bgColor: string): Promise<HTM
export function captureIncomingScene(
toScene: HTMLElement,
bgColor: string,
+ width: number = 1920,
+ height: number = 1080,
): Promise<HTMLCanvasElement> {
return new Promise<HTMLCanvasElement>((resolve, reject) => {
const origZ = toScene.style.zIndex;
@@ -105,7 +112,7 @@ export function captureIncomingScene(
requestAnimationFrame(() => {
requestAnimationFrame(() => {
- captureScene(toScene, bgColor).then(resolve, reject).finally(restore);
+ captureScene(toScene, bgColor, width, height).then(resolve, reject).finally(restore);
});
});
});
diff --git a/packages/shader-transitions/src/hyper-shader.ts b/packages/shader-transitions/src/hyper-shader.ts
index 30064fc0d7..0a726e8d6f 100644
--- a/packages/shader-transitions/src/hyper-shader.ts
+++ b/packages/shader-transitions/src/hyper-shader.ts
@@ -5,8 +5,8 @@ import {
createTexture,
uploadTexture,
renderShader,
- WIDTH,
- HEIGHT,
+ DEFAULT_WIDTH,
+ DEFAULT_HEIGHT,
type AccentColors,
} from "./webgl.js";
import { getFragSource, type ShaderName } from "./shaders/registry.js";
@@ -160,6 +160,8 @@ export function init(config: HyperShaderConfig): GsapTimeline {
const root = document.querySelector<HTMLElement>("[data-composition-id]");
const compId = config.compositionId || root?.getAttribute("data-composition-id") || "main";
+ const compWidth = Number(root?.getAttribute("data-width") || DEFAULT_WIDTH);
+ const compHeight = Number(root?.getAttribute("data-height") || DEFAULT_HEIGHT);
// The Hyperframes engine injects a virtual-time shim (window.__HF_VIRTUAL_TIME__)
// during render mode and composites every transition itself from the
@@ -188,13 +190,13 @@ export function init(config: HyperShaderConfig): GsapTimeline {
if (!glCanvas) {
glCanvas = document.createElement("canvas");
glCanvas.id = "gl-canvas";
- glCanvas.width = WIDTH;
- glCanvas.height = HEIGHT;
- glCanvas.style.cssText = `position:absolute;top:0;left:0;width:${WIDTH}px;height:${HEIGHT}px;z-index:100;pointer-events:none;display:none;`;
+ glCanvas.width = compWidth;
+ glCanvas.height = compHeight;
+ glCanvas.style.cssText = `position:absolute;top:0;left:0;width:${compWidth}px;height:${compHeight}px;z-index:100;pointer-events:none;display:none;`;
(root || document.body).appendChild(glCanvas);
}
- const gl = createContext(glCanvas);
+ const gl = createContext(glCanvas, compWidth, compHeight);
if (!gl) {
console.warn("[HyperShader] WebGL unavailable — shader transitions disabled.");
const fallback = config.timeline || gsap.timeline({ paused: true });
@@ -225,7 +227,17 @@ export function init(config: HyperShaderConfig): GsapTimeline {
const fromTex = textures.get(state.fromId);
const toTex = textures.get(state.toId);
if (fromTex && toTex) {
- renderShader(gl, quadBuf, state.prog, fromTex, toTex, state.progress, accentColors);
+ renderShader(
+ gl,
+ quadBuf,
+ state.prog,
+ fromTex,
+ toTex,
+ state.progress,
+ accentColors,
+ compWidth,
+ compHeight,
+ );
}
}
};
@@ -268,11 +280,11 @@ export function init(config: HyperShaderConfig): GsapTimeline {
const wasPlaying = !tl.paused();
if (wasPlaying) tl.pause();
- captureScene(fromScene, bgColor)
+ captureScene(fromScene, bgColor, compWidth, compHeight)
.then((fromCanvas) => {
const fromTex = textures.get(fromId);
if (fromTex) uploadTexture(gl, fromTex, fromCanvas);
- return captureIncomingScene(toScene, bgColor);
+ return captureIncomingScene(toScene, bgColor, compWidth, compHeight);
})
.then((toCanvas) => {
const toTex = textures.get(toId);
diff --git a/packages/shader-transitions/src/webgl.ts b/packages/shader-transitions/src/webgl.ts
index 1ed23b4ca7..3025229ae3 100644
--- a/packages/shader-transitions/src/webgl.ts
+++ b/packages/shader-transitions/src/webgl.ts
@@ -1,12 +1,16 @@
import { vertSrc } from "./shaders/common.js";
-export const WIDTH = 1920;
-export const HEIGHT = 1080;
+export const DEFAULT_WIDTH = 1920;
+export const DEFAULT_HEIGHT = 1080;
-export function createContext(canvas: HTMLCanvasElement): WebGLRenderingContext | null {
+export function createContext(
+ canvas: HTMLCanvasElement,
+ width: number = DEFAULT_WIDTH,
+ height: number = DEFAULT_HEIGHT,
+): WebGLRenderingContext | null {
const gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
if (!gl) return null;
- gl.viewport(0, 0, WIDTH, HEIGHT);
+ gl.viewport(0, 0, width, height);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
return gl as WebGLRenderingContext;
}
@@ -91,6 +95,8 @@ export function renderShader(
texTo: WebGLTexture,
progress: number,
colors?: AccentColors,
+ width: number = DEFAULT_WIDTH,
+ height: number = DEFAULT_HEIGHT,
): void {
const loc = getLocations(gl, prog);
gl.useProgram(prog);
@@ -101,7 +107,7 @@ export function renderShader(
gl.bindTexture(gl.TEXTURE_2D, texTo);
gl.uniform1i(loc.to, 1);
gl.uniform1f(loc.progress, progress);
- gl.uniform2f(loc.resolution, WIDTH, HEIGHT);
+ gl.uniform2f(loc.resolution, width, height);
if (colors) {
gl.uniform3f(loc.accent, ...colors.accent);
gl.uniform3f(loc.accentDark, ...colors.dark);