Skip to content

A lightweight, macOS-style dock for Windows 11, with glass visuals, smooth magnification, and Genie effect.

feat(demo): synchronize Genie animation with dock interactions

CristianOlivera19ba8159aeca8
diff --git a/muelle/src/scripts/dock.ts b/muelle/src/scripts/dock.ts
index 098b7a0..97cf98c 100644
--- a/muelle/src/scripts/dock.ts
+++ b/muelle/src/scripts/dock.ts
@@ -251,13 +251,14 @@ export function initDock(root: HTMLElement, reduceMotion: boolean) {
});
// While a window is flying to the dock the demo stands still: the sweep magnifies
- // icons and would drag the slot away from where the window is heading.
+ // icons and would drag the slot away from where the window is heading. Once it has
+ // landed the dock is live again, hover included.
window.addEventListener("muelle:genie", (event) => {
- const { flying, minimized } = (event as CustomEvent<{
+ const { flying } = (event as CustomEvent<{
flying: boolean;
minimized: boolean;
}>).detail;
- geniePaused = flying || minimized;
+ geniePaused = flying;
auto = !geniePaused;
nextClick = 0;
if (geniePaused) {
diff --git a/muelle/src/scripts/genie.ts b/muelle/src/scripts/genie.ts
index 37148ea..28abcbe 100644
--- a/muelle/src/scripts/genie.ts
+++ b/muelle/src/scripts/genie.ts
@@ -1,432 +1,385 @@
/**
- * Genie minimize effect: the window is drawn once into an offscreen canvas, sliced into
- * two-pixel rows, and each row is flown towards the dock along a smooth curve.
+ * Genie minimize, the same effect the Muelle app draws (`crates/muelle-core/src/genie.rs`):
+ * the sides of the window bend into a funnel that ends at the dock slot, and the image
+ * slides down through it, vanishing as it enters the slot.
+ *
+ * The window is copied once from the DOM into a canvas (same boxes, fonts, colours and
+ * icons), cut into thin strips, and each strip is stretched between the funnel's sides.
+ * It plays on a timer, like the app: when the dock comes fully into view while scrolling,
+ * from the yellow traffic light, or from the dock slot to restore.
*/
import { gsap } from "@/lib/motion";
-const SLICE = 1;
+const MINIMIZE_SECONDS = 0.6;
+const RESTORE_SECONDS = 0.5;
+/** Strip height in CSS pixels: thin enough that the curved sides look smooth. */
+const STRIP = 1;
const MAX_DPR = 2;
-const FLIGHT_SECONDS = 0.9;
-const SCROLL_SCRUB_SECONDS = 0.6;
const THUMB_SIZE = 52;
/** Resting width of a dock item, needed to measure the slot without the magnification. */
const DOCK_REST = 52;
-/** Rows near the bottom of the window start moving first. */
-const STAGGER = 0.42;
-const TRAVEL = 0.58;
+
+/** Same curve parameters as the app. */
+const BEND_END = 0.5;
+const SLIDE_START = 0.15;
const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
-/** Where the window must sit in the viewport for the flight to start. */
-const START_RATIO = 0.55;
-const smoothstep = (value: number) => value * value * (3 - 2 * value);
+const smoothstep = (value: number) => {
+ const x = clamp01(value);
+ return x * x * (3 - 2 * x);
+};
+
+interface Edges {
+ left: number;
+ top: number;
+ right: number;
+ bottom: number;
+}
interface Scene {
- x: number;
- y: number;
- width: number;
- height: number;
- targetX: number;
- targetY: number;
- targetWidth: number;
+ window: Edges;
+ slot: Edges;
source: HTMLCanvasElement;
- slice: number;
}
export function initGenie(root: HTMLElement, reduceMotion: boolean) {
const canvas = root.querySelector<HTMLCanvasElement>("[data-genie]");
const note = root.querySelector<HTMLElement>("[data-note]");
- const thumb = document.querySelector<HTMLCanvasElement>("[data-dock-thumb] canvas");
const slot = document.querySelector<HTMLElement>("[data-dock-thumb]");
+ const thumb = slot?.querySelector<HTMLCanvasElement>("canvas");
const restore = slot?.querySelector<HTMLButtonElement>("[data-dock-restore]");
+ const dock = root.querySelector<HTMLElement>(".dock");
const context = canvas?.getContext("2d");
- if (!canvas || !note || !thumb || !slot || !context) return;
+ if (!canvas || !note || !slot || !thumb || !dock || !context) return;
let dpr = Math.min(window.devicePixelRatio || 1, MAX_DPR);
const state = { progress: 0 };
let scene: Scene | null = null;
- let scrollProgress = 0;
- let scrollTarget: number | null = null;
- let manualTarget: number | null = null;
- let manualDirection = 0;
- let manualFlight = false;
- /** Last value written to the dock slot, so the layout is only touched when it changes. */
let slotValue = -1;
let flyingBefore = false;
let minimizedBefore = false;
const paint = (progress: number) => {
- if (!scene) return;
-
- const { x, y, width, height, targetX, targetY, targetWidth, source, slice } = scene;
- context.setTransform(dpr, 0, 0, dpr, 0, 0);
- context.clearRect(0, 0, canvas.width / dpr, canvas.height / dpr);
- // Both hand over on the same threshold: for a few pixels of scroll the DOM window and
- // its canvas copy were painted at once, which read as a ghost of the text.
- const hidden = progress > 0.02;
- const minimized = progress >= 0.995;
+ const hidden = progress > 0;
+ const minimized = progress >= 1;
const flying = hidden && !minimized;
canvas.classList.toggle("hidden", !flying);
note.style.visibility = hidden ? "hidden" : "";
- // The dock's demo pauses while a window is in the air: a sweeping, magnified row
- // would move the slot away from the point the flight is aiming at.
if (flying !== flyingBefore || minimized !== minimizedBefore) {
flyingBefore = flying;
minimizedBefore = minimized;
window.dispatchEvent(new CustomEvent("muelle:genie", { detail: { flying, minimized } }));
}
- // The dock opens its slot as the window arrives, so the thumbnail is not already
- // sitting there when nothing is minimized.
- const open = clamp01((progress - 0.72) / 0.28);
+ // The slot opens as the window reaches it, so the dock shows no thumbnail while
+ // nothing is minimized.
+ const open = clamp01((progress - 0.6) / 0.4);
if (open !== slotValue) {
slotValue = open;
slot.style.setProperty("--slot", String(open));
slot.toggleAttribute("inert", open < 0.5);
- // Only the ends of the travel reflow the row; the dock asks for nothing per frame.
if (open === 0 || open === 1) window.dispatchEvent(new CustomEvent("muelle:slot"));
}
- if (!flying) return;
-
- const rows = Math.round(height / SLICE);
- const restCenter = x + width / 2;
-
- const rowAt = (position: number) => {
- const local = clamp01((progress - (1 - position) * STAGGER) / TRAVEL);
- const eased = smoothstep(local);
- const start = y + position * height;
-
- return {
- top: start + (targetY - start) * eased,
- width: width + (targetWidth - width) * eased,
- center: restCenter + (targetX - restCenter) * eased,
- };
- };
-
- for (let index = 0; index < rows; index += 1) {
- const from = rowAt(index / rows);
- const to = rowAt((index + 1) / rows);
- context.drawImage(
- source,
- 0,
- index * slice,
- source.width,
- slice,
- from.center - from.width / 2,
- from.top,
- from.width,
- Math.max(1, to.top - from.top + 0.6),
- );
- }
+ context.setTransform(1, 0, 0, 1, 0, 0);
+ context.clearRect(0, 0, canvas.width, canvas.height);
+ if (flying && scene) drawFrame(context, scene, progress, dpr);
};
- let frame = 0;
-
- const build = () => {
+ const build = async () => {
dpr = Math.min(window.devicePixelRatio || 1, MAX_DPR);
const rootRect = root.getBoundingClientRect();
const noteRect = note.getBoundingClientRect();
-
- // Aim at the slot at rest: the dock's autoplay magnifies its icons, which moves the
- // whole row, so measuring now would fly the window to a place the slot has left.
- // The slot is opened for the measurement too, because that is where it will land.
- const dockItems = [...root.querySelectorAll<HTMLElement>(".dock-item")];
- const savedSize = dockItems.map((item) => [item.style.width, item.style.height]);
- dockItems.forEach((item) => {
- item.style.width = `${DOCK_REST}px`;
- item.style.height = `${DOCK_REST}px`;
- });
-
- const previousSlot = slot.style.getPropertyValue("--slot");
- slot.style.setProperty("--slot", "1");
- const thumbRect = slot.getBoundingClientRect();
- if (previousSlot) slot.style.setProperty("--slot", previousSlot);
- else slot.style.removeProperty("--slot");
-
- dockItems.forEach((item, index) => {
- const [width, height] = savedSize[index];
- item.style.width = width;
- item.style.height = height;
- });
+ const slotRect = measureSlot(root, slot);
canvas.width = Math.round(rootRect.width * dpr);
canvas.height = Math.round(rootRect.height * dpr);
- const source = renderWindow(noteRect.width, noteRect.height, dpr);
+ // The copy reads the window's layout synchronously, so it is shown for that
+ // moment only (no paint happens in between) in case it is minimized.
+ const visibility = note.style.visibility;
+ note.style.visibility = "";
+ const copying = copyElement(note, dpr);
+ note.style.visibility = visibility;
+ const source = await copying;
scene = {
- x: noteRect.left - rootRect.left,
- y: noteRect.top - rootRect.top,
- width: noteRect.width,
- height: noteRect.height,
- targetX: thumbRect.left - rootRect.left + thumbRect.width / 2,
- targetY: thumbRect.top - rootRect.top + thumbRect.height / 2,
- targetWidth: thumbRect.width,
+ window: {
+ left: noteRect.left - rootRect.left,
+ top: noteRect.top - rootRect.top,
+ right: noteRect.right - rootRect.left,
+ bottom: noteRect.bottom - rootRect.top,
+ },
+ slot: {
+ left: slotRect.left - rootRect.left,
+ top: slotRect.top - rootRect.top,
+ right: slotRect.right - rootRect.left,
+ bottom: slotRect.bottom - rootRect.top,
+ },
source,
- slice: SLICE * dpr,
};
-
renderThumbnail(thumb, source, dpr);
paint(state.progress);
};
build();
- // The dock is wider than a phone and scrolls sideways, so the minimized slot sits off
- // screen: the flight would leave the mockup clipped at its right edge. Below 900 px the
+ // Below 900 px the dock scrolls sideways and its slot sits off screen, so the
// window simply stays where it is.
const wideEnough = window.matchMedia("(min-width: 900px)");
if (reduceMotion || !wideEnough.matches) return;
- const readScrollProgress = () => {
- const top = note.getBoundingClientRect().top + window.scrollY;
- const start = Math.max(0, top - window.innerHeight * START_RATIO);
- return clamp01((window.scrollY - start) / window.innerHeight);
- };
-
- const syncScrollProgress = () => {
- const progress = readScrollProgress();
- scrollProgress = progress;
-
- // Let a manual flight finish, then let scroll take over once it catches the target.
- if (manualTarget !== null) {
- if (manualFlight || (progress - manualTarget) * manualDirection > 0) return;
- manualTarget = null;
- }
-
- if (progress === state.progress) {
- scrollTarget = progress;
- gsap.killTweensOf(state);
- return;
- }
-
- if (progress === scrollTarget) return;
- scrollTarget = progress;
+ const play = (target: 0 | 1) => {
+ if (state.progress === target) return;
gsap.killTweensOf(state);
gsap.to(state, {
- progress,
- duration: SCROLL_SCRUB_SECONDS,
+ progress: target,
+ duration: (target ? MINIMIZE_SECONDS : RESTORE_SECONDS) * Math.abs(target - state.progress),
ease: "none",
onUpdate: () => paint(state.progress),
});
};
- const followScroll = () => {
+ root.querySelector<HTMLButtonElement>("[data-minimize]")?.addEventListener("click", () => play(1));
+ restore?.addEventListener("click", () => play(0));
+
+ // Scrolling plays the effect once per crossing, never scrubs it: the window minimizes
+ // when the whole dock is on screen and comes back when the dock leaves at the bottom.
+ // In between, the buttons are free to do what the user asks.
+ let dockInView: boolean | null = null;
+ let frame = 0;
+ const onScroll = () => {
frame = 0;
- syncScrollProgress();
+ const rect = dock.getBoundingClientRect();
+ const inView = rect.bottom <= window.innerHeight && rect.top >= 0;
+ const below = rect.top > window.innerHeight;
+ if (dockInView === null) {
+ // First reading: jump straight to the matching state, without animating.
+ dockInView = inView;
+ if (inView) {
+ state.progress = 1;
+ paint(1);
+ }
+ return;
+ }
+ if (inView && !dockInView) play(1);
+ if (below && dockInView) play(0);
+ if (inView || below) dockInView = inView;
};
-
window.addEventListener(
"scroll",
() => {
- if (!frame) frame = requestAnimationFrame(followScroll);
+ if (!frame) frame = requestAnimationFrame(onScroll);
},
{ passive: true },
);
- const flyTo = (progress: number) => {
- scrollProgress = readScrollProgress();
- manualTarget = progress;
- manualDirection = Math.sign(scrollProgress - progress);
- scrollTarget = null;
- gsap.killTweensOf(state);
- manualFlight = true;
- gsap.to(state, {
- progress,
- duration: FLIGHT_SECONDS,
- ease: "power2.inOut",
- onUpdate: () => paint(state.progress),
- onComplete: () => {
- manualFlight = false;
- syncScrollProgress();
- },
- });
- };
-
- root.querySelector<HTMLButtonElement>("[data-minimize]")?.addEventListener("click", () => {
- flyTo(state.progress > 0.5 ? 0 : 1);
- });
-
- restore?.addEventListener("click", () => flyTo(0));
-
const refresh = () => {
build();
- syncScrollProgress();
+ onScroll();
};
window.addEventListener("resize", refresh, { passive: true });
document.fonts.ready.then(refresh);
window.addEventListener("load", refresh);
- syncScrollProgress();
}
-/** Draws the window at rest, mirroring the DOM mock so the flight can start from it. */
-function renderWindow(width: number, height: number, dpr: number) {
- const source = document.createElement("canvas");
- source.width = Math.round(width * dpr);
- source.height = Math.round(height * dpr);
+/** Where the slot will be once open, with every dock icon at rest. */
+function measureSlot(root: HTMLElement, slot: HTMLElement): DOMRect {
+ const items = [...root.querySelectorAll<HTMLElement>(".dock-item")];
+ const saved = items.map((item) => [item.style.width, item.style.height]);
+ items.forEach((item) => {
+ if (item === slot) return;
+ item.style.width = `${DOCK_REST}px`;
+ item.style.height = `${DOCK_REST}px`;
+ });
+ const previous = slot.style.getPropertyValue("--slot");
+ slot.style.setProperty("--slot", "1");
+ const rect = slot.getBoundingClientRect();
+ slot.style.setProperty("--slot", previous || "0");
+ items.forEach((item, index) => {
+ item.style.width = saved[index][0];
+ item.style.height = saved[index][1];
+ });
+ return rect;
+}
- const context = source.getContext("2d");
- if (!context) return source;
+/** One frame of the app's genie: equal strips placed by `bend` and `slide`. */
+function drawFrame(context: CanvasRenderingContext2D, scene: Scene, t: number, dpr: number) {
+ const { window: win, slot, source } = scene;
+ const height = win.bottom - win.top;
+ const count = Math.max(1, Math.round(height / STRIP));
+ const bend = smoothstep(t / BEND_END);
+ const slideT = clamp01((t - SLIDE_START) / (1 - SLIDE_START));
+ const shift = slideT * slideT * Math.max(0, slot.bottom - win.top);
+ const span = slot.top - win.top;
+ const sliceHeight = source.height / count;
+
+ const sides = (y: number) => {
+ const along = span > 0 ? clamp01((y - win.top) / span) : 1;
+ const curve = ((1 - Math.cos(Math.PI * along)) / 2) * bend;
+ return [win.left + (slot.left - win.left) * curve, win.right + (slot.right - win.right) * curve];
+ };
+ context.setTransform(dpr, 0, 0, dpr, 0, 0);
+ for (let index = 0; index < count; index += 1) {
+ const top = win.top + (index / count) * height + shift;
+ if (top >= slot.bottom) break;
+ const bottom = Math.min(win.top + ((index + 1) / count) * height + shift, slot.bottom);
+ const [left, right] = sides((top + bottom) / 2);
+ // Each strip overlaps the next slightly, so no seams show between them.
+ context.drawImage(
+ source,
+ 0,
+ index * sliceHeight,
+ source.width,
+ sliceHeight,
+ left,
+ top,
+ right - left,
+ bottom - top + 0.6,
+ );
+ }
+}
+
+/**
+ * Copies an element into a canvas from its own layout: backgrounds, borders, text and
+ * inline SVG icons, with the computed fonts and colours. Enough for the Notes mock, and
+ * it can never drift from the markup.
+ */
+async function copyElement(element: HTMLElement, dpr: number): Promise<HTMLCanvasElement> {
+ const box = element.getBoundingClientRect();
+ const out = document.createElement("canvas");
+ out.width = Math.round(box.width * dpr);
+ out.height = Math.round(box.height * dpr);
+ const context = out.getContext("2d");
+ if (!context) return out;
context.scale(dpr, dpr);
- context.textBaseline = "middle";
+ const local = (rect: DOMRect) => ({
+ x: rect.left - box.left,
+ y: rect.top - box.top,
+ width: rect.width,
+ height: rect.height,
+ });
+ const radius = (style: CSSStyleDeclaration, rect: DOMRect) =>
+ Math.min(parseFloat(style.borderTopLeftRadius) || 0, rect.width / 2, rect.height / 2);
+ const opacityOf = (node: Element) => {
+ let value = 1;
+ for (let current: Element | null = node; current && current !== element.parentElement; current = current.parentElement) {
+ value *= parseFloat(getComputedStyle(current).opacity);
+ }
+ return value;
+ };
+
+ const rootStyle = getComputedStyle(element);
context.save();
context.beginPath();
- context.roundRect(0, 0, width, height, 14);
+ context.roundRect(0, 0, box.width, box.height, radius(rootStyle, box));
context.clip();
- context.fillStyle = "rgba(255, 253, 247, 0.97)";
- context.fillRect(0, 0, width, height);
-
- // These offsets mirror the CSS in `NotesWindow.astro`; the two must line up or the
- // handover from the DOM window to this copy shows as a jump.
- const sidebar = width >= 440 ? 158 : 0;
- if (sidebar) drawSidebar(context, sidebar, height);
-
- const left = sidebar + 20;
- const center = (left + width - 20) / 2;
-
- drawTrafficLights(context, 22, 20);
-
- context.textAlign = "center";
- context.fillStyle = "#1d1d1f";
- context.font = '700 13px "Plus Jakarta Sans", system-ui, sans-serif';
- context.fillText("Notes", center, 22);
-
- context.fillStyle = "rgba(0, 0, 0, 0.38)";
- context.font = '500 11px "Plus Jakarta Sans", system-ui, sans-serif';
- context.fillText("Friday, November 14, 2026 at 9:41 AM", center, 44);
-
- context.textAlign = "left";
- context.fillStyle = "#1d1d1f";
- context.font = '800 26px "Plus Jakarta Sans", system-ui, sans-serif';
- context.fillText("Ship the Dock.", left, 81);
-
- const tasks = [
- ["Magnification, smooth curve", true],
- ["Live thumbnails when minimized", true],
- ["Taskbar always comes back", true],
- ["0% CPU when idle", true],
- ["Publish v0.1", false],
- ] as const;
-
- tasks.forEach(([label, done], index) => {
- const y = 117 + index * 29;
- drawCheck(context, left + 8, y, done);
- context.fillStyle = done ? "rgba(0, 0, 0, 0.4)" : "#1d1d1f";
- context.font = '500 14px "Plus Jakarta Sans", system-ui, sans-serif';
- context.fillText(label, left + 26, y);
- });
-
- context.restore();
- return source;
-}
+ const icons: Promise<void>[] = [];
+ const nodes = [element, ...element.querySelectorAll<Element>("*")];
+ for (const node of nodes) {
+ const style = getComputedStyle(node);
+ if (style.display === "none" || style.visibility === "hidden") continue;
+ const alpha = opacityOf(node);
+ if (alpha === 0) continue;
+ const rect = node.getBoundingClientRect();
+ if (rect.width === 0 || rect.height === 0) continue;
+ const at = local(rect);
+ context.globalAlpha = alpha;
+
+ if (node instanceof SVGSVGElement) {
+ icons.push(drawSvg(context, node, at));
+ continue;
+ }
+ if (node instanceof SVGElement) continue;
-function drawSidebar(
- context: CanvasRenderingContext2D,
- width: number,
- height: number,
-): void {
- context.strokeStyle = "rgba(0, 0, 0, 0.08)";
- context.lineWidth = 1;
- context.beginPath();
- context.moveTo(width, 0);
- context.lineTo(width, height);
- context.stroke();
-
- context.fillStyle = "rgba(0, 0, 0, 0.45)";
- context.font = '700 11px "Plus Jakarta Sans", system-ui, sans-serif';
- context.fillText("Folders", 12, 60);
-
- const folders = [
- ["All Notes", 12, false],
- ["Muelle", 5, true],
- ["Ideas", 4, false],
- ["Trash", 1, false],
- ] as const;
-
- folders.forEach(([name, count, active], index) => {
- const y = 90 + index * 29.7;
-
- if (active) {
- context.fillStyle = "#0a84ff";
+ if (style.backgroundColor !== "rgba(0, 0, 0, 0)") {
+ context.fillStyle = style.backgroundColor;
context.beginPath();
- context.roundRect(8, y - 13, width - 16, 26, 6);
+ context.roundRect(at.x, at.y, at.width, at.height, radius(style, rect));
context.fill();
}
+ const borderWidth = parseFloat(style.borderTopWidth);
+ if (borderWidth > 0 && style.borderTopStyle !== "none") {
+ context.strokeStyle = style.borderTopColor;
+ context.lineWidth = borderWidth;
+ context.beginPath();
+ const half = borderWidth / 2;
+ context.roundRect(at.x + half, at.y + half, at.width - borderWidth, at.height - borderWidth, radius(style, rect));
+ context.stroke();
+ }
+ const right = parseFloat(style.borderRightWidth);
+ if (right > 0 && borderWidth === 0 && style.borderRightStyle !== "none") {
+ context.fillStyle = style.borderRightColor;
+ context.fillRect(at.x + at.width - right, at.y, right, at.height);
+ }
- context.fillStyle = active ? "rgba(255, 255, 255, 0.9)" : "#f2b100";
- context.beginPath();
- context.roundRect(16, y - 6, 12, 10, 2);
- context.fill();
-
- context.fillStyle = active ? "#ffffff" : "#1d1d1f";
- context.font = '500 13px "Plus Jakarta Sans", system-ui, sans-serif';
- context.textAlign = "left";
- context.fillText(name, 34, y);
-
- context.fillStyle = active ? "rgba(255, 255, 255, 0.8)" : "rgba(0, 0, 0, 0.4)";
- context.font = '500 11px "Plus Jakarta Sans", system-ui, sans-serif';
- context.textAlign = "right";
- context.fillText(String(count), width - 16, y);
- context.textAlign = "left";
- });
-}
+ for (const child of node.childNodes) {
+ if (child.nodeType !== Node.TEXT_NODE || !child.textContent?.trim()) continue;
+ const range = document.createRange();
+ range.selectNodeContents(child);
+ const textRect = range.getBoundingClientRect();
+ if (textRect.width === 0) continue;
+ context.font = `${style.fontStyle} ${style.fontWeight} ${style.fontSize} ${style.fontFamily}`;
+ context.letterSpacing = style.letterSpacing === "normal" ? "0px" : style.letterSpacing;
+ context.fillStyle = style.color;
+ context.textBaseline = "alphabetic";
+ const metrics = context.measureText(child.textContent);
+ context.fillText(
+ child.textContent.trim(),
+ textRect.left - box.left,
+ textRect.top - box.top + metrics.fontBoundingBoxAscent,
+ );
+ }
+ }
-function drawTrafficLights(context: CanvasRenderingContext2D, x: number, y: number): void {
- ["#ff5f57", "#febc2e", "#28c840"].forEach((color, index) => {
- context.beginPath();
- context.arc(x + index * 20, y, 6, 0, Math.PI * 2);
- context.fillStyle = color;
- context.fill();
- context.strokeStyle = "rgba(0, 0, 0, 0.16)";
- context.lineWidth = 0.6;
- context.stroke();
- });
+ await Promise.all(icons);
+ context.restore();
+ return out;
}
-function drawCheck(context: CanvasRenderingContext2D, x: number, y: number, done: boolean) {
- context.beginPath();
- context.arc(x, y, 8, 0, Math.PI * 2);
-
- if (!done) {
- context.strokeStyle = "rgba(0, 0, 0, 0.25)";
- context.lineWidth = 1.3;
- context.stroke();
- return;
- }
-
- context.fillStyle = "#ffb547";
- context.fill();
- context.strokeStyle = "#ffffff";
- context.lineWidth = 1.8;
- context.beginPath();
- context.moveTo(x - 3.6, y - 0.2);
- context.lineTo(x - 1, y + 2.4);
- context.lineTo(x + 4, y - 3);
- context.stroke();
+function drawSvg(
+ context: CanvasRenderingContext2D,
+ svg: SVGSVGElement,
+ at: { x: number; y: number; width: number; height: number },
+): Promise<void> {
+ const copy = svg.cloneNode(true) as SVGSVGElement;
+ copy.setAttribute("xmlns", "http://www.w3.org/2000/svg");
+ copy.setAttribute("width", String(at.width));
+ copy.setAttribute("height", String(at.height));
+ const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(copy.outerHTML)}`;
+ const alpha = context.globalAlpha;
+ return new Promise((resolve) => {
+ const image = new Image();
+ image.onload = () => {
+ const previous = context.globalAlpha;
+ context.globalAlpha = alpha;
+ context.drawImage(image, at.x, at.y, at.width, at.height);
+ context.globalAlpha = previous;
+ resolve();
+ };
+ image.onerror = () => resolve();
+ image.src = url;
+ });
}
-/** The dock thumbnail is the same drawing, cropped square. */
+/** The dock thumbnail is the same copy, cropped square from the top. */
function renderThumbnail(canvas: HTMLCanvasElement, source: HTMLCanvasElement, dpr: number) {
const size = Math.round(THUMB_SIZE * dpr);
canvas.width = size;
canvas.height = size;
-
const context = canvas.getContext("2d");
if (!context) return;
-
context.clearRect(0, 0, size, size);
context.save();
context.beginPath();
context.roundRect(0, 0, size, size, 12 * dpr);
context.clip();
-
const scale = Math.max(size / source.width, size / source.height);
const width = source.width * scale;
- const height = source.height * scale;
- context.drawImage(source, (size - width) / 2, (size - height) / 2, width, height);
+ context.drawImage(source, (size - width) / 2, 0, width, source.height * scale);
context.restore();
}