Embers
A dark panel with real fire along its bottom edge and sparks rising off it. Painted by a shader.
You are on fire.
Fire along the bottom edge and sparks rising off it, for a streak or a win.
Options
Code
import { useEffect, useRef } from "react";
import "./embers.css";
const FRAGMENT = `precision highp float;
uniform vec2 u_size;
uniform float u_time;
uniform vec3 u_colour;
#define PX (1.0 / u_size.y)
float hash(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
float noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float value = 0.0;
float weight = 0.5;
for (int i = 0; i < 5; i++) {
value += weight * noise(p);
p = p * 2.02 + vec2(17.1, 9.2);
weight *= 0.5;
}
return value;
}
float grain() {
return hash(gl_FragCoord.xy * 0.731 + floor(u_time * 12.0) * 1.37) - 0.5;
}
vec3 scene(vec2 uv, float aspect, float t, vec3 colour) {
vec2 p = vec2(uv.x * aspect, uv.y);
vec3 hot = mix(colour, vec3(1.0, 0.8, 0.28), 0.78);
float low = fbm(vec2(p.x * 2.4, p.y * 1.9 - t * 0.85));
float high = fbm(vec2(p.x * 5.2 + 4.0, p.y * 3.4 - t * 1.6));
float heat = (low * 0.65 + high * 0.35) * 1.95 - uv.y * 3.3 + 0.12;
vec3 c = vec3(0.075, 0.066, 0.062);
c += colour * 0.42 * exp(-uv.y * 3.0);
c = mix(c, colour * 0.55, smoothstep(-0.25, 0.35, heat) * 0.55);
c = mix(c, colour, smoothstep(0.1, 0.75, heat));
c = mix(c, hot, smoothstep(0.6, 1.35, heat));
for (int layer = 0; layer < 2; layer++) {
float depth = float(layer);
vec2 g = vec2(p.x * (8.0 + depth * 6.0), uv.y * (4.5 + depth * 3.0) - t * (0.45 + depth * 0.3));
g.x += sin(g.y * 1.3 + depth * 2.0 + t * 0.6) * 0.28;
vec2 cell = floor(g);
float chance = hash(cell + depth * 31.0);
vec2 at = vec2(0.2 + 0.6 * hash(cell + 3.1), 0.2 + 0.6 * hash(cell + 7.7));
vec2 away = (fract(g) - at) * vec2(1.0, 0.42);
float spark = step(0.7, chance) * smoothstep(0.034, 0.0, length(away));
spark *= (1.0 - uv.y) * (0.45 + 0.55 * sin(t * 3.0 + chance * 40.0));
c += hot * spark * 1.3;
}
return c + grain() * 0.03;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_size;
gl_FragColor = vec4(scene(uv, u_size.x / u_size.y, u_time, u_colour), 1.0);
}`;
export function Embers({ children }) {
const ref = useRef(null);
useEffect(() => {
const canvas = ref.current;
const gl = canvas.getContext("webgl", { antialias: false });
if (!gl) return;
const compile = (type, source) => {
const part = gl.createShader(type);
gl.shaderSource(part, source);
gl.compileShader(part);
return part;
};
const program = gl.createProgram();
gl.attachShader(program, compile(gl.VERTEX_SHADER, "attribute vec2 corner; void main() { gl_Position = vec4(corner, 0.0, 1.0); }"));
gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));
gl.linkProgram(program);
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const corner = gl.getAttribLocation(program, "corner");
gl.enableVertexAttribArray(corner);
gl.vertexAttribPointer(corner, 2, gl.FLOAT, false, 0, 0);
const size = gl.getUniformLocation(program, "u_size");
const time = gl.getUniformLocation(program, "u_time");
const tone = gl.getUniformLocation(program, "u_colour");
const still = matchMedia("(prefers-reduced-motion: reduce)").matches;
let frame = 0;
let seen = true;
const draw = (now) => {
frame = 0;
const css = getComputedStyle(canvas).color;
const parts = css.match(/[\d.]+/g).slice(0, 3).map(Number);
gl.uniform3fv(tone, css.startsWith("color(") ? parts : parts.map((part) => part / 255));
gl.uniform2f(size, canvas.width, canvas.height);
gl.uniform1f(time, still ? 8 : now / 1000);
gl.drawArrays(gl.TRIANGLES, 0, 3);
if (seen && !still) frame = requestAnimationFrame(draw);
};
const start = () => {
if (!frame) frame = requestAnimationFrame(draw);
};
const sized = new ResizeObserver(() => {
canvas.width = canvas.clientWidth * 1;
canvas.height = canvas.clientHeight * 1;
gl.viewport(0, 0, canvas.width, canvas.height);
start();
});
const watched = new IntersectionObserver(([entry]) => {
seen = entry.isIntersecting;
if (seen) start();
});
sized.observe(canvas);
watched.observe(canvas);
return () => {
cancelAnimationFrame(frame);
sized.disconnect();
watched.disconnect();
};
}, []);
return (
<section className="dh-embers">
<canvas ref={ref} className="dh-embers-art" aria-hidden="true" />
<div className="dh-embers-content">{children}</div>
</section>
);
}.dh-embers {
position: relative;
display: grid;
place-items: center;
overflow: hidden;
min-height: 720px;
background: #131110;
color: #fff;
font-size: 14px;
line-height: 1.5;
}
.dh-embers-art {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
/* The drawing takes this colour. */
color: var(--dh-accent, #ff451a);
pointer-events: none;
}
.dh-embers-content {
position: relative;
isolation: isolate;
padding: 32px;
text-align: center;
}
.dh-embers h1 {
margin: 0;
font-size: 44px;
font-weight: 500;
line-height: 1.08;
letter-spacing: -0.04em;
}
.dh-embers p {
max-width: 360px;
margin: 14px auto 0;
color: rgb(255 255 255 / 0.72);
font-size: 15px;
}
@media (max-width: 640px) {
.dh-embers h1 { font-size: 32px; }
}
.dh-embers-content {
margin-bottom: 9%;
}<section class="dh-embers">
<canvas class="dh-embers-art" aria-hidden="true"></canvas>
<div class="dh-embers-content">
<h1>You are on fire.</h1>
<p>Fire along the bottom edge and sparks rising off it, for a streak or a win.</p>
</div>
</section>.dh-embers {
position: relative;
display: grid;
place-items: center;
overflow: hidden;
min-height: 720px;
background: #131110;
color: #fff;
font-size: 14px;
line-height: 1.5;
}
.dh-embers-art {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
/* The drawing takes this colour. */
color: var(--dh-accent, #ff451a);
pointer-events: none;
}
.dh-embers-content {
position: relative;
isolation: isolate;
padding: 32px;
text-align: center;
}
.dh-embers h1 {
margin: 0;
font-size: 44px;
font-weight: 500;
line-height: 1.08;
letter-spacing: -0.04em;
}
.dh-embers p {
max-width: 360px;
margin: 14px auto 0;
color: rgb(255 255 255 / 0.72);
font-size: 15px;
}
@media (max-width: 640px) {
.dh-embers h1 { font-size: 32px; }
}
.dh-embers-content {
margin-bottom: 9%;
}{
const FRAGMENT = `precision highp float;
uniform vec2 u_size;
uniform float u_time;
uniform vec3 u_colour;
#define PX (1.0 / u_size.y)
float hash(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
float noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float value = 0.0;
float weight = 0.5;
for (int i = 0; i < 5; i++) {
value += weight * noise(p);
p = p * 2.02 + vec2(17.1, 9.2);
weight *= 0.5;
}
return value;
}
float grain() {
return hash(gl_FragCoord.xy * 0.731 + floor(u_time * 12.0) * 1.37) - 0.5;
}
vec3 scene(vec2 uv, float aspect, float t, vec3 colour) {
vec2 p = vec2(uv.x * aspect, uv.y);
vec3 hot = mix(colour, vec3(1.0, 0.8, 0.28), 0.78);
float low = fbm(vec2(p.x * 2.4, p.y * 1.9 - t * 0.85));
float high = fbm(vec2(p.x * 5.2 + 4.0, p.y * 3.4 - t * 1.6));
float heat = (low * 0.65 + high * 0.35) * 1.95 - uv.y * 3.3 + 0.12;
vec3 c = vec3(0.075, 0.066, 0.062);
c += colour * 0.42 * exp(-uv.y * 3.0);
c = mix(c, colour * 0.55, smoothstep(-0.25, 0.35, heat) * 0.55);
c = mix(c, colour, smoothstep(0.1, 0.75, heat));
c = mix(c, hot, smoothstep(0.6, 1.35, heat));
for (int layer = 0; layer < 2; layer++) {
float depth = float(layer);
vec2 g = vec2(p.x * (8.0 + depth * 6.0), uv.y * (4.5 + depth * 3.0) - t * (0.45 + depth * 0.3));
g.x += sin(g.y * 1.3 + depth * 2.0 + t * 0.6) * 0.28;
vec2 cell = floor(g);
float chance = hash(cell + depth * 31.0);
vec2 at = vec2(0.2 + 0.6 * hash(cell + 3.1), 0.2 + 0.6 * hash(cell + 7.7));
vec2 away = (fract(g) - at) * vec2(1.0, 0.42);
float spark = step(0.7, chance) * smoothstep(0.034, 0.0, length(away));
spark *= (1.0 - uv.y) * (0.45 + 0.55 * sin(t * 3.0 + chance * 40.0));
c += hot * spark * 1.3;
}
return c + grain() * 0.03;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_size;
gl_FragColor = vec4(scene(uv, u_size.x / u_size.y, u_time, u_colour), 1.0);
}`;
document.querySelectorAll(".dh-embers-art").forEach((canvas) => {
if (canvas.dataset.ready) return;
canvas.dataset.ready = "true";
const gl = canvas.getContext("webgl", { antialias: false });
if (!gl) return;
const compile = (type, source) => {
const part = gl.createShader(type);
gl.shaderSource(part, source);
gl.compileShader(part);
return part;
};
const program = gl.createProgram();
gl.attachShader(program, compile(gl.VERTEX_SHADER, "attribute vec2 corner; void main() { gl_Position = vec4(corner, 0.0, 1.0); }"));
gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));
gl.linkProgram(program);
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const corner = gl.getAttribLocation(program, "corner");
gl.enableVertexAttribArray(corner);
gl.vertexAttribPointer(corner, 2, gl.FLOAT, false, 0, 0);
const size = gl.getUniformLocation(program, "u_size");
const time = gl.getUniformLocation(program, "u_time");
const tone = gl.getUniformLocation(program, "u_colour");
const still = matchMedia("(prefers-reduced-motion: reduce)").matches;
let frame = 0;
let seen = true;
const draw = (now) => {
frame = 0;
const css = getComputedStyle(canvas).color;
const parts = css.match(/[\d.]+/g).slice(0, 3).map(Number);
gl.uniform3fv(tone, css.startsWith("color(") ? parts : parts.map((part) => part / 255));
gl.uniform2f(size, canvas.width, canvas.height);
gl.uniform1f(time, still ? 8 : now / 1000);
gl.drawArrays(gl.TRIANGLES, 0, 3);
if (seen && !still) frame = requestAnimationFrame(draw);
};
const start = () => {
if (!frame) frame = requestAnimationFrame(draw);
};
const sized = new ResizeObserver(() => {
canvas.width = canvas.clientWidth * 1;
canvas.height = canvas.clientHeight * 1;
gl.viewport(0, 0, canvas.width, canvas.height);
start();
});
const watched = new IntersectionObserver(([entry]) => {
seen = entry.isIntersecting;
if (seen) start();
});
sized.observe(canvas);
watched.observe(canvas);
});
}Embers is not in the Figma file yet.
Use the React or HTML code for now.