Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 282 additions & 0 deletions apps/docs/app/home-prototype/dot-mark.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
"use client";

// Adapted from coder/coder-site packages/ui/components/FooterDotMark.tsx, grid5 pattern only.
import { onMotionPreferenceChange, prefersReducedMotion } from "@coder/docs-ui";
import { useEffect, useRef } from "react";

const PATHS = [
"M0 8.5C0 3.61351 4.15135 0.5 9.85946 0.5C15.5676 0.5 18.7676 3.2027 18.8757 7.18108L13.9459 7.33243C13.8162 5.12703 11.8595 3.67838 9.85946 3.72162C7.11351 3.77568 5.08108 5.6027 5.08108 8.5C5.08108 11.3973 7.11351 13.1919 9.85946 13.1919C11.8595 13.1919 13.773 11.8081 13.9892 9.6027L18.9189 9.71081C18.7892 13.7541 15.3946 16.5 9.85946 16.5C4.32433 16.5 0 13.3649 0 8.5Z",
"M20.6593 8.5C20.6593 3.67838 24.562 0.5 30.1728 0.5C35.7837 0.5 39.6864 3.65676 39.6864 8.5C39.6864 13.3432 35.7837 16.5 30.1728 16.5C24.562 16.5 20.6593 13.3216 20.6593 8.5ZM25.7404 8.5C25.7404 11.3108 27.5674 13.2351 30.1728 13.2351C32.7999 13.2351 34.6053 11.3541 34.6053 8.5C34.6053 5.64595 32.7783 3.67838 30.1728 3.67838C27.5674 3.67838 25.7404 5.68919 25.7404 8.5Z",
"M41.4269 16.0676V0.932454H47.6971C53.7728 0.932454 57.8593 3.76489 57.8593 8.50002C57.8593 13.2352 53.7728 16.0676 47.6971 16.0676H41.4269ZM46.3998 13.0406H47.4809C50.6809 13.0406 52.8215 11.3973 52.8215 8.50002C52.8215 5.60273 50.6809 3.93786 47.4809 3.93786H46.3998V13.0406Z",
"M64.5407 4.06759H72.3245V0.932454H59.611V16.0676H72.4758V12.8243H64.5407V9.84056H71.3515V6.96489H64.5407V4.06759Z",
"M84.8971 9.04056C87.8917 9.23516 89.3079 10.7811 89.8917 13.1487L90.4971 16.0676H84.8106L84.3349 13.3649C83.9241 11.6135 82.5187 10.9865 80.9404 10.9865H79.6214V16.0676H74.6485V0.932454H82.5404C87.0809 0.932454 89.8485 2.59732 89.8485 5.30002C89.8485 7.52705 87.9674 8.91083 84.8971 8.91083V9.04056ZM79.6214 8.32705H82.1079C83.816 8.32705 84.8755 7.46218 84.8755 6.0784C84.8755 4.69462 83.816 3.85137 82.1079 3.85137H79.6214V8.32705Z",
"M92.6811 0.932454H105.654V16.0676H92.6811V0.932454Z",
];
const VB_W = 106;
const VB_H = 17;
const TILE = 5;
const DOT_R = 0.75;
const INK_ALPHA = 0.36;
const SPRING_K = 0.04;
const DAMP = 0.78;
const REPEL_RADIUS = 90;
const REPEL_FORCE = 5;
const MAX_SPEED = 10;
const PAD = 60;

// Straight edges in viewBox units, weighted; the grid is shifted so they fall between dot rows.
const EDGES_Y: [number, number][] = [
[7.25, 4],
[9.66, 4],
[0.93, 2],
[16.07, 2],
[4.0, 1],
[6.96, 1],
[9.84, 1],
[12.93, 1],
];
const EDGES_X: [number, number][] = [
[13.97, 3],
[18.9, 3],
[41.43, 1],
[46.4, 1],
[59.61, 1],
[64.54, 1],
[74.65, 1],
[79.62, 1],
[92.68, 1],
[105.65, 1],
];

