Frontend System Design22 min readPremium

Design Photoshop on the Web – Frontend System Design

53. Design Photoshop on the Web

We’ll walk through the frontend system design for Photoshop on the web: a raster image editor that opens a 200 MB layered file in a browser tab, paints with a pressure-sensitive brush at display refresh rate, and runs a Gaussian blur across eight CPU cores. You’ll see why the engine is C++ compiled to WebAssembly, how threads share one block of memory, why every layer is a grid of 256 × 256 tiles, and where the pixels go when RAM runs out.

This is not the Figma question. Figma is a vector scene graph with multiplayer. Photoshop is pixels: hundreds of megabytes of them per layer, filters that touch every one, and an interviewer who wants you to go deep on “building it in WebAssembly”. The hard parts are memory, threads, and the JS-to-Wasm boundary.

Problem Statement

Design the frontend for a professional raster image editor that runs in the browser, like Photoshop on the web.

Requirements:

  • Open and save layered documents up to 8000 × 6000 px, 8 or 16 bits per channel
  • Pixel layers, layer masks, adjustment layers, groups, and blend modes
  • Brush, eraser, and selection tools with pen pressure
  • Filters such as Gaussian blur and sharpen over the whole layer or a selection
  • Deep undo history
  • Cloud documents with autosave, plus local file open and export

Reuse the company’s existing C++ imaging engine. Describe how it runs in the browser, how you manage memory and threads, and how the UI stays responsive.

Interviewers are checking three things. Do you understand what WebAssembly actually gives you, and what it costs to cross into it? Can you keep a multi-second filter off the thread that handles input? And can you fit a document that is bigger than the memory you have?


Clarifying Questions

# Question Interviewer’s Answer
1 Raster or vector editing? Raster. Vector shapes and text layers are out of scope.
2 Do we rewrite the engine or port it? Port it. Millions of lines of tested C++. A rewrite is not an option.
3 How big is a large document? 8000 × 6000, 20+ layers, sometimes 16 bits per channel.
4 Which browsers and devices? Desktop Chromium first. Firefox and Safari supported, with fallbacks.
5 Live co-editing of the same canvas? No. Cloud documents with versions and comments. One editor at a time.
6 Third-party content inside the app? A stock image picker, an analytics script, and an OAuth sign-in popup.
7 Offline? Survive a crash or a short network drop without losing work.

Question 2 decides the technology: an existing C++ codebase means Emscripten and WebAssembly. Question 3 decides the memory design. Question 6 looks harmless, but it is a trap: the threads you need require cross-origin isolation, and isolation breaks naive third-party embeds and popups. Bring it up before the interviewer does.


Requirements

Functional

  • Open PSD, PNG, JPEG, and WebP from disk or cloud. Save to cloud, export to disk.
  • Layers panel: add, delete, reorder, group, rename, toggle visibility, set opacity and blend mode.
  • Layer masks and non-destructive adjustment layers (levels, curves, hue/saturation).
  • Brush and eraser with size, hardness, flow, and pen pressure. Rectangle, lasso, and magic-wand selections.
  • Filters with live preview and a cancel button.
  • Undo and redo through a history panel of at least 50 steps.
  • Zoom from 1% to 3200% and pan with space+drag or trackpad.

Non-Functional

  • A brush stroke appears under the pen within one or two frames, even while a filter runs.
  • The main thread never blocks for more than 50 ms. Long work is cancellable and shows progress.
  • A 200 MB document opens to a visible first composite in a few seconds on a warm cache.
  • Memory stays within a budget. Large documents degrade to slower, not to a crashed tab.
  • No lost work: autosave locally and recover after a tab crash.
  • Every panel is usable with a keyboard and a screen reader.

The brush requirement and the 50 ms requirement pull in the same direction. Nothing heavy can run on the main thread, which means the engine, the compositor, and the pixel memory all live in workers.

Napkin Math

