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

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) <noreply@anthropic.com>
This commit is contained in:
ukimsanov
2026-04-23 21:19:49 -04:00
co-authored by Claude Opus 4.6
parent 31cd0ea6e7
commit 6b84971f6b
3 changed files with 43 additions and 18 deletions
+11 -4
View File
@@ -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);
});
});
});
@@ -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);
+11 -5
View File
@@ -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);