From 6b84971f6bc55612e55721deba51c45bf9534bd9 Mon Sep 17 00:00:00 2001 From: ukimsanov Date: Thu, 23 Apr 2026 21:19:49 -0400 Subject: [PATCH] feat(shader-transitions): support any aspect ratio (vertical, square) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Read data-width/data-height from the composition root instead of hardcoding 1920x1080. Enables shader transitions on vertical (1080x1920) and square (1080x1080) compositions. Changes across 3 files: - webgl.ts: WIDTH/HEIGHT constants → DEFAULT_WIDTH/DEFAULT_HEIGHT, createContext and renderShader accept width/height params - capture.ts: captureScene and captureIncomingScene accept width/height params for html2canvas - hyper-shader.ts: reads data-width/data-height from root element, passes dimensions to all webgl and capture calls GLSL shaders unchanged — they already use u_resolution uniform for all coordinate math and work at any aspect ratio. Backwards compatible: all params default to 1920x1080 when not provided or when data-width/data-height are missing from the DOM. Co-Authored-By: Claude Opus 4.6 (1M context) --- packages/shader-transitions/src/capture.ts | 15 +++++++--- .../shader-transitions/src/hyper-shader.ts | 30 +++++++++++++------ packages/shader-transitions/src/webgl.ts | 16 ++++++---- 3 files changed, 43 insertions(+), 18 deletions(-) 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 { +export function captureScene( + sceneEl: HTMLElement, + bgColor: string, + width: number = 1920, + height: number = 1080, +): Promise { 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 { return new Promise((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("[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);