diff --git a/packages/app/component-tests/new-session-wordmark.spec.ts b/packages/app/component-tests/new-session-wordmark.spec.ts index 1412d8cfdd3..f12af9ca9cb 100644 --- a/packages/app/component-tests/new-session-wordmark.spec.ts +++ b/packages/app/component-tests/new-session-wordmark.spec.ts @@ -1,96 +1,88 @@ +import type { Page } from "@playwright/test" import { expect, story } from "../../storybook/playwright/story" -for (const theme of ["light", "dark"]) { - for (const direction of ["ltr", "rtl"]) { - story(`shimmers once over the visible wordmark (${theme}, ${direction})`, async ({ mount, page }, testInfo) => { - await page.emulateMedia({ reducedMotion: "no-preference" }) - const component = await mount("app-new-session-wordmark--reveal", { globals: { theme, direction } }) - const logo = component.locator('[data-component="new-session-wordmark"]') - const reveal = logo.locator('[data-slot="wordmark-reveal"]') - const shimmer = logo.locator(".wordmark-shimmer") - const base = logo.locator("svg").first() - await expect(base).toHaveCSS("opacity", "0.16") - await expect(shimmer).toHaveCSS("animation-iteration-count", "1") - await expect(logo.locator("g[mask]")).toHaveCount(0) - await expect(shimmer).toHaveCSS("color", "rgb(255, 255, 255)") - await expect(shimmer).toHaveCSS("mix-blend-mode", "screen") - await expect(base.locator("path").nth(1)).toHaveAttribute("fill", "var(--icon-base)") - await expect(base.locator("path").nth(9)).toHaveCSS( - "fill", - await base.locator("path").nth(1).evaluate((element) => getComputedStyle(element).fill), - ) - await expect(base.locator('path[fill="var(--icon-weak-base)"]')).toHaveCount(8) +// A cell of the "c" in the wordmark (column 40, row 2 of the 78×14 pin grid). +const PIN = { x: 40.5 / 78, y: 2.5 / 14 } - await logo.evaluate((element) => { - element.getAnimations({ subtree: true }).forEach((animation) => { - animation.pause() - animation.currentTime = 0 - }) - }) - await expect(reveal).toHaveCSS("opacity", "1") - await expect(reveal).toHaveCSS("animation-name", "none") - await expect(shimmer).toHaveCSS("mask-position", "100% 0px") +story("draws the wordmark as pins and stops animating once settled", async ({ mount, page }) => { + await page.emulateMedia({ reducedMotion: "no-preference" }) + await mount("app-new-session-wordmark--pin-board") - await logo.evaluate((element) => { - element.getAnimations({ subtree: true }).forEach((animation) => (animation.currentTime = 400)) - }) - await expect(shimmer).toHaveCSS("mask-position", "50% 0px") - await expect(shimmer).toHaveCSS("opacity", theme === "dark" ? "0.0672" : "0.45") - const swept = await logo.screenshot({ path: testInfo.outputPath("wordmark-shimmer.png") }) + await expect.poll(() => pinPixel(page)).toMatchObject({ alpha: 255 }) + await expect.poll(() => requestedFrames(page)).toBe(0) +}) - await logo.evaluate((element) => { - element.getAnimations({ subtree: true }).forEach((animation) => animation.finish()) - }) - await expect(reveal).toHaveCSS("opacity", "1") - await expect(shimmer).toHaveCSS("opacity", "0") - await expect(shimmer).toHaveCSS("mask-position", "0% 0px") - const settled = await logo.screenshot({ path: testInfo.outputPath("wordmark-settled.png") }) +story("knocks pins out from the panel but never from the composer", async ({ mount, page }) => { + await page.emulateMedia({ reducedMotion: "no-preference" }) + await mount("app-new-session-wordmark--pin-board") + await expect.poll(() => requestedFrames(page)).toBe(0) + const before = await pinPixel(page) - const brightness = await page.evaluate( - async (screenshots) => { - const pixels = await Promise.all( - screenshots.map(async (screenshot) => { - const image = new Image() - image.src = `data:image/png;base64,${screenshot}` - await image.decode() - const canvas = new OffscreenCanvas(image.width, image.height) - const context = canvas.getContext("2d") + await page.locator('[data-component="composer"]').click() + await nextFrames(page, 2) + expect(await pinPixel(page)).toEqual(before) - if (!context) throw new Error("Cannot read screenshot pixels") - context.drawImage(image, 0, 0) + const box = (await page.locator('[data-slot="wordmark-box"]').boundingBox())! + await page.mouse.click(box.x + box.width * PIN.x, box.y + box.height * PIN.y) + await expect.poll(() => pinPixel(page)).not.toEqual(before) +}) - return context.getImageData(0, 0, image.width, image.height).data - }), - ) +story("shows a static pin board with reduced motion", async ({ mount, page }) => { + await page.emulateMedia({ reducedMotion: "reduce" }) + await mount("app-new-session-wordmark--pin-board") + await expect.poll(() => pinPixel(page)).toMatchObject({ alpha: 255 }) + await expect.poll(() => requestedFrames(page)).toBe(0) + const before = await pinPixel(page) - return { - min: pixels[0].reduce( - (min, value, index) => (index % 4 === 3 ? min : Math.min(min, value - pixels[1][index])), - 0, - ), - max: pixels[0].reduce( - (max, value, index) => (index % 4 === 3 ? max : Math.max(max, value - pixels[1][index])), - 0, - ), - } - }, - [swept.toString("base64"), settled.toString("base64")], - ) + const box = (await page.locator('[data-slot="wordmark-box"]').boundingBox())! + await page.mouse.click(box.x + box.width * PIN.x, box.y + box.height * PIN.y) + await nextFrames(page, 2) + expect(await pinPixel(page)).toEqual(before) + expect(await requestedFrames(page)).toBe(0) +}) - expect(brightness.min).toBeGreaterThanOrEqual(0) - expect(brightness.max).toBeGreaterThanOrEqual(5) - }) - } - - story(`shows a static wordmark with reduced motion (${theme})`, async ({ mount, page }) => { - await page.emulateMedia({ reducedMotion: "reduce" }) - await page.setViewportSize({ width: 360, height: 640 }) - const component = await mount("app-new-session-wordmark--reveal", { globals: { theme } }) - const logo = component.locator('[data-component="new-session-wordmark"]') - await expect(logo.locator("svg").first()).toHaveCSS("opacity", "0.16") - await expect(logo.locator('[data-slot="wordmark-reveal"]')).toHaveCSS("opacity", "1") - await expect(logo.locator(".wordmark-shimmer")).toHaveCSS("opacity", "0") - expect(await logo.evaluate((element) => element.getAnimations({ subtree: true }).length)).toBe(0) - expect(await logo.evaluate((element) => element.getBoundingClientRect().right <= innerWidth)).toBe(true) - }) +// Reads the back canvas pixel under the PIN cell, wherever the canvas currently sits. +function pinPixel(page: Page) { + return page.evaluate((pin) => { + const box = document.querySelector('[data-slot="wordmark-box"]')!.getBoundingClientRect() + const canvas = document.querySelector('canvas[data-slot="new-session-pins"]')! + const rect = canvas.getBoundingClientRect() + const x = Math.floor((box.left + box.width * pin.x - rect.left) * (canvas.width / rect.width)) + const y = Math.floor((box.top + box.height * pin.y - rect.top) * (canvas.height / rect.height)) + const d = canvas.getContext("2d")!.getImageData(x, y, 1, 1).data + return { red: d[0], green: d[1], blue: d[2], alpha: d[3] } + }, PIN) +} + +// Counts animation frames the page requests while we watch the next 20 frames. +function requestedFrames(page: Page) { + return page.evaluate( + () => + new Promise((resolve) => { + const original = window.requestAnimationFrame + const watch = original.bind(window) + const state = { requested: 0, left: 20 } + window.requestAnimationFrame = (callback) => { + state.requested++ + return watch(callback) + } + const tick = () => { + if (--state.left > 0) return watch(tick) + window.requestAnimationFrame = original + resolve(state.requested) + } + watch(tick) + }), + ) +} + +function nextFrames(page: Page, count: number) { + return page.evaluate( + (count) => + new Promise((resolve) => { + const tick = (left: number) => (left ? requestAnimationFrame(() => tick(left - 1)) : resolve()) + tick(count) + }), + count, + ) } diff --git a/packages/app/src/new-session/pin-board.tsx b/packages/app/src/new-session/pin-board.tsx new file mode 100644 index 00000000000..22949e25315 --- /dev/null +++ b/packages/app/src/new-session/pin-board.tsx @@ -0,0 +1,794 @@ +import { onCleanup, onMount } from "solid-js" +import { PinSound } from "./pin-sound" + +// The wordmark as a 78×14 pin grid: 1 = counter shadow, 2 = "open", 3 = "code". +const GRID = [ + "..................................................................33..........", + "..................................................................33..........", + "22222222..22222222..22222222..222222....33333333..33333333..33333333..33333333", + "22222222..22222222..22222222..222222....33333333..33333333..33333333..33333333", + "22....22..22....22..22....22..22....22..33........33....33..33....33..33....33", + "22....22..22....22..22....22..22....22..33........33....33..33....33..33....33", + "22111122..22111122..22222222..22111122..33111111..33111133..33111133..33333333", + "22111122..22111122..22222222..22111122..33111111..33111133..33111133..33333333", + "22111122..22111122..22111111..22111122..33111111..33111133..33111133..33111111", + "22111122..22111122..22111111..22111122..33111111..33111133..33111133..33111111", + "22222222..22222222..22222222..22111122..33333333..33333333..33333333..33333333", + "22222222..22222222..22222222..22111122..33333333..33333333..33333333..33333333", + "..........22..................................................................", + "..........22..................................................................", +] +const COLS = 78 +const ROWS = 14 + +const HOME = 0 +const AIR = 1 +const REST = 2 +const RETURN = 3 + +const REST_TILT = [0.42, 0.7] as const +const LIGHT = [0.55 / Math.hypot(0.55, 0.83), -0.83 / Math.hypot(0.55, 0.83)] as const +const SHADES = 16 +const LOOSE = 4 +const REVEAL = 0.75 +const INTERACTIVE = + 'button, a, input, textarea, select, label, [contenteditable], [role], [data-component="composer"], [data-component="new-session-tip"]' + +type Pin = { + c: number + r: number + lv: number + hx: number + hy: number + x: number + y: number + vx: number + vy: number + z: number + zv: number + e: number + rot: number + spin: number + state: number + pop: number + popAt: number + bucket: number + onComposer: boolean + level: number + x0: number + y0: number + rot0: number + t0: number + duration: number + lift: number + fd: number +} + +/** + * The new session wordmark as a 3D pin board. Pins tilt toward the cursor and sink under it. + * A click or drag knocks pins out; they pop toward the viewer, pile up on the composer, + * then slot back in. The animation loop only runs while something is moving. + */ +export function NewSessionPinBoard(props: { logo: HTMLElement; sound: () => boolean }) { + let back!: HTMLCanvasElement + let front!: HTMLCanvasElement + + onMount(() => onCleanup(mountPinBoard(back, front, props.logo, props.sound))) + + return ( + <> +