diff --git a/apps/web/modules/nova-web/chat-appearance.test.ts b/apps/web/modules/nova-web/chat-appearance.test.ts index 623d2245..adf0de2e 100644 --- a/apps/web/modules/nova-web/chat-appearance.test.ts +++ b/apps/web/modules/nova-web/chat-appearance.test.ts @@ -3,6 +3,15 @@ import { describe, expect, test } from "bun:test" import { contrastingText, DEFAULT_CHAT_APPEARANCE, parseChatAppearance } from "./chat-appearance" describe("chat appearance", () => { + test("restores and bounds background adjustments", () => { + const appearance = parseChatAppearance(JSON.stringify({ effect: "image", brightness: 250, blur: -1, imageOpacity: 55, bottomFade: 30, overlayOpacity: 20, inputOpacity: 70 })) + expect(appearance).toMatchObject({ effect: "image", brightness: 200, blur: 0, imageOpacity: 55, bottomFade: 30, overlayOpacity: 20, inputOpacity: 70 }) + expect(parseChatAppearance(JSON.stringify({ effect: "unknown", brightness: "100" }))).toEqual(DEFAULT_CHAT_APPEARANCE) + }) + test("restores a selected bundled background and ignores unknown images", () => { + expect(parseChatAppearance(JSON.stringify({ image: "clouds" })).image).toBe("clouds") + expect(parseChatAppearance(JSON.stringify({ image: "https://example.com/image" })).image).toBeUndefined() + }) test("uses defaults for missing or corrupt preferences", () => { for (const raw of [null, "bad json", "null", "42"]) { expect(parseChatAppearance(raw)).toEqual(DEFAULT_CHAT_APPEARANCE) diff --git a/apps/web/modules/nova-web/chat-appearance.ts b/apps/web/modules/nova-web/chat-appearance.ts index 4e16313b..0f9f2210 100644 --- a/apps/web/modules/nova-web/chat-appearance.ts +++ b/apps/web/modules/nova-web/chat-appearance.ts @@ -2,6 +2,24 @@ export type ChatAppearance = { background: string ascii: string text: string | null + image?: "night" | "clouds" + effect?: "ascii" | "image" + brightness?: number + blur?: number + imageOpacity?: number + bottomFade?: number + overlayOpacity?: number + inputOpacity?: number +} + +export const BACKGROUND_DEFAULTS = { + effect: "ascii" as const, + brightness: 100, + blur: 0, + imageOpacity: 100, + bottomFade: 40, + overlayOpacity: 0, + inputOpacity: 85, } export const DEFAULT_CHAT_APPEARANCE: ChatAppearance = { @@ -27,10 +45,18 @@ export function parseChatAppearance(raw: string | null): ChatAppearance { const record = value as Record const color = (key: string, fallback: string) => typeof record[key] === "string" && HEX_COLOR.test(record[key]) ? record[key] : fallback + const adjustments: Partial = {} + for (const [key, max] of [["brightness", 200], ["blur", 20], ["imageOpacity", 100], ["bottomFade", 100], ["overlayOpacity", 100], ["inputOpacity", 100]] as const) { + const number = record[key] + if (typeof number === "number" && Number.isFinite(number)) adjustments[key] = Math.max(0, Math.min(max, number)) + } return { background: color("background", DEFAULT_CHAT_APPEARANCE.background), ascii: color("ascii", DEFAULT_CHAT_APPEARANCE.ascii), text: typeof record.text === "string" && HEX_COLOR.test(record.text) ? record.text : null, + ...(record.image === "clouds" ? { image: "clouds" as const } : {}), + ...(record.effect === "image" ? { effect: "image" as const } : {}), + ...adjustments, } } catch { return DEFAULT_CHAT_APPEARANCE diff --git a/apps/web/modules/nova-web/components/ascii-background.tsx b/apps/web/modules/nova-web/components/ascii-background.tsx index cfcea368..1cf81268 100644 --- a/apps/web/modules/nova-web/components/ascii-background.tsx +++ b/apps/web/modules/nova-web/components/ascii-background.tsx @@ -1,97 +1,79 @@ "use client" -import { memo, useEffect, useRef, useState, useSyncExternalStore } from "react" -import { motion, useReducedMotion } from "framer-motion" -import { Pause, Play } from "lucide-react" +import { memo, useEffect, useRef } from "react" -const GLYPHS = " .,:;~+=*#" -const FRAME_MS = 1000 / 30 +import { BACKGROUND_DEFAULTS, type ChatAppearance } from "../chat-appearance" -const subscribeToHydration = () => () => {} -const clientSnapshot = () => true -const serverSnapshot = () => false +const GLYPHS = " .,:;+=xX#%@" -export const AsciiBackground = memo(function AsciiBackground({ color = "#a3aaa7" }: { color?: string }) { +/** Samples a bundled image into a bounded grid; redraws only when the image or viewport changes. */ +export const AsciiBackground = memo(function AsciiBackground({ + color = "#a3aaa7", + image = "/nova/ascii-night.webp", + appearance, +}: { color?: string; image?: string; appearance?: ChatAppearance }) { const canvasRef = useRef(null) - const elapsedRef = useRef(0) - const reducedMotion = useReducedMotion() - const hydrated = useSyncExternalStore(subscribeToHydration, clientSnapshot, serverSnapshot) - const [paused, setPaused] = useState(false) useEffect(() => { const canvas = canvasRef.current - if (!canvas) return - const context = canvas.getContext("2d") - if (!context) return - + const context = canvas?.getContext("2d") + if (!canvas || !context) return + const sample = document.createElement("canvas") + const sampler = sample.getContext("2d", { willReadFrequently: true }) + if (!sampler) return + const source = new Image() let width = 0 let height = 0 - let frame = 0 - let previous = 0 - let lastPaint = 0 - let visible = true - const animate = reducedMotion === false && !paused + let disposed = false function draw() { - if (!context) return + if (!width || !height || !source.naturalWidth || !source.naturalHeight || !context || !sampler || !canvas) return + if (appearance?.effect === "image") { + const scale = Math.max(width / source.naturalWidth, height / source.naturalHeight) + const imageWidth = source.naturalWidth * scale + const imageHeight = source.naturalHeight * scale + context.clearRect(0, 0, width, height) + context.drawImage(source, (width - imageWidth) / 2, (height - imageHeight) / 2, imageWidth, imageHeight) + return + } + const cell = Math.max(7, width / 180, height / 110) + const columns = Math.max(1, Math.ceil(width / cell)) + const rows = Math.max(1, Math.ceil(height / (cell * 1.15))) + sample.width = columns + sample.height = rows + const scale = Math.max(columns / source.naturalWidth, rows / source.naturalHeight) + const imageWidth = source.naturalWidth * scale + const imageHeight = source.naturalHeight * scale + sampler.drawImage(source, (columns - imageWidth) / 2, (rows - imageHeight) / 2, imageWidth, imageHeight) + const pixels = sampler.getImageData(0, 0, columns, rows).data context.clearRect(0, 0, width, height) - context.font = "10px ui-monospace, SFMono-Regular, Menlo, monospace" + context.font = `${cell * 1.05}px ui-monospace, SFMono-Regular, Menlo, monospace` context.textAlign = "center" context.textBaseline = "middle" - context.fillStyle = color - const time = elapsedRef.current * 0.0003 - const cell = Math.max(12, width / 130, height / 80) - const scale = Math.max(Math.min(width, height), 1) - - for (let y = cell / 2; y < height; y += cell) { - for (let x = cell / 2; x < width; x += cell) { - const nx = (x - width / 2) / Math.max(width, 1) - const ny = (y - height / 2) / Math.max(height, 1) - const px = (x - width / 2) / scale - const py = (y - height / 2) / scale - // Domain-warped contour bands drift like a living topographic map. - const flowX = px + Math.sin(py * 3.5 + time * 0.45) * 0.24 - const flowY = py + Math.cos(px * 3 - time * 0.35) * 0.2 - const field = Math.sin(flowX * 4 + time * 0.55) - + Math.cos(flowY * 5 - time * 0.4) - + Math.sin((flowX + flowY) * 3 + time * 0.3) * 0.5 - const contour = Math.pow(0.5 + 0.5 * Math.cos(field * 9 - time), 8) - const mist = (Math.sin(flowX * 8 - flowY * 6 + time) + 1) * 0.5 - const density = Math.min(1, contour * 0.85 + mist * 0.15) - const glyph = GLYPHS[Math.min(GLYPHS.length - 1, Math.floor(density * GLYPHS.length))] + const tint = /^#[0-9a-f]{6}$/i.test(color) ? [1, 3, 5].map((offset) => parseInt(color.slice(offset, offset + 2), 16)) : [163, 170, 167] + for (let y = 0; y < rows; y++) { + for (let x = 0; x < columns; x++) { + const i = (y * columns + x) * 4 + const red = pixels[i], green = pixels[i + 1], blue = pixels[i + 2] + const luminance = (red * 0.2126 + green * 0.7152 + blue * 0.0722) / 255 + const glyph = GLYPHS[Math.min(GLYPHS.length - 1, Math.floor(luminance * GLYPHS.length))] if (glyph === " ") continue - const edge = Math.max(0, 1 - Math.pow(Math.abs(nx) * 2, 4)) - * Math.max(0, 1 - Math.pow(Math.abs(ny) * 2, 4)) - // Leave a quiet pocket behind the composer and project controls. - const pocket = 1 - 0.92 * Math.exp(-(nx * nx * 18 + (ny + 0.06) ** 2 * 26)) - context.globalAlpha = (0.025 + density * 0.13) * edge * pocket - context.fillText(glyph, x, y) + const nx = (x / columns - 0.5) * 2 + const ny = (y / rows - 0.5) * 2 + const centerFade = 1 - 0.45 * Math.exp(-(nx * nx * 4 + (ny + 0.05) ** 2 * 6)) + context.globalAlpha = (0.3 + luminance * 0.45) * centerFade + context.fillStyle = `rgb(${Math.round(red * 0.8 + tint[0] * 0.2)}, ${Math.round(green * 0.8 + tint[1] * 0.2)}, ${Math.round(blue * 0.8 + tint[2] * 0.2)})` + context.fillText(glyph, (x + 0.5) * cell, (y + 0.5) * cell * 1.15) } } context.globalAlpha = 1 } - function tick(now: number) { - frame = 0 - if (document.hidden || !visible || !animate) return - if (previous) elapsedRef.current += Math.min(now - previous, 100) - previous = now - if (now - lastPaint >= FRAME_MS) { - draw() - lastPaint = now - } - frame = requestAnimationFrame(tick) - } - - function syncAnimation() { - cancelAnimationFrame(frame) - frame = 0 - previous = 0 - if (animate && !document.hidden && visible) frame = requestAnimationFrame(tick) - } - + source.onload = () => { if (!disposed) draw() } + source.onerror = () => { if (!disposed) { context.fillStyle = color; context.globalAlpha = 0.1; context.fillText("·", width / 2, height / 2); context.globalAlpha = 1 } } + source.src = image const resize = new ResizeObserver(([entry]) => { - if (!entry || !context) return + if (!entry) return width = entry.contentRect.width height = entry.contentRect.height const dpr = Math.min(window.devicePixelRatio || 1, 2) @@ -100,46 +82,15 @@ export const AsciiBackground = memo(function AsciiBackground({ color = "#a3aaa7" context.setTransform(dpr, 0, 0, dpr, 0, 0) draw() }) - const intersection = new IntersectionObserver(([entry]) => { - visible = entry?.isIntersecting ?? false - syncAnimation() - }) resize.observe(canvas) - intersection.observe(canvas) - document.addEventListener("visibilitychange", syncAnimation) - syncAnimation() - - return () => { - cancelAnimationFrame(frame) - resize.disconnect() - intersection.disconnect() - document.removeEventListener("visibilitychange", syncAnimation) - } - }, [color, paused, reducedMotion]) + return () => { disposed = true; source.onload = null; source.onerror = null; resize.disconnect() } + }, [color, image, appearance?.effect]) - return ( - <> - - {hydrated && reducedMotion === false ? ( - - ) : null} - - ) + const background = appearance?.background ?? "#0e0e10" + const fade = appearance?.bottomFade ?? BACKGROUND_DEFAULTS.bottomFade + return