mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-04 16:42:27 +00:00
* feat(catalog): put the variables panel back, on payloads The panel drove its preview by loading an .html from docs/public, a type the host does not publish, so it showed an empty frame in production and was parked when the catalog was re-landed. It now mounts the same JSON payload the plain player uses and re-mounts it as values change, injecting them as window.__hfVariables into the composition head before any of its scripts run, which is where the runtime reads overrides from. Doing it in the markup rather than after load is what stops the composition initialising with the wrong values first. 172 items with variables get the panel back; the playhead carries across a change so a tweak mid-shot does not jump back to frame zero. * fix(docs): drop the unused url form and the needless escapes * fix(docs): the panel cannot reference a binding beside the export * feat(catalog): make importing an SVG the obvious move A reader arrives at this control with a shape, not with path data, and the panel asked for the coordinates first. Import is now the primary action in a drop target you can see is a drop target, and the raw path sits behind a disclosure for anyone who wants it. * feat(cli): let a fruitless catalog search report the gap An agent that searches by meaning and finds nothing worth installing knows something we do not: the name of a move the catalog is missing. There was no way to tell us, so that knowledge was lost at the end of every run. hyperframes feedback --search-miss "<query>" --wanted "<the move>" records it. It carries no rating, so it never lands in the rating metric, and it is a separate deliberate command rather than something catalog --query does on its own: plain search still sends nothing, which is what the CLI promises. --rating stops being required at the arg level, since a miss has no rating to give. The check moved into the run body, where an absent one is now handled rather than crashing on undefined. * feat(cli): carry tuned variable values into the install snippet Someone who tunes a block on its catalog page had no way to keep those values: the install command was the same one everybody gets, and the tuning stayed on the page. hyperframes add <item> --vars '<json>' now prints a mount element carrying data-variable-values, so the values land where the block is used. They ride on the host rather than being written into the installed file. That keeps the composition on disk byte-identical to the registry's, so a later reinstall can still tell an edit from an update, and it lets two mounts of the same block carry different values. * fix(catalog): serve the item's own directory so runtime paths resolve Some compositions assemble their asset URLs at run time — "compositions/components/" + texture + ".png" for the texture masks, a font the compiler pulled into _remote_media — and no scan of the markup can see a string that does not exist until a script concatenates it. Those items either rendered black or were dropped to a video that had never been uploaded. Each item that needs it now has its prepared directory published, and its payload carries a <base> pointing at it, so any relative path the composition invents resolves. caption-texture renders its masks again, and variable-font-flex has a preview at all for the first time: its MP4 and poster are both 403. Both layouts are published, because which one a composition asks for differs per item, and a directory only earns that if it is under 2 MB. The 12 MB texture sheet keeps the recorded video it already had. * fix(catalog): let the variables panel actually drive the composition Every control on the panel was inert. The values reached the composition and nothing repainted, because the payload had already been compiled: compiling inlines a mounted component and resolves its variables into the markup and CSS, so by the time a reader turns a knob there is nothing left to change. An item that declares variables now ships uncompiled, keeping the mount the runtime loads at run time, which is the only state where data-variable-values still means anything. The component travels inline as a data URI rather than a sibling file, because .html is the one type the docs host will not publish. The demo's own pinned values come off, so the reader's choices reach the mount instead of losing to the values the demo picked to show itself off. Measured on the rendered frame rather than the DOM: green rgb(98,207,144), blue rgb(6,6,199), violet rgb(177,147,230), and back to green. docs/public/catalog drops from 48 MB to 35 MB along the way, since an uncompiled payload carries far less than an inlined one. * feat(catalog): keep variable changes in the url A reader who tuned a piece lost it on reload, and had nothing to send anyone. The values now live in the query string, scoped by composition id so two links never read each other,and only the ones that differ from the defaults are written, so changing one knob gives a short URL rather than every variable spelled out. replaceState rather than pushState: dragging a slider should not leave a trail of history entries. An unreadable value is ignored rather than thrown, so a truncated or hand-edited link opens the piece at its defaults. * fix(catalog): only rewrite the url when a value actually changed * refactor(catalog): memoise the declared defaults on their content * feat(catalog): offer an install command carrying the tuned values The Install block is generated before anyone touches a knob, so it can only ever print the plain command. Someone who spent a minute tuning a piece copied it and got the defaults back. The panel now carries its own command in the Snippet tab, with --vars holding exactly the values that differ. An untouched piece still offers the same short command, so nothing gets noisier for the common case. * fix(catalog): a piece with nothing to render is a skip, not a failure caption-blend-difference is a stylesheet and a paragraph of prose — a class you add to your own captions, with no standalone scene to show. The generator treated that as a build failure, so every run ended by reporting something broken when nothing was. It now reports the shape it is and keeps its recorded video, which is the only honest preview such an item has. A genuine render failure still throws. * fix(catalog): restore variables from the url on a cold load A shared link opened at the defaults. The first render happens on the server, where there is no window to read the query string from, and React then hydrates against that markup and never revisits it — so the values only appeared once you touched a control. The URL is read again after mount, which is the first moment it exists. The value is also escaped once now rather than twice: URLSearchParams already decodes on the way out, and decoding a second time turned an SVG path full of percent-escapes into something that no longer parsed, besides doubling the length of every link. * fix(catalog): mount the preview with the values a link carried The frame was built from the declared defaults and the shared values were posted to it afterwards, which is too late for anything the composition reads once at init: a path arrived after the mark had already been drawn from the default one, so a link looked right in the panel and wrong on screen. * feat(catalog): the install command follows the values you tuned Copying the Install line gave the plain command back, because that block is generated before anyone touches a knob and had no way to know what changed. The tuned command only existed in the panel Snippet tab, which is not where anyone looks for it. The line now reads the same query string the panel writes, so the two agree without either component knowing the other exists, and a shared link carries the right command too. replaceState fires no event, so the panel announces its own writes. * fix(catalog): send a text variable to the preview once it is finished Every other control in the explorer reports a whole value on every event: a slider at any position is a position, a swatch is a colour. A text field is not. Typing v3 into a badge posted v first, so the preview remounted and rendered a composition built from half a word. The post now waits while a text field has focus and goes out when the edit is committed, with Enter or by clicking away. The field itself is unchanged and still tracks every keystroke. --------- Co-authored-by: Miguel Angel Simon Sierra <miguelangelsi07@gmail.com>
848 lines
32 KiB
Plaintext
848 lines
32 KiB
Plaintext
---
|
||
title: "Anamorphic Streak Flare"
|
||
description: "A drifting field of bright points, each one throwing the long horizontal blue-violet streak a real anamorphic cinema lens produces. Built from the measured filter constants: a 0.3 high-pass, 80 horizontal taps at quarter resolution, a samples/3 gain and a 0x7a8aff tint."
|
||
---
|
||
|
||
import { InstallCommand } from "/snippets/install-command.jsx";
|
||
import { VariablesExplorer } from "/snippets/variables-explorer.jsx";
|
||
|
||
<VariablesExplorer
|
||
previewSrc="/public/catalog/blocks/vfx-anamorphic-flare.json"
|
||
compositionId="vfx-anamorphic-flare"
|
||
compositionSrc="compositions/vfx-anamorphic-flare.html"
|
||
variables={[{"id":"intensity","type":"number","label":"Streak intensity","default":5,"min":0,"max":20,"step":0.1},{"id":"threshold","type":"number","label":"High-pass threshold","default":0.3,"min":0,"max":1,"step":0.01},{"id":"streakLength","type":"number","label":"Streak length gain","default":1,"min":0.1,"max":6,"step":0.05},{"id":"tint","type":"color","label":"Streak tint","default":"#7a8aff"},{"id":"bloomRadius","type":"number","label":"Bloom radius","default":0,"min":0,"max":80,"step":1,"unit":"px"},{"id":"timeScale","type":"number","label":"Drift rate","default":0.5,"min":0,"max":3,"step":0.05},{"id":"emitters","type":"number","label":"Emitter count","default":140,"min":1,"max":400,"step":1},{"id":"backdrop","type":"color","label":"Backdrop","default":"#04040a"}]}
|
||
>
|
||
|
||
```html vfx-anamorphic-flare.html
|
||
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=1920, height=1080" />
|
||
<title>Anamorphic Streak Flare</title>
|
||
<script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script>
|
||
<style>
|
||
*,
|
||
*::before,
|
||
*::after {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
body {
|
||
background: #000;
|
||
overflow: hidden;
|
||
}
|
||
#af-root {
|
||
position: relative;
|
||
width: 1920px;
|
||
height: 1080px;
|
||
overflow: hidden;
|
||
}
|
||
#af-canvas {
|
||
position: absolute;
|
||
top: 0;
|
||
left: 0;
|
||
width: 1920px;
|
||
height: 1080px;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div
|
||
id="af-root"
|
||
data-composition-id="vfx-anamorphic-flare"
|
||
data-root="true"
|
||
data-width="1920"
|
||
data-height="1080"
|
||
data-start="0"
|
||
data-duration="10"
|
||
data-composition-variables='[
|
||
{"id":"intensity","type":"number","label":"Streak intensity","default":5,"min":0,"max":20,"step":0.1},
|
||
{"id":"threshold","type":"number","label":"High-pass threshold","default":0.3,"min":0,"max":1,"step":0.01},
|
||
{"id":"streakLength","type":"number","label":"Streak length gain","default":1,"min":0.1,"max":6,"step":0.05},
|
||
{"id":"tint","type":"color","label":"Streak tint","default":"#7a8aff"},
|
||
{"id":"bloomRadius","type":"number","label":"Bloom radius","default":0,"min":0,"max":80,"step":1,"unit":"px"},
|
||
{"id":"timeScale","type":"number","label":"Drift rate","default":0.5,"min":0,"max":3,"step":0.05},
|
||
{"id":"emitters","type":"number","label":"Emitter count","default":140,"min":1,"max":400,"step":1},
|
||
{"id":"backdrop","type":"color","label":"Backdrop","default":"#04040a"}
|
||
]'
|
||
>
|
||
<canvas id="af-canvas" width="1920" height="1080"></canvas>
|
||
|
||
<!-- Driver clip: gives HyperFrames a timed element to own on track 0. -->
|
||
<div
|
||
id="af-drv"
|
||
class="clip"
|
||
data-start="0"
|
||
data-duration="10"
|
||
data-track-index="0"
|
||
style="position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none"
|
||
></div>
|
||
</div>
|
||
|
||
<script>
|
||
(function () {
|
||
var DUR = 10;
|
||
var W = 1920;
|
||
var H = 1080;
|
||
|
||
var root = document.getElementById("af-root");
|
||
var V = (window.__hyperframes && window.__hyperframes.getVariables()) || {};
|
||
var CS = getComputedStyle(root);
|
||
|
||
// A declared variable reaches CSS as a custom property on the root.
|
||
// Read that first so a host stylesheet can retheme the block, then
|
||
// fall back to the declared value. Both the namespaced and the bare
|
||
// property name are probed: which one the runtime emits has changed
|
||
// across versions, and the block should not care.
|
||
function raw(id) {
|
||
var slug = id.toLowerCase();
|
||
var css =
|
||
CS.getPropertyValue("--hf-var-" + slug).trim() ||
|
||
CS.getPropertyValue("--" + slug).trim();
|
||
return css !== "" ? css : V[id];
|
||
}
|
||
function num(id, fallback) {
|
||
var n = parseFloat(raw(id));
|
||
return isFinite(n) ? n : fallback;
|
||
}
|
||
function str(id, fallback) {
|
||
var s = raw(id);
|
||
return typeof s === "string" && s !== "" ? s : fallback;
|
||
}
|
||
|
||
// ---------------------------------------------------------------
|
||
// Measured constants.
|
||
//
|
||
// Read from the three.js anamorphic post-processing example (MIT),
|
||
// cross-checked against a live capture of that demo's own GUI:
|
||
//
|
||
// tint 0x7a8aff, threshold 0.3, intensity 5, bloom radius 0,
|
||
// samples 80, time scale 0.5, resolution scale 0.25.
|
||
//
|
||
// The filter walks samples/2 taps in each direction HORIZONTALLY
|
||
// only, weights each tap by 1 - |i| / halfSamples (a tent), and
|
||
// normalises the sum by samples / 3.0. Dividing by samples/3 rather
|
||
// than by the weight sum is deliberate: it is a gain above 1, not
|
||
// an average, which is why a dim highlight still throws a readable
|
||
// streak.
|
||
//
|
||
// This implementation evaluates that convolution in closed form
|
||
// instead of running the 80-tap loop. For a small bright source the
|
||
// tent-weighted horizontal blur has an exact analytic result: the
|
||
// tent itself, centred on the source. So one horizontal gradient
|
||
// per highlight is the same arithmetic the loop would perform, at
|
||
// a fraction of the cost, and with no dependence on WebGL surviving
|
||
// seek capture.
|
||
// ---------------------------------------------------------------
|
||
var SAMPLES = 80;
|
||
var HALF_SAMPLES = SAMPLES / 2; // 40 taps each side
|
||
var RES_SCALE = 0.25; // the streak buffer runs at quarter resolution
|
||
var NORM = SAMPLES / 3; // total / (samples / 3.0)
|
||
var REF_W = 1280; // width the constants were measured at
|
||
|
||
// 40 taps of a 1/(0.25 * 1280) texel step = 12.5% of frame width per
|
||
// side. Held as a fraction rather than as absolute pixels so the look
|
||
// matches the reference at any output size.
|
||
var HALF_FRAC = HALF_SAMPLES / (RES_SCALE * REF_W);
|
||
|
||
var INTENSITY = num("intensity", 5);
|
||
var THRESHOLD = num("threshold", 0.3);
|
||
var STREAK_LEN = num("streakLength", 1);
|
||
var BLOOM_R = num("bloomRadius", 0);
|
||
var TIME_SCALE = num("timeScale", 0.5);
|
||
var COUNT = Math.max(1, Math.round(num("emitters", 140)));
|
||
var BACKDROP = str("backdrop", "#04040a");
|
||
var TINT = hexRgb(str("tint", "#7a8aff"), [0.478, 0.541, 1.0]);
|
||
|
||
function hexRgb(hex, fallback) {
|
||
var m = /^#?([0-9a-f]{6})$/i.exec(String(hex).trim());
|
||
if (!m) return fallback;
|
||
var n = parseInt(m[1], 16);
|
||
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];
|
||
}
|
||
|
||
function mulberry32(seed) {
|
||
return function () {
|
||
seed |= 0;
|
||
seed = (seed + 0x6d2b79f5) | 0;
|
||
var t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
|
||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||
};
|
||
}
|
||
|
||
function hsv(h, s, v) {
|
||
var hh = ((((h % 360) + 360) % 360) / 60) % 6;
|
||
var c = v * s;
|
||
var x = c * (1 - Math.abs((hh % 2) - 1));
|
||
var m = v - c;
|
||
var r = 0;
|
||
var g = 0;
|
||
var b = 0;
|
||
if (hh < 1) {
|
||
r = c;
|
||
g = x;
|
||
} else if (hh < 2) {
|
||
r = x;
|
||
g = c;
|
||
} else if (hh < 3) {
|
||
g = c;
|
||
b = x;
|
||
} else if (hh < 4) {
|
||
g = x;
|
||
b = c;
|
||
} else if (hh < 5) {
|
||
r = x;
|
||
b = c;
|
||
} else {
|
||
r = c;
|
||
b = x;
|
||
}
|
||
return [r + m, g + m, b + m];
|
||
}
|
||
|
||
// ---------------------------------------------------------------
|
||
// The emitter field. Every emitter is a closed-form function of t:
|
||
// two incommensurate sinusoids for position and one for brightness.
|
||
// The seeded PRNG runs once, at setup, so the field is identical on
|
||
// every load and every seek.
|
||
// ---------------------------------------------------------------
|
||
var rng = mulberry32(1337);
|
||
var EM = [];
|
||
for (var i = 0; i < COUNT; i++) {
|
||
EM.push({
|
||
x0: rng() * W,
|
||
y0: rng() * H,
|
||
ax: 24 + rng() * 90,
|
||
ay: 12 + rng() * 48,
|
||
fx: 0.018 + rng() * 0.05,
|
||
fy: 0.015 + rng() * 0.045,
|
||
px: rng(),
|
||
py: rng(),
|
||
r: 3 + Math.pow(rng(), 1.6) * 10,
|
||
h: rng() * 360,
|
||
s: 0.62 + rng() * 0.38,
|
||
v: 0.7 + rng() * 0.3,
|
||
fb: 0.03 + rng() * 0.09,
|
||
pb: rng(),
|
||
});
|
||
}
|
||
|
||
var TAU = Math.PI * 2;
|
||
var canvas = document.getElementById("af-canvas");
|
||
var ctx = canvas.getContext("2d");
|
||
|
||
// Streaks are accumulated in a quarter-resolution buffer, matching
|
||
// the reference's setResolutionScale(0.25). Upscaling it bilinearly
|
||
// is what gives the streak its soft vertical falloff, and it is the
|
||
// reason the streak reads as smooth rather than as a hard bar.
|
||
var SW = Math.round(W * RES_SCALE);
|
||
var SH = Math.round(H * RES_SCALE);
|
||
var sbuf = document.createElement("canvas");
|
||
sbuf.width = SW;
|
||
sbuf.height = SH;
|
||
var sctx = sbuf.getContext("2d");
|
||
|
||
function rgba(c, a) {
|
||
return (
|
||
"rgba(" +
|
||
Math.round(Math.min(1, Math.max(0, c[0])) * 255) +
|
||
"," +
|
||
Math.round(Math.min(1, Math.max(0, c[1])) * 255) +
|
||
"," +
|
||
Math.round(Math.min(1, Math.max(0, c[2])) * 255) +
|
||
"," +
|
||
Math.min(1, Math.max(0, a)) +
|
||
")"
|
||
);
|
||
}
|
||
|
||
// A slow swell across the clip so the field reads as a shot rather
|
||
// than as a loop. Closed form in t, like everything else here.
|
||
function gainAt(t) {
|
||
var u = Math.min(1, Math.max(0, t / DUR));
|
||
return 0.75 + 0.35 * Math.sin(Math.PI * u);
|
||
}
|
||
|
||
function draw(t) {
|
||
var tau = t * TIME_SCALE;
|
||
var gain = gainAt(t);
|
||
var halfLen = HALF_FRAC * W * STREAK_LEN;
|
||
|
||
sctx.setTransform(1, 0, 0, 1, 0, 0);
|
||
sctx.clearRect(0, 0, SW, SH);
|
||
sctx.globalCompositeOperation = "lighter";
|
||
|
||
var e;
|
||
var k;
|
||
var col;
|
||
var bright;
|
||
var x;
|
||
var y;
|
||
for (k = 0; k < EM.length; k++) {
|
||
e = EM[k];
|
||
x = e.x0 + e.ax * Math.sin(TAU * (e.fx * tau + e.px));
|
||
y = e.y0 + e.ay * Math.sin(TAU * (e.fy * tau + e.py));
|
||
bright = e.v * (0.62 + 0.38 * Math.sin(TAU * (e.fb * tau + e.pb)));
|
||
col = hsv(e.h, e.s, Math.max(0, bright));
|
||
|
||
// High pass, then tint, exactly as the reference filter does:
|
||
// max(colour - threshold, 0) * tint. Below threshold there is no
|
||
// streak at all, which is what keeps the field from smearing.
|
||
var hp = [
|
||
Math.max(0, col[0] - THRESHOLD) * TINT[0],
|
||
Math.max(0, col[1] - THRESHOLD) * TINT[1],
|
||
Math.max(0, col[2] - THRESHOLD) * TINT[2],
|
||
];
|
||
var peak = Math.max(hp[0], Math.max(hp[1], hp[2]));
|
||
if (peak <= 0.0001) continue;
|
||
|
||
// Peak of the tent equals the source value times intensity over
|
||
// the samples/3 normaliser. Magnitude is carried in alpha so the
|
||
// hue keeps full 8-bit precision at low brightness.
|
||
var alpha = (peak * INTENSITY * gain) / NORM;
|
||
if (alpha < 0.003) continue;
|
||
var hue = [hp[0] / peak, hp[1] / peak, hp[2] / peak];
|
||
|
||
var bx = x * RES_SCALE;
|
||
var by = y * RES_SCALE;
|
||
var bh = Math.max(1, e.r * 2 * RES_SCALE);
|
||
var bl = halfLen * RES_SCALE;
|
||
|
||
var g = sctx.createLinearGradient(bx - bl, 0, bx + bl, 0);
|
||
g.addColorStop(0, rgba(hue, 0));
|
||
g.addColorStop(0.5, rgba(hue, alpha));
|
||
g.addColorStop(1, rgba(hue, 0));
|
||
sctx.fillStyle = g;
|
||
sctx.fillRect(bx - bl, by - bh / 2, bl * 2, bh);
|
||
}
|
||
|
||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||
ctx.globalCompositeOperation = "source-over";
|
||
ctx.fillStyle = BACKDROP;
|
||
ctx.fillRect(0, 0, W, H);
|
||
|
||
ctx.globalCompositeOperation = "lighter";
|
||
ctx.imageSmoothingEnabled = true;
|
||
ctx.imageSmoothingQuality = "high";
|
||
ctx.drawImage(sbuf, 0, 0, SW, SH, 0, 0, W, H);
|
||
|
||
// The highlights themselves, drawn at full resolution on top.
|
||
for (k = 0; k < EM.length; k++) {
|
||
e = EM[k];
|
||
x = e.x0 + e.ax * Math.sin(TAU * (e.fx * tau + e.px));
|
||
y = e.y0 + e.ay * Math.sin(TAU * (e.fy * tau + e.py));
|
||
bright = e.v * (0.62 + 0.38 * Math.sin(TAU * (e.fb * tau + e.pb)));
|
||
col = hsv(e.h, e.s, Math.max(0, bright));
|
||
|
||
if (BLOOM_R > 0) {
|
||
var br = e.r + BLOOM_R;
|
||
var gb = ctx.createRadialGradient(x, y, 0, x, y, br);
|
||
gb.addColorStop(0, rgba(col, 0.5 * gain));
|
||
gb.addColorStop(0.35, rgba(col, 0.16 * gain));
|
||
gb.addColorStop(1, rgba(col, 0));
|
||
ctx.fillStyle = gb;
|
||
ctx.fillRect(x - br, y - br, br * 2, br * 2);
|
||
}
|
||
|
||
var core = ctx.createRadialGradient(x, y, 0, x, y, e.r);
|
||
core.addColorStop(
|
||
0,
|
||
rgba([col[0] * 0.88 + 0.12, col[1] * 0.88 + 0.12, col[2] * 0.88 + 0.12], 1),
|
||
);
|
||
core.addColorStop(0.72, rgba(col, 0.96));
|
||
core.addColorStop(1, rgba(col, 0));
|
||
ctx.fillStyle = core;
|
||
ctx.fillRect(x - e.r, y - e.r, e.r * 2, e.r * 2);
|
||
}
|
||
}
|
||
|
||
window.__timelines = window.__timelines || {};
|
||
var tl = gsap.timeline({ paused: true });
|
||
|
||
// The canvas is repainted from a property SETTER, not from a
|
||
// timeline onUpdate callback: gsap suppresses events on seek(), so a
|
||
// callback-driven canvas silently freezes on frame 0 under any
|
||
// consumer that scrubs. Tweened values are written on every render,
|
||
// suppressed or not, so the setter fires on seek and hands us the
|
||
// frame time directly.
|
||
var driver = { _t: 0 };
|
||
Object.defineProperty(driver, "t", {
|
||
get: function () {
|
||
return this._t;
|
||
},
|
||
set: function (v) {
|
||
this._t = v;
|
||
draw(v);
|
||
},
|
||
});
|
||
tl.to(driver, { t: DUR, duration: DUR, ease: "none" }, 0);
|
||
window.__timelines["vfx-anamorphic-flare"] = tl;
|
||
|
||
draw(0);
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
</VariablesExplorer>
|
||
|
||
## Install
|
||
|
||
<InstallCommand command="npx hyperframes add vfx-anamorphic-flare" item="vfx-anamorphic-flare" />
|
||
|
||
That writes one file: `compositions/vfx-anamorphic-flare.html`.
|
||
|
||
## Add it to your video
|
||
|
||
It runs for 10 seconds at 1920×1080. Paste this into your composition:
|
||
|
||
```html index.html
|
||
<div
|
||
data-composition-id="vfx-anamorphic-flare"
|
||
data-composition-src="compositions/vfx-anamorphic-flare.html"
|
||
data-start="0"
|
||
data-duration="10"
|
||
data-track-index="1"
|
||
data-width="1920"
|
||
data-height="1080"
|
||
></div>
|
||
```
|
||
|
||
Move it in time with `data-start`. Put it on a different timeline row with
|
||
`data-track-index`. See [data attributes](/concepts/data-attributes) for the rest.
|
||
|
||
## Change how it looks
|
||
|
||
Set these CSS variables on the block:
|
||
|
||
- `--intensity` — Streak intensity. Defaults to `5`.
|
||
- `--threshold` — High-pass threshold. Defaults to `0.3`.
|
||
- `--streaklength` — Streak length gain. Defaults to `1`.
|
||
- `--tint` — Streak tint. Defaults to `#7a8aff`.
|
||
- `--bloomradius` — Bloom radius. Defaults to `0`.
|
||
- `--timescale` — Drift rate. Defaults to `0.5`.
|
||
- `--emitters` — Emitter count. Defaults to `140`.
|
||
- `--backdrop` — Backdrop. Defaults to `#04040a`.
|
||
|
||
## Variables
|
||
|
||
Every one of these has a default, so the piece works untouched. Set the ones you
|
||
want to change on the element:
|
||
|
||
| Variable | Default | Accepts | What it does |
|
||
| --- | --- | --- | --- |
|
||
| `intensity` | `5` | 0 to 20, step 0.1 | |
|
||
| `threshold` | `0.3` | 0 to 1, step 0.01 | |
|
||
| `streakLength` | `1` | 0.1 to 6, step 0.05 | |
|
||
| `tint` | `#7a8aff` | color | |
|
||
| `bloomRadius` | `0` | 0px to 80px, step 1px | |
|
||
| `timeScale` | `0.5` | 0 to 3, step 0.05 | |
|
||
| `emitters` | `140` | 1 to 400, step 1 | |
|
||
| `backdrop` | `#04040a` | color | |
|
||
|
||
Set them with `data-variable-values` on the element that mounts it. These are the
|
||
defaults, so this behaves exactly like the preview above until you change one:
|
||
|
||
```html wrap
|
||
<div
|
||
data-composition-id="vfx-anamorphic-flare"
|
||
data-composition-src="compositions/vfx-anamorphic-flare.html"
|
||
data-variable-values='{"intensity":5,"threshold":0.3,"streakLength":1,"tint":"#7a8aff","bloomRadius":0,"timeScale":0.5,"emitters":140,"backdrop":"#04040a"}'
|
||
></div>
|
||
```
|
||
|
||
## Source
|
||
|
||
<Accordion title={`vfx-anamorphic-flare.html`}>
|
||
|
||
```html
|
||
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=1920, height=1080" />
|
||
<title>Anamorphic Streak Flare</title>
|
||
<script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script>
|
||
<style>
|
||
*,
|
||
*::before,
|
||
*::after {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
body {
|
||
background: #000;
|
||
overflow: hidden;
|
||
}
|
||
#af-root {
|
||
position: relative;
|
||
width: 1920px;
|
||
height: 1080px;
|
||
overflow: hidden;
|
||
}
|
||
#af-canvas {
|
||
position: absolute;
|
||
top: 0;
|
||
left: 0;
|
||
width: 1920px;
|
||
height: 1080px;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div
|
||
id="af-root"
|
||
data-composition-id="vfx-anamorphic-flare"
|
||
data-root="true"
|
||
data-width="1920"
|
||
data-height="1080"
|
||
data-start="0"
|
||
data-duration="10"
|
||
data-composition-variables='[
|
||
{"id":"intensity","type":"number","label":"Streak intensity","default":5,"min":0,"max":20,"step":0.1},
|
||
{"id":"threshold","type":"number","label":"High-pass threshold","default":0.3,"min":0,"max":1,"step":0.01},
|
||
{"id":"streakLength","type":"number","label":"Streak length gain","default":1,"min":0.1,"max":6,"step":0.05},
|
||
{"id":"tint","type":"color","label":"Streak tint","default":"#7a8aff"},
|
||
{"id":"bloomRadius","type":"number","label":"Bloom radius","default":0,"min":0,"max":80,"step":1,"unit":"px"},
|
||
{"id":"timeScale","type":"number","label":"Drift rate","default":0.5,"min":0,"max":3,"step":0.05},
|
||
{"id":"emitters","type":"number","label":"Emitter count","default":140,"min":1,"max":400,"step":1},
|
||
{"id":"backdrop","type":"color","label":"Backdrop","default":"#04040a"}
|
||
]'
|
||
>
|
||
<canvas id="af-canvas" width="1920" height="1080"></canvas>
|
||
|
||
<!-- Driver clip: gives HyperFrames a timed element to own on track 0. -->
|
||
<div
|
||
id="af-drv"
|
||
class="clip"
|
||
data-start="0"
|
||
data-duration="10"
|
||
data-track-index="0"
|
||
style="position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none"
|
||
></div>
|
||
</div>
|
||
|
||
<script>
|
||
(function () {
|
||
var DUR = 10;
|
||
var W = 1920;
|
||
var H = 1080;
|
||
|
||
var root = document.getElementById("af-root");
|
||
var V = (window.__hyperframes && window.__hyperframes.getVariables()) || {};
|
||
var CS = getComputedStyle(root);
|
||
|
||
// A declared variable reaches CSS as a custom property on the root.
|
||
// Read that first so a host stylesheet can retheme the block, then
|
||
// fall back to the declared value. Both the namespaced and the bare
|
||
// property name are probed: which one the runtime emits has changed
|
||
// across versions, and the block should not care.
|
||
function raw(id) {
|
||
var slug = id.toLowerCase();
|
||
var css =
|
||
CS.getPropertyValue("--hf-var-" + slug).trim() ||
|
||
CS.getPropertyValue("--" + slug).trim();
|
||
return css !== "" ? css : V[id];
|
||
}
|
||
function num(id, fallback) {
|
||
var n = parseFloat(raw(id));
|
||
return isFinite(n) ? n : fallback;
|
||
}
|
||
function str(id, fallback) {
|
||
var s = raw(id);
|
||
return typeof s === "string" && s !== "" ? s : fallback;
|
||
}
|
||
|
||
// ---------------------------------------------------------------
|
||
// Measured constants.
|
||
//
|
||
// Read from the three.js anamorphic post-processing example (MIT),
|
||
// cross-checked against a live capture of that demo's own GUI:
|
||
//
|
||
// tint 0x7a8aff, threshold 0.3, intensity 5, bloom radius 0,
|
||
// samples 80, time scale 0.5, resolution scale 0.25.
|
||
//
|
||
// The filter walks samples/2 taps in each direction HORIZONTALLY
|
||
// only, weights each tap by 1 - |i| / halfSamples (a tent), and
|
||
// normalises the sum by samples / 3.0. Dividing by samples/3 rather
|
||
// than by the weight sum is deliberate: it is a gain above 1, not
|
||
// an average, which is why a dim highlight still throws a readable
|
||
// streak.
|
||
//
|
||
// This implementation evaluates that convolution in closed form
|
||
// instead of running the 80-tap loop. For a small bright source the
|
||
// tent-weighted horizontal blur has an exact analytic result: the
|
||
// tent itself, centred on the source. So one horizontal gradient
|
||
// per highlight is the same arithmetic the loop would perform, at
|
||
// a fraction of the cost, and with no dependence on WebGL surviving
|
||
// seek capture.
|
||
// ---------------------------------------------------------------
|
||
var SAMPLES = 80;
|
||
var HALF_SAMPLES = SAMPLES / 2; // 40 taps each side
|
||
var RES_SCALE = 0.25; // the streak buffer runs at quarter resolution
|
||
var NORM = SAMPLES / 3; // total / (samples / 3.0)
|
||
var REF_W = 1280; // width the constants were measured at
|
||
|
||
// 40 taps of a 1/(0.25 * 1280) texel step = 12.5% of frame width per
|
||
// side. Held as a fraction rather than as absolute pixels so the look
|
||
// matches the reference at any output size.
|
||
var HALF_FRAC = HALF_SAMPLES / (RES_SCALE * REF_W);
|
||
|
||
var INTENSITY = num("intensity", 5);
|
||
var THRESHOLD = num("threshold", 0.3);
|
||
var STREAK_LEN = num("streakLength", 1);
|
||
var BLOOM_R = num("bloomRadius", 0);
|
||
var TIME_SCALE = num("timeScale", 0.5);
|
||
var COUNT = Math.max(1, Math.round(num("emitters", 140)));
|
||
var BACKDROP = str("backdrop", "#04040a");
|
||
var TINT = hexRgb(str("tint", "#7a8aff"), [0.478, 0.541, 1.0]);
|
||
|
||
function hexRgb(hex, fallback) {
|
||
var m = /^#?([0-9a-f]{6})$/i.exec(String(hex).trim());
|
||
if (!m) return fallback;
|
||
var n = parseInt(m[1], 16);
|
||
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];
|
||
}
|
||
|
||
function mulberry32(seed) {
|
||
return function () {
|
||
seed |= 0;
|
||
seed = (seed + 0x6d2b79f5) | 0;
|
||
var t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
|
||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||
};
|
||
}
|
||
|
||
function hsv(h, s, v) {
|
||
var hh = ((((h % 360) + 360) % 360) / 60) % 6;
|
||
var c = v * s;
|
||
var x = c * (1 - Math.abs((hh % 2) - 1));
|
||
var m = v - c;
|
||
var r = 0;
|
||
var g = 0;
|
||
var b = 0;
|
||
if (hh < 1) {
|
||
r = c;
|
||
g = x;
|
||
} else if (hh < 2) {
|
||
r = x;
|
||
g = c;
|
||
} else if (hh < 3) {
|
||
g = c;
|
||
b = x;
|
||
} else if (hh < 4) {
|
||
g = x;
|
||
b = c;
|
||
} else if (hh < 5) {
|
||
r = x;
|
||
b = c;
|
||
} else {
|
||
r = c;
|
||
b = x;
|
||
}
|
||
return [r + m, g + m, b + m];
|
||
}
|
||
|
||
// ---------------------------------------------------------------
|
||
// The emitter field. Every emitter is a closed-form function of t:
|
||
// two incommensurate sinusoids for position and one for brightness.
|
||
// The seeded PRNG runs once, at setup, so the field is identical on
|
||
// every load and every seek.
|
||
// ---------------------------------------------------------------
|
||
var rng = mulberry32(1337);
|
||
var EM = [];
|
||
for (var i = 0; i < COUNT; i++) {
|
||
EM.push({
|
||
x0: rng() * W,
|
||
y0: rng() * H,
|
||
ax: 24 + rng() * 90,
|
||
ay: 12 + rng() * 48,
|
||
fx: 0.018 + rng() * 0.05,
|
||
fy: 0.015 + rng() * 0.045,
|
||
px: rng(),
|
||
py: rng(),
|
||
r: 3 + Math.pow(rng(), 1.6) * 10,
|
||
h: rng() * 360,
|
||
s: 0.62 + rng() * 0.38,
|
||
v: 0.7 + rng() * 0.3,
|
||
fb: 0.03 + rng() * 0.09,
|
||
pb: rng(),
|
||
});
|
||
}
|
||
|
||
var TAU = Math.PI * 2;
|
||
var canvas = document.getElementById("af-canvas");
|
||
var ctx = canvas.getContext("2d");
|
||
|
||
// Streaks are accumulated in a quarter-resolution buffer, matching
|
||
// the reference's setResolutionScale(0.25). Upscaling it bilinearly
|
||
// is what gives the streak its soft vertical falloff, and it is the
|
||
// reason the streak reads as smooth rather than as a hard bar.
|
||
var SW = Math.round(W * RES_SCALE);
|
||
var SH = Math.round(H * RES_SCALE);
|
||
var sbuf = document.createElement("canvas");
|
||
sbuf.width = SW;
|
||
sbuf.height = SH;
|
||
var sctx = sbuf.getContext("2d");
|
||
|
||
function rgba(c, a) {
|
||
return (
|
||
"rgba(" +
|
||
Math.round(Math.min(1, Math.max(0, c[0])) * 255) +
|
||
"," +
|
||
Math.round(Math.min(1, Math.max(0, c[1])) * 255) +
|
||
"," +
|
||
Math.round(Math.min(1, Math.max(0, c[2])) * 255) +
|
||
"," +
|
||
Math.min(1, Math.max(0, a)) +
|
||
")"
|
||
);
|
||
}
|
||
|
||
// A slow swell across the clip so the field reads as a shot rather
|
||
// than as a loop. Closed form in t, like everything else here.
|
||
function gainAt(t) {
|
||
var u = Math.min(1, Math.max(0, t / DUR));
|
||
return 0.75 + 0.35 * Math.sin(Math.PI * u);
|
||
}
|
||
|
||
function draw(t) {
|
||
var tau = t * TIME_SCALE;
|
||
var gain = gainAt(t);
|
||
var halfLen = HALF_FRAC * W * STREAK_LEN;
|
||
|
||
sctx.setTransform(1, 0, 0, 1, 0, 0);
|
||
sctx.clearRect(0, 0, SW, SH);
|
||
sctx.globalCompositeOperation = "lighter";
|
||
|
||
var e;
|
||
var k;
|
||
var col;
|
||
var bright;
|
||
var x;
|
||
var y;
|
||
for (k = 0; k < EM.length; k++) {
|
||
e = EM[k];
|
||
x = e.x0 + e.ax * Math.sin(TAU * (e.fx * tau + e.px));
|
||
y = e.y0 + e.ay * Math.sin(TAU * (e.fy * tau + e.py));
|
||
bright = e.v * (0.62 + 0.38 * Math.sin(TAU * (e.fb * tau + e.pb)));
|
||
col = hsv(e.h, e.s, Math.max(0, bright));
|
||
|
||
// High pass, then tint, exactly as the reference filter does:
|
||
// max(colour - threshold, 0) * tint. Below threshold there is no
|
||
// streak at all, which is what keeps the field from smearing.
|
||
var hp = [
|
||
Math.max(0, col[0] - THRESHOLD) * TINT[0],
|
||
Math.max(0, col[1] - THRESHOLD) * TINT[1],
|
||
Math.max(0, col[2] - THRESHOLD) * TINT[2],
|
||
];
|
||
var peak = Math.max(hp[0], Math.max(hp[1], hp[2]));
|
||
if (peak <= 0.0001) continue;
|
||
|
||
// Peak of the tent equals the source value times intensity over
|
||
// the samples/3 normaliser. Magnitude is carried in alpha so the
|
||
// hue keeps full 8-bit precision at low brightness.
|
||
var alpha = (peak * INTENSITY * gain) / NORM;
|
||
if (alpha < 0.003) continue;
|
||
var hue = [hp[0] / peak, hp[1] / peak, hp[2] / peak];
|
||
|
||
var bx = x * RES_SCALE;
|
||
var by = y * RES_SCALE;
|
||
var bh = Math.max(1, e.r * 2 * RES_SCALE);
|
||
var bl = halfLen * RES_SCALE;
|
||
|
||
var g = sctx.createLinearGradient(bx - bl, 0, bx + bl, 0);
|
||
g.addColorStop(0, rgba(hue, 0));
|
||
g.addColorStop(0.5, rgba(hue, alpha));
|
||
g.addColorStop(1, rgba(hue, 0));
|
||
sctx.fillStyle = g;
|
||
sctx.fillRect(bx - bl, by - bh / 2, bl * 2, bh);
|
||
}
|
||
|
||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||
ctx.globalCompositeOperation = "source-over";
|
||
ctx.fillStyle = BACKDROP;
|
||
ctx.fillRect(0, 0, W, H);
|
||
|
||
ctx.globalCompositeOperation = "lighter";
|
||
ctx.imageSmoothingEnabled = true;
|
||
ctx.imageSmoothingQuality = "high";
|
||
ctx.drawImage(sbuf, 0, 0, SW, SH, 0, 0, W, H);
|
||
|
||
// The highlights themselves, drawn at full resolution on top.
|
||
for (k = 0; k < EM.length; k++) {
|
||
e = EM[k];
|
||
x = e.x0 + e.ax * Math.sin(TAU * (e.fx * tau + e.px));
|
||
y = e.y0 + e.ay * Math.sin(TAU * (e.fy * tau + e.py));
|
||
bright = e.v * (0.62 + 0.38 * Math.sin(TAU * (e.fb * tau + e.pb)));
|
||
col = hsv(e.h, e.s, Math.max(0, bright));
|
||
|
||
if (BLOOM_R > 0) {
|
||
var br = e.r + BLOOM_R;
|
||
var gb = ctx.createRadialGradient(x, y, 0, x, y, br);
|
||
gb.addColorStop(0, rgba(col, 0.5 * gain));
|
||
gb.addColorStop(0.35, rgba(col, 0.16 * gain));
|
||
gb.addColorStop(1, rgba(col, 0));
|
||
ctx.fillStyle = gb;
|
||
ctx.fillRect(x - br, y - br, br * 2, br * 2);
|
||
}
|
||
|
||
var core = ctx.createRadialGradient(x, y, 0, x, y, e.r);
|
||
core.addColorStop(
|
||
0,
|
||
rgba([col[0] * 0.88 + 0.12, col[1] * 0.88 + 0.12, col[2] * 0.88 + 0.12], 1),
|
||
);
|
||
core.addColorStop(0.72, rgba(col, 0.96));
|
||
core.addColorStop(1, rgba(col, 0));
|
||
ctx.fillStyle = core;
|
||
ctx.fillRect(x - e.r, y - e.r, e.r * 2, e.r * 2);
|
||
}
|
||
}
|
||
|
||
window.__timelines = window.__timelines || {};
|
||
var tl = gsap.timeline({ paused: true });
|
||
|
||
// The canvas is repainted from a property SETTER, not from a
|
||
// timeline onUpdate callback: gsap suppresses events on seek(), so a
|
||
// callback-driven canvas silently freezes on frame 0 under any
|
||
// consumer that scrubs. Tweened values are written on every render,
|
||
// suppressed or not, so the setter fires on seek and hands us the
|
||
// frame time directly.
|
||
var driver = { _t: 0 };
|
||
Object.defineProperty(driver, "t", {
|
||
get: function () {
|
||
return this._t;
|
||
},
|
||
set: function (v) {
|
||
this._t = v;
|
||
draw(v);
|
||
},
|
||
});
|
||
tl.to(driver, { t: DUR, duration: DUR, ease: "none" }, 0);
|
||
window.__timelines["vfx-anamorphic-flare"] = tl;
|
||
|
||
draw(0);
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
</Accordion>
|
||
|
||
{/* hf:generated-footer */}
|
||
|
||
Tagged `vfx` `background` `showcase` `optical` `canvas`.
|
||
|
||
## Related topics
|
||
|
||
- [Browse the complete Catalog](/catalog)
|
||
- [Add assets and Catalog items in Studio](/studio/assets-and-blocks)
|
||
- [Build a richer composition](/go-further)
|