Metric Estimate Calculation So what?
One layer, 8-bit RGBA 192 MB 8000 × 6000 = 48 M px × 4 bytes Never copy a whole layer for undo.
One layer, 16-bit RGBA 384 MB 48 M px × 8 bytes Ten 16-bit layers is 3.84 GB.
Wasm32 address space 4 GiB 65,536 pages × 64 KiB A big document does not fit. Page tiles to disk.
One tile 256 KiB 256 × 256 × 4 bytes The unit of undo, swap, upload, and GPU upload.
Tiles per layer 768 ceil(8000 / 256) × ceil(6000 / 256) = 32 × 24 Enough units to spread across 8 threads.
Brush stroke undo cost ~5 MiB 20 touched tiles × 256 KiB 50 steps fit in a few hundred MB, not 9.6 GB.
Blur, radius 8, separable ~6.5 G multiply-adds 2 passes × 17 taps × 48 M px × 4 channels Needs SIMD and threads, and still runs in a worker.
Frame budget at 120 Hz 8.3 ms 1000 / 120 Brush dabs and compositing must fit inside it.

The 768 tiles per layer is slightly more than the layer itself (201 MB of tile slots for 192 MB of pixels), because edge tiles are partly empty. That overhead is the price of fixed-size units, and it disappears once you skip fully transparent tiles.


Architecture

Photoshop on the web architecture: main thread UI, engine worker with WebAssembly tile store, thread pool, GPU compositor, OPFS storage, cloud sync, and servers

The client has five parts. The key decision is that the main thread owns no pixels.

Layer Owns Runs on Talks to
UI Shell Panels, menus, input, file pickers Main thread Engine worker via postMessage
Engine Document, tiles, history, brush, filters Dedicated worker, C++ in Wasm Thread pool, compositor, OPFS
Thread pool Nothing; runs tile jobs N workers sharing Wasm memory Engine, through shared memory
Compositor GPU textures for visible tiles WebGPU or WebGL2 on an OffscreenCanvas Engine (dirty tiles in)
Storage and sync Swap file, autosave, cloud upload OPFS + a sync worker Cloud Docs API, blob store

Adobe shipped Photoshop on the web this way. Its existing C++ code is compiled to WebAssembly with Emscripten, it relies on WebAssembly SIMD and threads, and it uses the Origin Private File System for fast local storage. The UI is built from web components (Adobe’s Spectrum Web Components, based on Lit). You don’t need to know their internals. The public facts are enough to anchor the design.

“The UI thread only renders panels and forwards input. The C++ engine runs in a worker as WebAssembly, owns every pixel, and draws through an OffscreenCanvas, so a ten-second filter can never freeze a slider.”


Data Model

Photoshop on the web data model: document, layer, adjustment, tile grid, tile reference, and history state

Layers are small records. Pixels live in tile grids. The engine owns both in C++, and the UI gets a read-only summary. The TypeScript below is the shape of that state, not where it physically lives.


type LayerId = number;
type BitDepth = 8 | 16 | 32;
type BlendMode = "normal" | "multiply" | "screen" | "overlay" | "soft-light" | "difference" | "color" | "luminosity";

interface DocumentModel {
  id: string;
  width: number;
  height: number;
  depth: BitDepth;            // bits per channel
  colorProfile: string;       // ICC profile name, e.g. "Display P3"
  rootLayers: LayerId[];      // top of the stack first
  layers: Map<LayerId, Layer>;
  version: number;            // last cloud version this copy is based on
}

interface LayerBase {
  id: LayerId;
  name: string;
  parentId: LayerId | null;
  blendMode: BlendMode;
  opacity: number;            // 0..1
  visible: boolean;
  mask?: TileGrid;            // 1 channel, 8 bit
}

type Layer =
  | (LayerBase & { kind: "pixel"; pixels: TileGrid })
  | (LayerBase & { kind: "adjustment"; adjustment: Adjustment })
  | (LayerBase & { kind: "group"; children: LayerId[] });

type Adjustment =
  | { type: "levels"; black: number; white: number; gamma: number }
  | { type: "curves"; points: [x: number, y: number][] }
  | { type: "hueSaturation"; hue: number; saturation: number; lightness: number };

interface TileGrid {
  tileSize: 256;
  cols: number;
  rows: number;
  channels: 1 | 4;
  slots: (TileRef | null)[];  // row-major; null = fully transparent, no memory
}

