Crackling Lightning Border
裂变闪电边框
A continuously deformed outline surrounds the card with jagged strands and small offshoots of current.
Where to use it
Highlight a small featured tile in a technical or playful interface. Reserve the animated edge for a single highlight, and keep its branches out of the area used for copy and controls.
Practical example
A sci-fi board game shop frames a limited expansion card with a flickering electric outline. Short arcs branch off around the edge while the box title and preorder button sit inside a stable panel.
AI prompt
Add a crackling lightning border to [panel]. Sample a closed perimeter and perturb it using seeded continuous variation. Draw one bright core plus a wider translucent halo and short branches. Confine the effect to its perimeter, avoid full-area flashes, and freeze the drawing for reduced motion.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Crackling Lightning Border</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.specimen {
display: grid;
width: min(100%, 356px);
gap: 8px;
margin: 0;
justify-items: center;
}
.specimen canvas {
display: block;
width: 100%;
aspect-ratio: 2;
max-height: max(72px, calc(100vh - 65px));
border-radius: 11px;
background: linear-gradient(145deg, #232943, #14172c);
box-shadow: 0 8px 22px #090a16a8;
}
.specimen label {
display: grid;
width: 100%;
min-height: 25px;
grid-template-columns: minmax(0, auto) minmax(56px, 1fr);
align-items: center;
gap: 8px;
color: var(--demo-ink);
font-size: 12px;
}
.specimen input {
width: 100%;
margin: 0;
accent-color: #b9a6ff;
}
@media (prefers-reduced-motion: reduce) {
.specimen [data-loop-toggle] { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<figure class="specimen">
<canvas role="img" aria-label="Crackling Lightning Border" width="288" height="144">Crackling Lightning Border</canvas>
<button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">Play demo</button>
</figure>
</main>
<script>
(() => {
const canvas = document.querySelector('.specimen canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
document.querySelector('.specimen :is(input, button)').disabled = true;
return;
}
const width = 288;
const height = 144;
const turn = Math.PI * 2;
let elapsed = 0;
let variation = 0;
function dot(x, y, radius, ink) {
ctx.fillStyle = ink;
ctx.beginPath();
ctx.arc(x, y, radius, 0, turn);
ctx.fill();
}
function mix(a, b, amount) {
return a + (b - a) * amount;
}
const corners = [[51, 29], [237, 29], [237, 115], [51, 115]];
const pieceCount = 28;
function draw(time) {
ctx.fillStyle = '#1c1a3b';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = '#20274a';
ctx.fillRect(54, 32, 180, 80);
const perimeter = [];
for (let side = 0; side < corners.length; side++) {
const start = corners[side];
const end = corners[(side + 1) % corners.length];
for (let i = 0; i < pieceCount; i++) {
const position = i / pieceCount;
const jitter = (i === 0 ? 0 : 1) * (2.6 * Math.sin(i * 2.41 + time * 5 + side) + Math.sin(i * 5.9 - time * 7.2));
perimeter.push([
mix(start[0], end[0], position) + (side % 2 ? jitter : 0),
mix(start[1], end[1], position) + (side % 2 ? 0 : jitter),
]);
}
}
ctx.beginPath();
perimeter.forEach(([x, y], i) => i ? ctx.lineTo(x, y) : ctx.moveTo(x, y));
ctx.closePath();
ctx.lineJoin = 'round';
ctx.lineWidth = 8;
ctx.strokeStyle = '#7e86ff19';
ctx.stroke();
ctx.lineWidth = 3.8;
ctx.strokeStyle = '#94a8ff5c';
ctx.stroke();
ctx.lineWidth = 1.15;
ctx.strokeStyle = '#dcecff';
ctx.stroke();
ctx.strokeStyle = '#b6aafb95';
for (let i = 9; i < perimeter.length; i += 14) {
const [x, y] = perimeter[i];
const side = Math.floor(i / pieceCount);
const dx = side === 1 ? 1 : side === 3 ? -1 : 0;
const dy = side === 0 ? -1 : side === 2 ? 1 : 0;
const reach = 5 + 8 * (.5 + .5 * Math.sin(time * 4.5 + i));
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + dx * reach * .58 + dy * 5, y + dy * reach * .58 - dx * 5);
ctx.lineTo(x + dx * reach + dy * 9, y + dy * reach - dx * 9);
ctx.stroke();
}
ctx.fillStyle = '#b6adfe';
ctx.beginPath();
ctx.moveTo(148, 55);
ctx.lineTo(133, 76);
ctx.lineTo(144, 76);
ctx.lineTo(140, 91);
ctx.lineTo(156, 70);
ctx.lineTo(146, 70);
ctx.closePath();
ctx.fill();
}
function render() {
const ratio = Math.min(devicePixelRatio || 1, 2);
const pixelsWide = Math.max(1, Math.round(canvas.clientWidth * ratio));
const pixelsTall = Math.max(1, Math.round(canvas.clientHeight * ratio));
if (canvas.width !== pixelsWide || canvas.height !== pixelsTall) {
canvas.width = pixelsWide;
canvas.height = pixelsTall;
}
ctx.setTransform(pixelsWide / width, 0, 0, pixelsTall / height, 0, 0);
ctx.clearRect(0, 0, width, height);
ctx.save();
draw(elapsed, variation);
ctx.restore();
}
render();
new ResizeObserver(render).observe(canvas);
const button = document.querySelector('[data-loop-toggle]');
const preference = matchMedia('(prefers-reduced-motion: reduce)');
let playing = false;
let intersecting = false;
let pendingFrame = null;
let previousTime = 0;
function canAnimate() {
return playing && intersecting && !preference.matches && !document.hidden;
}
function tick(now) {
pendingFrame = null;
if (!canAnimate()) return;
elapsed += Math.min(Math.max(0, now - previousTime), 45) / 1000;
previousTime = now;
render();
pendingFrame = requestAnimationFrame(tick);
}
function syncPlayback() {
if (pendingFrame !== null) cancelAnimationFrame(pendingFrame);
pendingFrame = null;
if (canAnimate()) {
previousTime = performance.now();
pendingFrame = requestAnimationFrame(tick);
}
}
button.addEventListener('click', () => {
playing = !playing;
button.textContent = playing ? "Pause demo" : "Play demo";
button.closest('.specimen').toggleAttribute('data-playing', playing);
syncPlayback();
});
new IntersectionObserver(([entry]) => {
intersecting = entry.isIntersecting;
syncPlayback();
}).observe(canvas);
preference.addEventListener('change', syncPlayback);
document.addEventListener('visibilitychange', syncPlayback);
})();
</script>
</body>
</html>Original example by UI Skills