const svgUrl = (svg: string) => `url("data:image/svg+xml,${encodeURIComponent(svg)}")`;
const LOGO = svgUrl(
`<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ${VB_W} ${VB_H}'>${PATHS.map((d) => `<path fill-rule='evenodd' d='${d}'/>`).join("")}</svg>`,
);
const DOT_TILE = svgUrl(
`<svg xmlns='http://www.w3.org/2000/svg' width='${TILE}' height='${TILE}' viewBox='0 0 ${TILE} ${TILE}'><circle cx='${DOT_R}' cy='${DOT_R}' r='${DOT_R}'/></svg>`,
);

type Dot = { x: number; y: number; ox: number; oy: number; vx: number; vy: number };

// The C's cut ends are levelled for the inside test, so each arm keeps its corner dot.
function insideTestPath() {
const path = new Path2D();
PATHS.forEach((d, index) => {
const level =
index === 0
? d
.replace("18.8757 7.18108L13.9459 7.33243", "18.8757 7.25676L13.9459 7.25676")
.replace("13.9892 9.6027L18.9189 9.71081", "13.9892 9.65676L18.9189 9.65676")
: d;
path.addPath(new Path2D(level));
});
return path;
}

function phase(edges: [number, number][], scale: number) {
let best = 0;
let bestCost = Infinity;
for (let shift = 0; shift < TILE; shift += 0.125) {
let cost = 0;
for (const [edge, weight] of edges) {
const offset = (((edge * scale - shift) % TILE) + TILE) % TILE;
cost += weight * Math.max(0, TILE / 2 - Math.min(offset, TILE - offset));
}
if (cost < bestCost) [best, bestCost] = [shift, cost];
}
return best;
}

/** The Coder wordmark as a dot field that moves away from the pointer; it stays still under reduced motion. */
export function DotMark({ className }: { className?: string }) {
const wrapRef = useRef<HTMLDivElement>(null);
const layerRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);