interface TileRef {
  id: number;
  refCount: number;           // grids (document + history states) that point here
  location: { kind: "heap"; ptr: number } | { kind: "swap"; slot: number };
  hash?: string;              // content hash once uploaded
}

Three choices are worth defending in the interview.

Tiles, not one big buffer. A 192 MB contiguous allocation is hard to find in a fragmented heap, impossible to swap partially, and expensive to snapshot. Fixed 256 × 256 tiles make every operation incremental: undo, swap, upload, and GPU upload all work on the tiles that changed.

Sparse slots. A new empty layer costs zero tile memory. A mask that is white everywhere except a small vignette stores only the tiles with variation, with a per-grid default value for the rest.

Adjustment layers hold parameters, not pixels. A curves layer is a lookup table applied at composite time to everything below it in its group. That is what makes it non-destructive: change the curve and nothing below is rewritten.


Building the Engine in WebAssembly

Why Wasm, and why port

Interviewers often ask “why not write it in TypeScript?” There are three honest answers.

  • The code already exists. Decades of file-format parsers, color management, and filters are tested in C++. Emscripten compiles them for the web with a POSIX-like runtime, so most of the code ports unchanged.
  • Predictable speed. Wasm is compiled ahead of execution to machine code with no garbage collector in the hot path. Pixel loops run near native speed, and they don’t pause for GC in the middle of a stroke.
  • SIMD and threads. Wasm has 128-bit SIMD and C++ threads over shared memory, which is what image processing needs. JavaScript has no SIMD, and its workers can only share raw buffers, not the engine’s data structures.

The trade-offs are real too: large binaries, a slow first compile, painful debugging across the boundary, and memory capped at 4 GiB in 32-bit Wasm. A strong answer names them.

Compiling with Emscripten


