Photoshot development · 01
Reversible layer masks in a browser editor: separating document state from rendering
From a React–PixiJS adapter to a shared Canvas compositor, with explicit source provenance and a reproducible mask diagnostic.
Source scope: initial eead3ca / later v0.16.0 · 19f45ed. This record does not audit every feature of the current production editor.
Abstract
The target was a browser editor in which image, text and paint layers retain their source data while masks change visibility. An initial PixiJS adapter could display document layers, but rendering primitives did not supply the application’s persistence and masking contract. We trace the installed dependencies, the application-specific adapter and the later shared compositor. A new 100 × 100 pixel diagnostic contrasts destructive erasure with document-level masking: disabling the latter restores source RGBA exactly. Five source-level checks pass. This is an engineering record with a controlled counterexample, not a claim of universal Photoshop compatibility.
1. Objective and acceptance criteria
The intended feature was not simply a canvas showing several images. A layer needed an independent position, size, rotation, opacity and blend mode. A mask needed to hide and restore content without overwriting the source. The same document also needed to survive serialization and produce a composited output. These requirements determine what “working” means for this entry.
We therefore separate three objects: the persistent document, the temporary surface rendered for one layer, and the display object shown by the graphics library. React controls the UI; the layer model owns edit parameters and mask strokes. A screenshot of an attractive composition alone cannot establish that those objects remain consistent.1
2. Initial implementation and the integration obstacle
The initial public commit, eead3ca, imports Application, Sprite and Texture from PixiJS. It creates one sprite per visible layer and transfers document fields into renderer properties. Rotation is converted from degrees to radians, the anchor is centered, and position is offset by half the layer dimensions. This is direct API reuse of an installed library, with separately written adapter code.
The obstacle was the boundary between a graphics object and an editable document. A Sprite does not define our mask history, project schema or export semantics. Moreover, the initial stage renders through PixiJS while document export has its own Canvas composition loop. Source inspection establishes these separate paths; it does not establish a measured preview/export error for every image. We do not turn that architectural risk into an invented historical failure.1
eead3ca'use client';
import { useEffect, useRef } from 'react';
import { Application, Sprite, Texture } from 'pixi.js';
import 'pixi.js/advanced-blend-modes';
import { layerSurface, type StudioDoc } from './document';eead3ca const s = new Sprite(Texture.from(canvas));
s.anchor.set(0.5);
s.position.set(l.x + l.width / 2, l.y + l.height / 2);
s.rotation = (l.rotation * Math.PI) / 180;
s.alpha = l.opacity;
s.blendMode = l.blend;
a.stage.addChild(s);3. GitHub material and the reuse decision
React and PixiJS supplied established UI and rendering infrastructure. The concrete evidence of PixiJS use is the import and Sprite construction in our own initial commit, rather than the presence of a repository URL in a reference list. The layerSurface call, coordinate mapping and document schema are Photoshot integration code. A whole external photo editor was not transplanted.23
Photopea’s mask documentation supplied a behavioral comparison: content must remain recoverable when the mask is disabled. It is documentation consulted for expected behavior, not evidence that Photopea source code was available or copied. For the later compositor we cite Photoshot’s own exact revision, 19f45ed, instead of attributing its implementation to PixiJS.4
| Source | Use category | Contribution | Boundary |
|---|---|---|---|
| React | Installed dependency | UI components and state | No claim of a copied editor |
| PixiJS | Installed dependency; early stage | Application, Sprite and Texture APIs | Later Stage uses Canvas composition |
| Photopea Masks | Behavioral documentation | Recoverable visibility edits | No source transplant |
| Photoshot | Application-specific code | Document, adapter, masking and shared composition | Exact source revisions linked below |
Dependency use, behavioral consultation and application code are separate provenance claims.
4. Implementation: retain the source; compose a temporary surface
For each render, layerSurface creates a Canvas, draws the layer’s source or paint strokes, and applies the mask with destination-in. The mask’s alpha controls how much of that temporary destination survives. The operation then returns to source-over. Mask strokes and source strokes remain distinct fields in the document; disabling the mask causes the surface to be regenerated from the retained source.
In the later Stage, preview calls the shared composite function. Layer order, transforms, blending and adjustments are handled through the document composition path. Preview requests bound the longest edge to 1,600 pixels and cancel stale work using a revision predicate. Sharing that path narrows the number of independent render implementations; the preview-size limit means full-resolution equivalence still requires an appropriately matched test.5
19f45ed if (layer.mask?.enabled) {
ctx.globalCompositeOperation = 'destination-in';
ctx.drawImage(maskSurface(layer.mask), 0, 0, c.width, c.height);
ctx.globalCompositeOperation = 'source-over';
}19f45ed const image = await composite(d, false, {
maxEdge: 1600,
cancelled: () => !mounted.current || r !== revision.current,
});
if (!mounted.current) break;v0.16.0 composition path. The early PixiJS stage is discussed in Listings 1–2; the arrows here represent data flow, not benchmark timings. Preview and export use the same compositor but may request different resolutions.5. Diagnostic method and observed result
For this revision of the article, we executed the unchanged public document engine under Node with @napi-rs/canvas. The input is a 100 × 100 paint layer with blue and orange strokes. A 40-pixel mask stroke hides its center. We compare source, enabled-mask and disabled-mask surfaces, then serialize the document to JSON, validate it and composite it again. The source document is also compared before and after rendering.
The negative control deliberately erases the center of a retained Canvas and then stops applying the erase operation. Merely disabling that operation cannot reconstruct pixels that were already removed. It is a newly constructed diagnostic alternative, not an archived early Photoshot build. At the center, the destructive control remains transparent (alpha 0), whereas disabling the document mask restores alpha 255. The complete disabled-mask RGBA array equals the original source array, and the masked project round trip equals its pre-save render.6
| Check | Observation | Result |
|---|---|---|
| Source document before/after | Identical JSON | PASS |
| Mask at the center | Alpha 0 | PASS |
| Restore stroke at the center | Alpha 255 | PASS |
| Mask disabled | RGBA equals original source | PASS |
| JSON serialize → validate → composite | RGBA equals masked render | PASS |
Five deterministic checks on the stated synthetic input. JSON round trip is not an IndexedDB durability test. Browser and Photoshop parity are outside this rerun.
6. Interpretation, scope and next step
The result supports a precise implementation claim: this source revision preserves the source and can regenerate the tested image when its document mask is disabled. It does not show that every image format, GPU/browser combination, transformed mask or blend mode matches Photoshop. The diagnostic uses a native Canvas implementation and does not replace a browser interaction test, a real IndexedDB save/reopen test, or a full-resolution export comparison.
The architectural change is also narrower than replacing one library name with another. We reused React and early PixiJS, wrote the document adapter and mask plumbing, and later routed preview through our own common compositor. Part 2 follows a numerical failure: a plausible brightness/contrast response produced visibly wrong chart values, prompting a measured-response correction rather than a cosmetic UI change.
Code, data and reproducibility
Reproduce and inspect: unchanged source files, exact excerpt provenance and the new diagnostic outputs are available below. The diagnostic harness imports Photoshot’s document engine; it is separate from the production editor.