useEffect(() => {
const wrap = wrapRef.current;
const layer = layerRef.current;
const canvas = canvasRef.current;
const ctx = canvas?.getContext("2d");
if (!wrap || !layer || !canvas || !ctx) return;

const sw = VB_W * 24;
const sh = VB_H * 24;
const raster = document.createElement("canvas");
raster.width = sw;
raster.height = sh;
const rctx = raster.getContext("2d");
if (!rctx) return;
rctx.scale(24, 24);
rctx.fill(insideTestPath(), "evenodd");
const alpha = rctx.getImageData(0, 0, sw, sh).data;
const inside = (u: number, v: number) => {
const x = Math.min(sw - 1, Math.max(0, Math.floor(u * sw)));
const y = Math.min(sh - 1, Math.max(0, Math.floor(v * sh)));
return alpha[(y * sw + x) * 4 + 3] > 127;
};

const state = {
dots: [] as Dot[],
w: 0,
h: 0,
dpr: 1,
mx: -1e6,
my: -1e6,
strength: 0,
moving: false,
active: false,
raf: 0,
};

const build = () => {
const rect = layer.getBoundingClientRect();
state.w = rect.width;
state.h = rect.height;
state.dpr = window.devicePixelRatio || 1;
canvas.width = Math.round((state.w + PAD * 2) * state.dpr);
canvas.height = Math.round((state.h + PAD * 2) * state.dpr);
canvas.style.width = `${canvas.width / state.dpr}px`;
canvas.style.height = `${canvas.height / state.dpr}px`;
const scale = state.w / VB_W;
const px = phase(EDGES_X, scale);
const py = phase(EDGES_Y, scale);
const dots: Dot[] = [];
for (let y = py - TILE + DOT_R; y <= state.h; y += TILE) {
for (let x = px - TILE + DOT_R; x <= state.w; x += TILE) {
if (x >= 0 && y >= 0 && inside(x / state.w, y / state.h)) {
dots.push({ x, y, ox: x, oy: y, vx: 0, vy: 0 });
}
}
}
state.dots = dots;
};

const draw = () => {
if (!state.moving && state.strength > 0) state.strength = Math.max(0, state.strength - 0.018);
const radius = REPEL_RADIUS * state.strength;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = getComputedStyle(layer).color;
ctx.globalAlpha = INK_ALPHA;
ctx.setTransform(state.dpr, 0, 0, state.dpr, PAD * state.dpr, PAD * state.dpr);
ctx.beginPath();
let moving = state.active || state.strength > 0;
for (const dot of state.dots) {
if (radius > 1) {
const dx = dot.x - state.mx;
const dy = dot.y - state.my;
const dist = Math.hypot(dx, dy);
if (dist < radius && dist > 0) {
const k = 1 - dist / radius;
const force = k * k * k * REPEL_FORCE * state.strength;
dot.vx += (dx / dist) * force;
dot.vy += (dy / dist) * force;
}
}
dot.vx = (dot.vx + (dot.ox - dot.x) * SPRING_K) * DAMP;
dot.vy = (dot.vy + (dot.oy - dot.y) * SPRING_K) * DAMP;
const speed = Math.hypot(dot.vx, dot.vy);
if (speed > MAX_SPEED) {
dot.vx = (dot.vx / speed) * MAX_SPEED;
dot.vy = (dot.vy / speed) * MAX_SPEED;
}
dot.x += dot.vx;
dot.y += dot.vy;
if (speed > 0.01) moving = true;
if (Math.abs(dot.x - dot.ox) + Math.abs(dot.y - dot.oy) < 0.02 && speed < 0.01) {
dot.x = dot.ox;
dot.y = dot.oy;
dot.vx = 0;
dot.vy = 0;
}
ctx.moveTo(dot.x + DOT_R, dot.y);
ctx.arc(dot.x, dot.y, DOT_R, 0, Math.PI * 2);
}
ctx.fill();
state.raf = moving ? requestAnimationFrame(draw) : 0;
};

const restart = () => {
cancelAnimationFrame(state.raf);
state.raf = requestAnimationFrame(draw);
};

const settle = () => {
cancelAnimationFrame(state.raf);
state.raf = 0;
state.strength = 0;
state.moving = false;
state.active = false;
for (const dot of state.dots) {
dot.x = dot.ox;
dot.y = dot.oy;
dot.vx = 0;
dot.vy = 0;
}
draw();
};

let idle: number | undefined;
const onMove = (event: PointerEvent) => {
if (prefersReducedMotion()) return;
const rect = layer.getBoundingClientRect();
state.mx = event.clientX - rect.left;
state.my = event.clientY - rect.top;
state.moving = true;
state.strength = 1;
state.active = true;
if (!state.raf) restart();
window.clearTimeout(idle);
idle = window.setTimeout(() => (state.moving = false), 80);
};
const onLeave = () => {
state.active = false;
state.moving = false;
if (!state.raf) restart();
};
wrap.addEventListener("pointermove", onMove);
wrap.addEventListener("pointerleave", onLeave);
// A running loop picks up changes on its next frame; a second draw() would start a second loop.
const redraw = () => {
if (!state.raf) draw();
};
const stopMotion = onMotionPreferenceChange(() =>
prefersReducedMotion() ? settle() : redraw(),
);
const resize = new ResizeObserver(() => {
build();
redraw();
});
resize.observe(layer);
// The ink follows the theme and contrast setting, so a change redraws the dots.
const theme = new MutationObserver(redraw);
theme.observe(document.documentElement, { attributeFilter: ["class", "data-contrast"] });

build();
draw();
layer.style.visibility = "hidden";

return () => {
wrap.removeEventListener("pointermove", onMove);
wrap.removeEventListener("pointerleave", onLeave);
stopMotion();
window.clearTimeout(idle);
resize.disconnect();
theme.disconnect();
cancelAnimationFrame(state.raf);
layer.style.visibility = "";
};
}, []);

return (
<div ref={wrapRef} className={className} aria-hidden="true">
<div
ref={layerRef}
className="mk-dot-mark-layer"
style={{ maskImage: `${LOGO}, ${DOT_TILE}` }}
/>
<canvas ref={canvasRef} className="mk-dot-mark-canvas" />
</div>
);
}
Loading
Loading