emcc engine/*.cpp -O3 -flto 
  -msimd128 -pthread 
  -sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency 
  -sINITIAL_MEMORY=512MB -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=4GB 
  -sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=web,worker 
  -lembind -o dist/engine.js

-msimd128 enables Wasm SIMD and lets the compiler auto-vectorize loops. -pthread maps C++ threads onto Web Workers that share one WebAssembly.Memory. PTHREAD_POOL_SIZE spawns the workers at startup, because creating a worker is asynchronous and the engine can’t wait for one in the middle of a filter. -lembind generates JS bindings for C++ classes.

The output is two files: a JS loader and an engine.wasm binary. Ship a second build without -pthread for pages that are not cross-origin isolated (more on that below).

Linear memory

WebAssembly linear memory layout with a tile pool, JavaScript typed array views into it, and the costs of copying, transferring, or sharing pixels

A Wasm module sees memory as one flat, resizable byte array called linear memory. In JavaScript it is a WebAssembly.Memory whose buffer is an ArrayBuffer, or a SharedArrayBuffer when the memory is created with shared: true. A C++ pointer is just an integer offset into that buffer.

That one fact explains the whole boundary. JS can read and write engine memory directly through a typed array view, with no copy. But JS objects cannot live in linear memory, so anything that is not bytes (strings, objects, callbacks) has to be converted when it crosses.

Crossing the JS/Wasm boundary

A call from JS into Wasm is cheap, in the range of a normal function call. What is expensive is doing it millions of times, or converting data on every call. The rule: cross once per command or per tile, never per pixel.

The binding surface is deliberately tiny. The C++ Engine class exposes a handful of coarse methods through embind: run a named filter on a layer, return a view of one tile’s pixels, and a few more like it. Each one does a lot of work per call. A tile view is a typed_memory_view over the tile’s 256 KiB inside linear memory, valid only until memory grows or the tile is released. Everything fine-grained, like per-pixel loops, stays on the C++ side.

A typed memory view hands JS a Uint8Array that points straight at the tile inside linear memory. A string argument, like a filter name, is the opposite case: embind copies it into the Wasm heap on every call. That is fine once per command and wasteful in a loop.


interface EngineModule {
  Engine: new () => Engine;
}

interface Engine {
  applyFilter(layerId: number, name: string, radius: number): void;
  tileView(layerId: number, tx: number, ty: number): Uint8Array;
  delete(): void;
}

On the JS side, the worker awaits the Emscripten module factory once, constructs one Engine, and keeps it for the life of the document. Writing decoded pixels into a tile view writes straight into the Wasm heap with no copy. Bytes that must outlive the view are copied out explicitly.

Two traps catch people here. First, when non-shared memory grows, the old ArrayBuffer is detached and every view on it reads as length 0. Shared memory does not detach, but old views still cover only the old length. Either way, re-create views after any call that can allocate. Second, C++ objects created through embind live in the Wasm heap, so the JS garbage collector can’t free them. Call delete() or you leak engine memory.

Way to move pixels Copies? Use it for Catch
Typed array view No JS reading engine memory on the same thread Stale after memory grows
postMessage clone Yes Small results, thumbnails 192 MB clones block both threads
Transferable ArrayBuffer No Decoded file bytes into the engine worker Sender loses it; Wasm memory can’t be transferred
SharedArrayBuffer No Wasm memory across the thread pool Needs cross-origin isolation and Atomics

SIMD

Wasm SIMD gives you 128-bit vectors: 16 bytes, 8 × 16-bit, or 4 × 32-bit lanes per instruction. One RGBA8 pixel is 4 bytes, so one instruction processes 4 pixels. The compiler auto-vectorizes simple loops with -msimd128, and you hand-write intrinsics for the hottest kernels.

Treat the hand-written kernels as a small library of per-tile functions with a fixed contract: a pointer to RGBA pixels and a pixel count, processed in place. The vector loop handles four pixels per instruction and a scalar tail handles any remainder. Inverting RGB is the simplest example: XOR each 32-bit lane with a mask of 0x00FFFFFF, which flips the three color bytes and leaves alpha untouched, because in little-endian lanes alpha is the top byte.

XOR with 0xFF equals 255 - x for a byte, so the mask inverts RGB and leaves alpha alone. A 256 × 256 tile has 65,536 pixels, which divides by 4, so the scalar tail never runs on full tiles. Real filters like blur widen bytes to 16- or 32-bit lanes before multiplying, which is why 16-bit documents cost about twice as much per pixel.

Loading a large binary fast

An engine like this compiles to tens of megabytes of Wasm. Assume 20 MB compressed: that is 1.6 seconds at 100 Mbps before compilation even starts. Four techniques bring it down.

  1. Streaming compilation. WebAssembly.instantiateStreaming(fetch(url), imports) compiles while bytes arrive. It requires the server to send Content-Type: application/wasm.
  2. Tiered compilation and code caching. V8 compiles quickly with its baseline compiler (Liftoff), optimizes hot functions with TurboFan, and caches compiled code for modules loaded through the streaming APIs. The second visit skips most of the work. Keep the URL stable per build (hashed filename, Cache-Control: immutable) so the cache hits.
  3. Split the engine. Ship a core module with file open, layers, compositing, and the brush. Rarely used filters become separate modules loaded on first use. Emscripten supports this with dynamic linking (side modules), and Binaryen’s wasm-split can split one module by profile.
  4. Precache in a service worker. After the first load, the binary comes from Cache Storage, and an update downloads in the background.

Threads, Isolation, and the Main Thread

Cross-origin isolation

SharedArrayBuffer was restricted after Spectre, because shared memory plus a worker incrementing a counter is a high-resolution timer. Browsers now expose it only to pages that are cross-origin isolated. Without it there are no Wasm threads.

Header Value Why
Cross-Origin-Opener-Policy same-origin Puts the page in its own browsing context group
Cross-Origin-Embedder-Policy require-corp or credentialless Every subresource must opt in to being loaded
Cross-Origin-Resource-Policy same-site or cross-origin Sent by your own CDN so its assets pass COEP
Content-Type (for .wasm) application/wasm Required for streaming compilation

Check self.crossOriginIsolated at startup. If it is false, load the single-threaded build. The app is slower, not broken.

Now return to clarifying question 6, because isolation has consequences:

  • Third-party images and scripts must send CORP or be loaded with CORS (crossorigin="anonymous" plus Access-Control-Allow-Origin). You need CORS on stock images anyway, because the engine must read their pixels.
  • Iframes such as the stock picker must themselves be served with COEP and CORP, or they won’t load.
  • Popups opened with window.open lose their opener link under same-origin COOP, so an OAuth popup can’t post the token back. Use a redirect flow, or have the popup land on a same-origin callback page that signals the app through a BroadcastChannel.
  • COEP: credentialless (Chromium and Firefox) loads no-cors cross-origin resources without cookies instead of requiring CORP. It is the easier migration path where it is supported.

Main thread versus engine worker

Run the engine in a dedicated worker, not on the main thread. Three reasons: filters take seconds, Atomics.wait is not allowed on the main thread (and Emscripten’s pthread mutexes rely on blocking), and the OPFS synchronous file API only exists in workers.

The canvas moves with it. transferControlToOffscreen() hands the canvas to the worker, which renders with WebGPU (or WebGL2 as a fallback) and presents frames without any main-thread involvement.


type StrokeSample = { x: number; y: number; pressure: number; tiltX: number; tiltY: number; t: number };

The main thread does three things at startup and nothing heavy afterward.

  • Create the engine as a module worker and transfer the canvas to it with transferControlToOffscreen(), along with the device pixel ratio and whether the page is cross-origin isolated (which picks the threaded or single-threaded build).
  • On pointerdown, capture the pointer so a stroke that leaves the canvas keeps drawing.
  • On pointermove with a button held, convert the coalesced events and the predicted events to StrokeSamples and post one stroke message per frame.

Pointer events only arrive on the main thread, so input still takes one hop. Keep that thread nearly idle: a 30 ms React render of the layers panel delays every pen sample behind it. Set touch-action: none on the canvas so the browser doesn’t pan or zoom the page under the pen.

Brush latency

A pen digitizer reports at 240 Hz or more, but browsers deliver one pointermove per frame. getCoalescedEvents() returns every sample since the last event, so a fast curve stays a curve instead of becoming straight segments. getPredictedEvents() returns where the pen is likely to be next.

  1. The engine interpolates between samples and stamps a dab every 10–25% of the brush diameter, with size and opacity driven by pressure.
  2. Dabs are written into the layer’s tiles. Each touched tile is cloned on its first write in the stroke (copy-on-write, below).
  3. The predicted segment is drawn into a temporary overlay only, then thrown away next frame. It hides one frame of latency without ever touching the document.
  4. The compositor uploads only the dirty tiles and presents.

Applying a filter across threads

Sequence diagram of a Gaussian blur command flowing from the UI to the engine worker, the Wasm thread pool, and the GPU compositor

A filter is a perfect parallel job, because tiles are independent units. The engine posts one job per tile, and each worker repeatedly grabs the next tile index with an atomic fetch_add on a shared counter. Dynamic grabbing beats a static split: tiles with a transparent mask finish instantly while busy tiles take longer, and nobody idles.

  • Halos. A radius-8 blur needs 8 pixels from each neighbouring tile. Workers read neighbours from the source grid, which is safe because copy-on-write keeps the source immutable while new tiles are written.
  • Progress. A second shared counter of finished tiles. The engine reads it a few times a second and posts progress to the UI.
  • Cancel. An atomic flag checked before each tile. Cancelling drops the new tiles and the history state never commits.
  • Preview. While the user drags the radius slider, run a GPU shader on the visible tiles at screen resolution. Run the full-resolution Wasm pass only when the dialog is confirmed.

Size the pool at about navigator.hardwareConcurrency - 1. Leaving one core for the main thread keeps input responsive while the filter runs.

Compositing on the GPU

The compositor keeps a texture per visible tile per layer, and blends the layer stack bottom-up in a shader: multiply is a × b, screen is 1 - (1 - a)(1 - b), and adjustment layers are lookup-table passes. Masks multiply opacity per pixel.

For 16-bit documents, upload tiles as rgba16float textures. Half floats carry less precision than 16-bit integers, but display output is 8 or 10 bits, so the screen never shows the difference. The authoritative pixels stay in the Wasm heap at full precision.

When zoomed out, compositing 768 full-resolution tiles per layer is wasteful. Keep a mip pyramid per layer (half, quarter, eighth resolution), rebuilt lazily for dirty tiles, and composite from the level that matches the zoom.


Tiles, Memory, and Storage

Copy-on-write tiles for undo

Three history states of a 4 by 3 tile grid showing shared tiles and the few tiles cloned by a brush stroke and a blur

A history state is a list of layer records whose grids point at shared tiles. Taking a snapshot copies pointers and increments reference counts. The first write to a shared tile clones it. Undo swaps the document back to the previous grids: no pixels move.


interface TilePool {
  allocate(): TileRef;                       // refCount = 1, transparent pixels
  copy(from: TileRef, to: TileRef): void;
  release(tile: TileRef): void;              // refCount - 1, frees at 0
}

Two operations sit on top of the pool. Share a grid increments the reference count of every tile in it and returns a new slot array pointing at the same tiles. That is the snapshot. Get a writable tile returns the tile in place if this grid is its sole owner. Otherwise it allocates a fresh tile, copies the pixels, releases the shared one, and puts the fresh tile in the slot. Every write path in the engine goes through that second operation, so no code can mutate a tile another history state still references.

The engine implements this in C++. A brush stroke that touches 20 tiles costs 5 MiB of history, not 192 MB. Dropping the oldest history state releases its tiles, and only tiles no other state references are freed.

Cap history by bytes as well as by step count. One “fill the whole layer” step owns all 768 tiles, so 50 of those would be 9.6 GB. When the byte budget is hit, evict the oldest states first, then page their tiles to disk before deleting them.

Living within 4 GiB

32-bit Wasm addresses at most 4 GiB, and browsers may give you less on low-memory devices. Shared memory also needs a declared maximum up front. A 20-layer 16-bit document is 7.7 GB of pixels before history, so the engine must treat RAM as a cache.

  • Tile pager. Keep resident tiles in an LRU. When the heap passes its budget, write cold tiles (hidden layers, old history, off-screen areas) to a swap file and mark their TileRef location as swap.
  • Compress cold tiles. Image tiles with flat areas compress well with a fast codec like LZ4 before they are swapped.
  • Memory64. 64-bit Wasm memory is shipping in Chromium and Firefox and lifts the 4 GiB cap. It is slower, because engines can no longer use the 4 GiB guard region to skip bounds checks. Treat it as an option for huge documents, not a reason to drop the pager.
  • Measure. performance.measureUserAgentSpecificMemory() (Chromium) works only in cross-origin isolated pages, which you already are. Use it in telemetry to tune budgets per device class.

OPFS for swap and autosave

The Origin Private File System is a per-origin file system that the user does not see. In a dedicated worker, createSyncAccessHandle() gives synchronous, in-place reads and writes, which is exactly what a C++ pager expects. It is far faster than IndexedDB for this access pattern.

The pager lives in the engine worker, because sync access handles exist only in dedicated workers. At startup it opens a swap-<docId>.bin file in OPFS and takes a sync access handle, which also holds an exclusive lock so a second tab can’t corrupt it. The file is an array of fixed-size slots, one tile each, so page-out is a write at slot × tileBytes and page-in is a read at the same offset. A short read is a hard error, not a partial tile. The swap budget is the smaller of a fixed cap and the remaining quota from navigator.storage.estimate() minus a safety margin.

The tile view handed to the pager points directly into Wasm memory, so a tile goes from the heap to disk with no intermediate copy. Call navigator.storage.persist() after the user saves their first document, so the browser is less likely to evict autosave data under storage pressure.

Autosave uses a second OPFS file: a journal of committed commands plus the tiles each one wrote. After a crash, the engine reloads the last snapshot and replays the journal. Emscripten’s WasmFS also has an OPFS backend, which lets the C++ code use ordinary file APIs on top of it.

Opening and saving files

  • Open. Where available (Chromium), showOpenFilePicker() returns a FileSystemFileHandle. Elsewhere, fall back to <input type="file">. Drag and drop works everywhere through DataTransfer.
  • Parse in the engine. Pass the File to the worker (it is cloneable without copying the bytes), stream it with file.stream(), and decode straight into tiles. Never decode a 200 MB PSD into one JS buffer first.
  • Save in place. With a file handle, createWritable() writes back to the same file. Without one, encode in the engine and trigger a download from a Blob URL on an <a download> link.
  • Export. PNG, JPEG, and WebP encoders run in Wasm, so output matches the desktop app byte for byte across browsers.

State and Data Flow

The engine is the single source of truth. The UI never holds pixels or a second copy of the document.

  1. Intent. A panel or tool sends a typed command (setOpacity, applyFilter, stroke) to the engine worker.
  2. Apply. The engine runs it, creates one history state per completed command, and marks dirty tiles.
  3. Render. The compositor uploads dirty visible tiles and presents on its own frame loop.
  4. Summarize. At most once per frame, the engine posts a DocSummary: layer list, selection, history labels, and small layer thumbnails as transferable ImageBitmaps.
  5. Persist. The journal entry is appended to OPFS. The sync worker uploads dirty tiles in the background.

While a user drags the opacity slider, the UI shows its local value immediately and sends throttled commands. The engine coalesces them into one history step when the drag ends.


API

There are two interfaces: the worker message protocol inside the tab, and the cloud API for documents.


// Main thread -> engine worker
type ToEngine =
  | { type: "init"; canvas: OffscreenCanvas; dpr: number; threaded: boolean }
  | { type: "open"; file: File } | { type: "openCloud"; documentId: string }
  | { type: "stroke"; pointerId: number; samples: StrokeSample[]; predicted: StrokeSample[] }
  | { type: "command"; id: string; command: Command }
  | { type: "cancel"; id: string }
  | { type: "undo" } | { type: "redo" }
  | { type: "viewport"; zoom: number; x: number; y: number; width: number; height: number };

// Engine worker -> main thread
type FromEngine =
  | { type: "ready"; threaded: boolean; simd: boolean }
  | { type: "summary"; doc: DocSummary }
  | { type: "progress"; commandId: string; done: number; total: number }
  | { type: "error"; commandId?: string; code: "OUT_OF_MEMORY" | "QUOTA_EXCEEDED" | "PARSE_FAILED" | "ENGINE_CRASHED"; message: string };

// Cloud documents (REST)
GET  /api/documents/:id                  // manifest: layer tree + tile hashes per layer, version
POST /api/blobs/missing                  // { hashes } -> the hashes the server doesn't have yet
PUT  /api/blobs/:sha256                  // one compressed tile; immutable
GET  /api/blobs/:sha256                  // cacheable forever
POST /api/documents/:id/versions         // { baseVersion, manifest } -> 201 or 409 if stale

// Realtime (WebSocket): wss://sync.example.com/documents/:id
type SyncEvent =
  | { type: "version"; version: number; by: string }
  | { type: "editLock"; heldBy: string | null };

Cloud sync is content-addressed. Each tile is compressed and hashed, the client asks which hashes are missing, uploads only those, then commits a manifest with the version it was based on. A brush stroke uploads a few tiles, not the whole file, and two versions that share most tiles share most storage.

Conflicts are rare because there is one editor at a time, enforced by a soft edit lock. If a commit still returns 409 (the document was edited on another device), don’t try to merge pixels. Save the local work as a copy and tell the user.


Optimizations

Problem Technique Effect
Slow first load Streaming compile, code cache, split modules Second load skips most compilation
Boundary overhead One call per command or tile, typed array views No per-pixel crossings or copies
Undo memory Copy-on-write tiles, byte-capped history A stroke costs MiB, not hundreds of MB
Empty areas Sparse tile slots New layers cost nothing
Slow filters SIMD + thread pool + atomic work queue All cores busy, no idle stragglers
Slider preview lag GPU shader preview on visible tiles Instant feedback, full pass on confirm
Memory pressure LRU tile pager to OPFS, compressed Large files get slower, not killed
Zoomed-out compositing Per-layer mip pyramid Composites 1/64 of the pixels at 12.5%

At 12.5% zoom, the eighth-resolution level has (1/8)² = 1/64 of the pixels. That is the difference between compositing 48 M pixels per layer and 750 K.


Handling Edge Cases

  • Not cross-origin isolated. An embed or an old browser lacks isolation. Load the single-threaded build, disable the thread pool, and keep everything else.
  • Allocation fails. malloc returns null when memory can’t grow. The engine pages out cold tiles, drops the oldest history, retries once, and then reports OUT_OF_MEMORY with a suggestion to flatten or reduce bit depth.
  • Wasm trap. A C++ bug throws a WebAssembly.RuntimeError. Engine state is now undefined, so terminate the worker, start a new one, and recover from the OPFS journal. Never keep running on a heap you can’t trust.
  • Storage quota. A write throws QuotaExceededError. Stop swapping history first, warn the user, and keep the document itself working in RAM.
  • Two tabs, one document. The second createSyncAccessHandle() on the same file fails because the first tab holds the lock. Open the second tab read-only.
  • GPU device lost. Listen to WebGPU’s device.lost promise (or webglcontextlost). Recreate the device and re-upload visible tiles from the heap. GPU memory is never the source of truth.
  • Tab hidden mid-filter. Background tabs can be throttled, frozen, or discarded. On visibilitychange to hidden, checkpoint the journal so a discard loses nothing, and pause the compositor since nobody is looking.

Accessibility and Security

Accessibility

The canvas is one bitmap to assistive technology. The panels are where accessibility lives, and they carry real weight in a tool like this.

  • The layers panel is a list with aria-selected rows, arrow-key navigation, and keyboard reorder (for example, Cmd+[ and Cmd+]) as the alternative to drag and drop.
  • Opacity, size, and hardness are native <input type="range"> elements with labels, a paired number field, and aria-valuetext like “Opacity 45 percent”.
  • Long filters show a role="progressbar" and announce completion through an aria-live="polite" region.
  • The canvas has an accessible name with the document title and zoom level, and color pickers always show numeric values (hex, RGB, HSB) next to the swatch.
  • Respect forced-colors for panel chrome and prefers-reduced-motion for animated zoom.

Security

  • Isolation helps you. COOP and COEP keep other origins out of your process, which matters when your heap holds a user’s private photos.
  • Untrusted file parsing. PSD and TIFF parsers in C++ have a history of memory-safety bugs. Wasm contains the damage: a bad pointer can only corrupt linear memory, never the browser. But it can still corrupt your document, so parse imported files in a separate worker with its own Wasm instance, and pass only validated tiles to the main engine.
  • CSP. Compiling WebAssembly under a strict Content Security Policy needs script-src 'self' 'wasm-unsafe-eval', which permits Wasm compilation without allowing JS eval.
  • Cloud blobs. Serve tiles through short-lived signed URLs, and check permissions on every manifest commit, not just on open.

What a Strong Answer Sounds Like

Topic Junior answer Senior answer
Why Wasm “It’s faster than JS” Port tested C++ with Emscripten; SIMD, threads, no GC pauses
Boundary Pass pixel arrays to C++ functions Typed array views into linear memory, one call per tile
Threads “Use Web Workers” Shared Wasm memory, COOP/COEP, fallback build, atomic work queue
Memory Load the image into a canvas Sparse tiles, LRU pager to OPFS, 4 GiB math, Memory64 trade-off
Undo Store a copy per step Copy-on-write tiles with refcounts and a byte budget
Responsiveness Show a spinner during filters Engine + OffscreenCanvas in workers, coalesced pointer events, GPU preview

Conclusion: How to Design Photoshop on the Web

Start with the constraint the interviewer gave you: an existing C++ engine. Compile it to WebAssembly with Emscripten, enable SIMD and threads, and serve the page cross-origin isolated so the thread pool can share one SharedArrayBuffer. Put the engine and the canvas in a worker, and keep the main thread for panels and input.

Then make the tile the unit of everything. 256 × 256 tiles give you sparse layers, copy-on-write undo, parallel filters, paging to OPFS when 4 GiB runs out, and cloud sync that uploads only what changed. If you can do the 192 MB napkin math and explain why a typed array view goes stale after memory grows, you’ve shown the interviewer you have actually built in WebAssembly.