Something went wrong. Try again.
r/place on the atmosphere, targeting cloudflare's free tier
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110// Page wiring: picks a canvas, connects the view to the feed, and handles// sign-in, color choice and cooldown display.
import type { CanvasConfig } from "../shared/protocol.ts";import { createAuth } from "./auth.ts";import { connectFeed } from "./feed.ts";import { placePixel } from "./place.ts";import { loadProfile } from "./profile.ts";import { CanvasView } from "./view.ts";
const $ = <T extends HTMLElement>(id: string) => document.getElementById(id) as T;
const auth = await createAuth();const canvasUri = await chooseCanvas();let config: CanvasConfig | null = null;let color = 0;let nextPlaceAt = 0;
const view = new CanvasView($("viewport"), async (x, y) => { const session = auth.session; if (!session) return setStatus("Sign in to place pixels."); if (Date.now() < nextPlaceAt) return; setStatus("Placing…"); const result = await placePixel(session, canvasUri, { x, y, color }); if (result.nextPlaceAt) nextPlaceAt = result.nextPlaceAt; if (result.ok) view.applyDiff([{ x, y, color }]); setStatus(result.ok ? "" : result.error);});
connectFeed(canvasUri, { onConfig(next) { config = next; document.title = next.name; view.setConfig(next); renderPalette(); }, onSnapshot: (pixels) => view.setPixels(pixels), onDiff: (pixels) => view.applyDiff(pixels), onConnection: (connected) => $("connection").toggleAttribute("hidden", connected),});
renderAccount();setInterval(renderCooldown, 250);
// ?canvas=<at-uri>, else the deployment's first allowlisted canvas.async function chooseCanvas(): Promise<string> { const fromQuery = new URLSearchParams(location.search).get("canvas"); if (fromQuery) return fromQuery; const canvases = (await (await fetch("/api/canvases")).json()) as string[]; if (!canvases[0]) throw new Error("this deployment has no canvases"); return canvases[0];}
function renderAccount(): void { const session = auth.session; $("signed-in").hidden = !session; $("sign-in").hidden = !!session; if (session) void renderProfile(session.did); $<HTMLFormElement>("sign-in").onsubmit = (e) => { e.preventDefault(); void auth.signIn($<HTMLInputElement>("handle").value.trim()); }; $("sign-out").onclick = async () => { await auth.signOut(); renderAccount(); };}
// Shows the DID until the profile loads, and keeps it if loading fails.async function renderProfile(did: string): Promise<void> { $("signed-in").dataset.did = did; $("display-name").textContent = ""; $("handle-label").textContent = did; $("avatar").hidden = true; const profile = await loadProfile(did).catch(() => null); if (!profile || $("signed-in").dataset.did !== did) return;
$("display-name").textContent = profile.displayName ?? ""; $("handle-label").textContent = profile.handle ? `@${profile.handle}` : did; const avatar = $<HTMLImageElement>("avatar"); if (profile.avatarUrl) avatar.src = profile.avatarUrl; avatar.hidden = !profile.avatarUrl;}
function renderPalette(): void { const palette = $("palette"); palette.replaceChildren( ...(config?.palette ?? []).map((hex, i) => { const swatch = document.createElement("button"); swatch.style.background = hex; swatch.ariaLabel = hex; swatch.ariaPressed = String(i === color); swatch.onclick = () => { color = i; renderPalette(); }; return swatch; }), );}
function renderCooldown(): void { const remaining = Math.ceil((nextPlaceAt - Date.now()) / 1000); $("cooldown").textContent = remaining > 0 ? `Next pixel in ${remaining}s` : "";}
function setStatus(text: string): void { $("status").textContent = text;}