AI-native design editor. Open-source Figma alternative.
feat: fill layers with shaders (#1051)
Danila Poyarkov906173ef44bb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3220d1b6c..eafcce8c5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -54,6 +54,7 @@- Comment on designs, as in Figma. Pick the **Comment** tool or press <kbd>C</kbd>, click the canvas, and type; the pin shows your avatar and stays on the top-level layer under it when that layer moves. Open a pin to reply, resolve, copy, or delete it. Comments take bold, italic, strikethrough, links, and numbered and bulleted lists, with Figma's shortcuts or Markdown, and Shift+Enter continues a list. While the Comment tool is active the right sidebar lists the document's threads with search, newest- or oldest-first order, and filters for resolved comments, your threads, and the current page; **View → Comments** (<kbd>⇧</kbd><kbd>C</kbd>), also in the zoom menu and a pin's right-click menu, hides pins outside the tool. Comments are saved in the document as OpenPencil plugin data, so they travel with the `.fig` file, its storage copy, and a collaboration room, comments two people add at the same time are both kept, and Figma and older versions of OpenPencil still open the file. Adding or resolving a comment marks the document as changed, but undo leaves comments alone. Scripts, the AI chat, and MCP clients can act on review feedback too: `openpencil.getComments()`, `addComment()`, and each comment's `reply()` and `resolve()` in `openpencil eval`, and the `get_comments`, `add_comment`, `reply_to_comment`, and `resolve_comment` tools.- Edit a polygon's or star's corner radius, point count, and a star's inner ratio in the properties panel, as in Figma: **Count** from 3 to 60 and **Ratio** as a percentage.- Round a rectangle's, polygon's, or star's corners on the canvas, as in Figma: while the pointer is over a selected shape at least 108 screen pixels across, a handle sits inside each corner of a rectangle and below the top point of a polygon or star, and handles on a polygon's or star's next point and a star's first inner point set its point count and inner ratio. Dragging a rectangle's handle sets every corner while they are equal and only that corner while they differ, <kbd>⌥</kbd> swaps the two, and <kbd>⇧</kbd> rounds to tens; a label beside the pointer shows the radius.+- Fill layers with shaders from the open-source [`shaders`](https://shaders.com) library: the fill picker's Shader tab stacks effects, generates controls for each effect's props, and takes a preset copied from shaders.com, and design JSX takes `shader()` paints. The canvas shows each shader's first frame and preview plays it; both need WebGPU, without which a shader shows the frame it was saved with. Safari copies each playing frame from WebGPU into the canvas synchronously, so there preview updates shaders in turn, less often the more are on screen. Files and other formats keep that frame as an image fill with the preset in OpenPencil plugin data, so a `.fig` file opens in Figma with the frame and reopens in OpenPencil as the shader. Tailwind JSX and Vue and React Storybook exports play a layer's shader behind its content with the library's `<Shader>`, telemetry off, and HTML shows its still frame there.- Edit several layers at once as in Figma: the properties panel gives a multi-selection a Layout section with sizes, text resizing, flow, clip content, and a Spacing field that spaces a row or column evenly; Typography for the text among it; corner radius for the layers that have one; the stroke of the layers that have strokes; and Selection colors, which lists every fill and stroke colour in the selection and its children and recolours one everywhere it is used. The panel's header reads "N selected".- Edit gradients on the canvas, as in Figma: while a fill or stroke gradient's picker is open, the layer shows the gradient's line or ellipse with its end dots and stop squares. Dragging a dot moves it, <kbd>Shift</kbd> turns it in 15° steps, and dragging a square slides its stop; the stop selected on the canvas is the one selected in the picker.- Paste SVG markup copied as text, such as from a code editor or Figma's Copy as SVG, as layers.diff --git a/bun.lock b/bun.lockindex 9fce1c935..0a663bb18 100644--- a/bun.lock+++ b/bun.lock@@ -318,6 +318,7 @@"react": "^19.3.0","react-dom": "^19.3.0","reka-ui": "^2.10.5",+ "shaders": "4.0.4","storybook": "^10.6.1","tsdown": "^0.22.14","typescript": "~5.8.3",@@ -462,6 +463,7 @@"fuse.js": "^7.5.0","nanostores": "^1.5.4","reka-ui": "^2.10.5",+ "shaders": "4.0.4",},"devDependencies": {"tsdown": "^0.22.14",@@ -4012,6 +4014,8 @@"setprototypeof": ["[email protected]", "", {}, "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="],+ "shaders": ["[email protected]", "", { "dependencies": { "typegpu": "0.12.3" }, "peerDependencies": { "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0", "solid-js": "^1.8.0", "svelte": "^5", "vue": "^3.5.0" }, "optionalPeers": ["react", "react-dom", "solid-js", "svelte", "vue"], "bin": { "shaders": "dist/cli.js" } }, "sha512-NB+/RiK0oydteeeKdPqsLpR3iSoDTvt8bsdgw2wPb697QVCY/yadZV8gitCyCQx5t/kb4nJicIR+cbG9Q6JFfQ=="],+"sharp": ["[email protected]", "", { "dependencies": { "@img/colour": "^1.0.0", "detect-libc": "^2.1.2", "semver": "^7.7.3" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.34.5", "@img/sharp-darwin-x64": "0.34.5", "@img/sharp-libvips-darwin-arm64": "1.2.4", "@img/sharp-libvips-darwin-x64": "1.2.4", "@img/sharp-libvips-linux-arm": "1.2.4", "@img/sharp-libvips-linux-arm64": "1.2.4", "@img/sharp-libvips-linux-ppc64": "1.2.4", "@img/sharp-libvips-linux-riscv64": "1.2.4", "@img/sharp-libvips-linux-s390x": "1.2.4", "@img/sharp-libvips-linux-x64": "1.2.4", "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", "@img/sharp-libvips-linuxmusl-x64": "1.2.4", "@img/sharp-linux-arm": "0.34.5", "@img/sharp-linux-arm64": "0.34.5", "@img/sharp-linux-ppc64": "0.34.5", "@img/sharp-linux-riscv64": "0.34.5", "@img/sharp-linux-s390x": "0.34.5", "@img/sharp-linux-x64": "0.34.5", "@img/sharp-linuxmusl-arm64": "0.34.5", "@img/sharp-linuxmusl-x64": "0.34.5", "@img/sharp-wasm32": "0.34.5", "@img/sharp-win32-arm64": "0.34.5", "@img/sharp-win32-ia32": "0.34.5", "@img/sharp-win32-x64": "0.34.5" } }, "sha512-Ou9I5Ft9WNcCbXrU9cMgPBcCK8LiwLqcbywW3t4oDV37n1pzpuNLsYiAV8eODnjbtQlSDwZ2cUEeQz4E54Hltg=="],"shebang-command": ["[email protected]", "", { "dependencies": { "shebang-regex": "^3.0.0" } }, "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA=="],@@ -4214,6 +4218,8 @@"tiny-invariant": ["[email protected]", "", {}, "sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg=="],+ "tinyest": ["[email protected]", "", {}, "sha512-81EcMHTCQ2IPweOGk9SpMgjbenR9ZJRsAE84btEv0DhvVKwhe7qBy28hWgatoXa9ppWzmtpHiJ+e86SiaAtY6w=="],+"tinyexec": ["[email protected]", "", {}, "sha512-QKAl9m8gWWGHV8jZcPeym6j+XULi6tOf1mT83WYJ4Lk2ytW/uwAWkrP0uFsdoYMdueVJ0qs26wZ+23xeB4ibNQ=="],"tinyglobby": ["[email protected]", "", { "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.4" } }, "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g=="],@@ -4260,6 +4266,8 @@"tslib": ["[email protected]", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],+ "tsover-runtime": ["[email protected]", "", {}, "sha512-FHHwMJzZbnP23+fU1PxXljy7j0RRRosL2r1/CRUNTqfW+l7m35JsUkM615x/cAzw4GqRdXPIl3axKMj9qzpZtQ=="],+"tsx": ["[email protected]", "", { "dependencies": { "esbuild": "~0.28.0" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "bin": { "tsx": "dist/cli.mjs" } }, "sha512-FDf4L4sYzKtzWYhU/Xm0AQFdTjdIxNo9ElTf2mxXM6k8YMHXzYUe4yODVaXP4V9uMFbVg8c0qyBccK2OOxb45Q=="],"tunnel-agent": ["[email protected]", "", { "dependencies": { "safe-buffer": "^5.0.1" } }, "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w=="],@@ -4292,8 +4300,12 @@"typed-array-length": ["[email protected]", "", { "dependencies": { "call-bind": "^1.0.9", "for-each": "^0.3.5", "gopd": "^1.2.0", "is-typed-array": "^1.1.15", "possible-typed-array-names": "^1.1.0", "reflect.getprototypeof": "^1.0.10" } }, "sha512-phPGCwqr2+Qo0fwniCE8e4pKnGu/yFb5nD5Y8bf0EEeiI5GklnACYA9GFy/DrAeRrKHXvHn+1SUsOWgJp6RO+g=="],+ "typed-binary": ["[email protected]", "", {}, "sha512-W2hLsSzt3k/tg38gDE4Fn/QiwcoqGuUHBc2cb3mXuH7KcYxe/GM57vzW14s2/bawB4R5knGgGq8Xb57vsaJ4Sg=="],+"typedarray": ["[email protected]", "", {}, "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA=="],+ "typegpu": ["[email protected]", "", { "dependencies": { "tinyest": "~0.3.2", "tsover-runtime": "^0.0.7", "typed-binary": "^4.3.3" }, "bin": { "typegpu": "./bin.mjs" } }, "sha512-zYwQEhnPPwhq5vQKmollXRK1LCVjStOnL+2B0PiebqvAzYPr25rPNR4r3olLoow5sKKjyj3kzHKEcpD5SyDzGQ=="],+"typescript": ["[email protected]", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="],"uc.micro": ["[email protected]", "", {}, "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A=="],diff --git a/packages/core/src/canvas/fills.ts b/packages/core/src/canvas/fills.tsindex ea3d201b7..af7a045cc 100644--- a/packages/core/src/canvas/fills.ts+++ b/packages/core/src/canvas/fills.ts@@ -1,4 +1,4 @@-import type { Canvas, Paint } from 'canvaskit-wasm'+import type { Canvas, Image, Paint } from 'canvaskit-wasm'import type { SceneNode, SceneGraph, Fill } from '@open-pencil/scene-graph'import { invertGradientTransform } from '@open-pencil/scene-graph/gradient'@@ -424,6 +424,44 @@ export function makeImageFillLocalMatrix()}+/**+ * Sets `paint`'s shader to `img` laid out as `fill` places it on `node`; `preview`, when `img`+ * is a downscaled preview, keeps a tiled image at its original size.+ */+function setImageShader(+ r: SkiaRenderer,+ fill: Fill,+ node: SceneNode,+ img: Image,+ paint: Paint,+ preview?: { originalWidth: number; originalHeight: number }+): boolean {+ const imgW = img.width()+ const imgH = img.height()+ const scaleMode = fill.imageScaleMode ?? 'FILL'+ const localMatrix =+ scaleMode === 'TILE' && !fill.imageTransform && preview+ ? r.ck.Matrix.scaled(preview.originalWidth / imgW, preview.originalHeight / imgH)+ : makeImageFillLocalMatrix(r, fill, node, imgW, imgH)+ const tileMode = scaleMode === 'FIT' ? r.ck.TileMode.Decal : r.ck.TileMode.Clamp+ const shader =+ scaleMode === 'TILE'+ ? img.makeShaderCubic(r.ck.TileMode.Repeat, r.ck.TileMode.Repeat, 1 / 3, 1 / 3, localMatrix)+ : img.makeShaderOptions(+ tileMode,+ tileMode,+ r.ck.FilterMode.Linear,+ r.ck.MipmapMode.Linear,+ localMatrix+ )+ try {+ paint.setShader(shader)+ } finally {+ shader.delete()+ }+ return true+}+export function applyImageFill(r: SkiaRenderer,fill: Fill,@@ -433,6 +471,8 @@ export function applyImageFill(): boolean {const hash = fill.imageHashif (!hash) return false+ const live = r.liveImages.get(hash)+ if (live?.surface === r.surface) return setImageShader(r, fill, node, live.image, paint)const preview = r.viewportImageRendering? // eslint-disable-next-line open-pencil/no-zoom-in-scene-drawing -- picks the preview resolution, not a size; preview mode draws the scene uncached on every frame.r.imagePreviews.get(graph, hash, previewEdge(node, r.zoom, r.dpr))@@ -455,33 +495,7 @@ export function applyImageFill(}try {- const imgW = img.width()- const imgH = img.height()- const scaleMode = fill.imageScaleMode ?? 'FILL'- const localMatrix =- scaleMode === 'TILE' && !fill.imageTransform && preview- ? r.ck.Matrix.scaled(- preview.preview.originalWidth / imgW,- preview.preview.originalHeight / imgH- )- : makeImageFillLocalMatrix(r, fill, node, imgW, imgH)- const tileMode = scaleMode === 'FIT' ? r.ck.TileMode.Decal : r.ck.TileMode.Clamp- const shader =- scaleMode === 'TILE'- ? img.makeShaderCubic(r.ck.TileMode.Repeat, r.ck.TileMode.Repeat, 1 / 3, 1 / 3, localMatrix)- : img.makeShaderOptions(- tileMode,- tileMode,- r.ck.FilterMode.Linear,- r.ck.MipmapMode.Linear,- localMatrix- )- try {- paint.setShader(shader)- } finally {- shader.delete()- }- return true+ return setImageShader(r, fill, node, img, paint, preview?.preview)} finally {if (temporary) img.delete()}diff --git a/packages/core/src/canvas/renderer.ts b/packages/core/src/canvas/renderer.tsindex 320216181..1f5dba919 100644--- a/packages/core/src/canvas/renderer.ts+++ b/packages/core/src/canvas/renderer.ts@@ -32,7 +32,7 @@ import { LabelParagraphCache } from './labels/paragraph-cache'import { labelHitOptions } from './labels/style'import * as RenderColors from './renderer/colors'import * as RendererFonts from './renderer/fonts'-import { destroyRenderer } from './renderer/lifecycle'+import { destroyRenderer, releaseLiveImages } from './renderer/lifecycle'import { installRendererDomainMethods } from './renderer/methods'import { initializeRendererPaints, initializeSelectionPaintColors } from './renderer/paints'import * as RenderPipeline from './renderer/pipeline'@@ -61,7 +61,9 @@ import type {ImageFilter,MaskFilter,RuntimeEffect,- Paragraph+ Paragraph,+ Image,+ TextureSource} from 'canvaskit-wasm'export interface SubtreePictureCacheEntry {@@ -83,6 +85,17 @@ import { TiledSceneController } from './renderer/tiles'import type { TransientCanvasPreview } from './renderer/transient-previews'import type { PresenceCursor, RenderOverlays, RulerTheme, SelectionTheme } from './renderer/types'+/**+ * A picture uploaded as a texture that knows its size: an `ImageBitmap`, or a canvas, which+ * WebGL uploads as it does an image though CanvasKit's types leave it out. A canvas is uploaded+ * as it is when this is called, so a WebGPU canvas is passed in the task that drew it.+ */+export type LiveImageSource =+ | Extract<TextureSource, { width: number; height: number }>+ | HTMLCanvasElement++const textureSource = (source: LiveImageSource) => source as TextureSource+export class SkiaRenderer {ck: CanvasKitsurface: Surface@@ -117,6 +130,11 @@ export class SkiaRenderer {textPictureGenerations = new Map<string, { data: Uint8Array; generation: number }>()readonly transientPreviews = new Map<string, TransientCanvasPreview>()imageCache = createImageCache()+ /**+ * Images drawn in place of stored ones while they play, such as a shader's live frames, each+ * with the surface whose GL context holds its texture.+ */+ readonly liveImages = new Map<string, { image: Image; surface: Surface }>()viewportImageRendering = falseimageMemoryGraph: SceneGraph | null = nullimageMemoryPage: string | null = null@@ -544,6 +562,8 @@ export class SkiaRenderer {replaceSurface(surface: Surface): void {this.tiledScene.destroy()+ // Live textures belong to the old surface's context, so they go before it does.+ releaseLiveImages(this)this.surface.delete()this.surface = surfacethis.sceneBackingAllocationFailed = false@@ -558,6 +578,48 @@ export class SkiaRenderer {RendererState.invalidateAllPictures(this)}+ /**+ * Draws `source` wherever the image `hash` is painted, until it is set to null, and redraws+ * `nodeIds`, the layers that paint it. Frames of the same size reuse one texture. Returns+ * whether a live image is shown, which a browser that cannot upload `source` leaves false.+ */+ setLiveImage(+ hash: string,+ source: LiveImageSource | null,+ nodeIds: Iterable<string> = []+ ): boolean {+ const live = this.liveImages.get(hash)+ // A texture made on a surface since replaced has no context left to update or free it in.+ const current = live?.surface === this.surface ? live.image : null+ if (+ source &&+ current &&+ current.width() === source.width &&+ current.height() === source.height+ ) {+ this.surface.updateTextureFromSource(current, textureSource(source))+ } else {+ current?.delete()+ this.liveImages.delete(hash)+ const image = source && this.surface.makeImageFromTextureSource(textureSource(source))+ if (image) this.liveImages.set(hash, { image, surface: this.surface })+ }+ for (const id of nodeIds) this.invalidateNodePicture(id)+ return this.liveImages.has(hash)+ }++ /**+ * Forgets the decoded image `hash` after its bytes were replaced, such as a shader's frame+ * rendered again, and redraws whatever showed it. Previews notice new bytes themselves.+ */+ forgetImage(hash: string): void {+ const keys = Array.from(this.imageCache.entries(), ([key]) => key)+ for (const key of keys) {+ if (key === hash || key.startsWith(`${hash}:`)) this.imageCache.delete(key)+ }+ this.invalidateAllPictures()+ }+/** Drops `nodeId`'s cached drawing; `changedKeys`, when known, lets text keep its glyph coverage. */invalidateNodePicture(nodeId: string, changedKeys?: readonly (keyof SceneNode)[]): void {RendererState.invalidateNodePicture(this, nodeId, changedKeys)diff --git a/packages/core/src/canvas/renderer/lifecycle.ts b/packages/core/src/canvas/renderer/lifecycle.tsindex 9f51dc292..820b862ad 100644--- a/packages/core/src/canvas/renderer/lifecycle.ts+++ b/packages/core/src/canvas/renderer/lifecycle.ts@@ -26,6 +26,14 @@ function disposePathCaches(r: SkiaRenderer): void {r.glyphSilhouetteCache.clear()}+/** Frees every live image `r`'s surface holds and forgets those an earlier surface held. */+export function releaseLiveImages(r: Pick<SkiaRenderer, 'liveImages' | 'surface'>): void {+ for (const { image, surface } of r.liveImages.values()) {+ if (surface === r.surface) image.delete()+ }+ r.liveImages.clear()+}+export function destroyRenderer(r: SkiaRenderer): void {if (r.destroyed) returnr.destroyed = true@@ -34,6 +42,7 @@ export function destroyRenderer(r: SkiaRenderer): void {r.onImagePreviewReady = nullr.imagePreviews.destroy()r.imageCache.clear()+ releaseLiveImages(r)disposePathCaches(r)r.fillPaint.delete()r.diamondGradientEffect?.delete()diff --git a/packages/core/src/editor/create.ts b/packages/core/src/editor/create.tsindex 93b63b173..11241085c 100644--- a/packages/core/src/editor/create.ts+++ b/packages/core/src/editor/create.ts@@ -43,6 +43,8 @@ import { createNodeActions } from './nodes'import { createPageActions } from './pages'import { createPlayActions } from './play/actions'import { createSelectionActions } from './selection'+import { createShaderActions } from './shaders/actions'+import { createShaderFrames } from './shaders/frames'import { createShapeActions } from './shapes'import { createDefaultEditorState } from './state'import { createStructureActions } from './structure'@@ -224,6 +226,7 @@ export function createEditor(options?: EditorOptions) {icons: options?.icons ?? iconify,getCk: () => _ck,getRenderer: () => _renderer,+ getRenderers: () => _renderers,getTextEditor: () => _textEditor,requestRender,requestRefresh,@@ -242,7 +245,8 @@ export function createEditor(options?: EditorOptions) {// Assemble domain modulesconst viewport = createViewportActions(ctx)const selection = createSelectionActions(ctx)- const pages = createPageActions(ctx)+ const shaderFrames = createShaderFrames(ctx, options)+ const pages = createPageActions(ctx, shaderFrames)const guides = createGuideActions(ctx)const designIssues = createDesignIssueActions(ctx)const shapes = createShapeActions(ctx)@@ -257,6 +261,7 @@ export function createEditor(options?: EditorOptions) {const designTokens = createDesignTokenActions(ctx, variables, nodes)const vectorize = createVectorizeActions(ctx)const icons = createIconActions(ctx)+ const shaders = createShaderActions(ctx, nodes)const alignment = createAlignmentActions(ctx)const preview = createPlayActions(ctx)const clipboardBridge = createClipboardBridge(clipboard, selection)@@ -391,6 +396,8 @@ export function createEditor(options?: EditorOptions) {// Bitmap-to-vector replacement...vectorize,...icons,+ ...shaders,+ ...shaderFrames,// Variables...variables,diff --git a/packages/core/src/editor/index.ts b/packages/core/src/editor/index.tsindex af860a3d2..d1ba9f98b 100644--- a/packages/core/src/editor/index.ts+++ b/packages/core/src/editor/index.ts@@ -32,6 +32,8 @@ export { resolvePasteTarget } from './clipboard/paste-target'export { playIslandRoots } from './play/islands'export { resolvePlayState, type InstanceState } from './play/states'export type { PlayState } from './play/actions'+export type { ShaderRasterizer } from './shaders/types'+export type { ShaderPaintList } from './shaders/actions'export type { Editor } from './create'export type { VariableTokenFields } from './variables'export type { TokenImportResult } from '#core/io/formats/design-tokens'diff --git a/packages/core/src/editor/pages.ts b/packages/core/src/editor/pages.tsindex 21605fc3a..94d0948f3 100644--- a/packages/core/src/editor/pages.ts+++ b/packages/core/src/editor/pages.ts@@ -22,7 +22,12 @@ import { createPageViewportStore } from './page-viewports'import type { EditorContext } from './types'export interface PageSwitchProgress {- phase: 'populating-page' | 'resolving-fonts' | 'resolving-fallbacks' | 'layout'+ phase:+ | 'populating-page'+ | 'resolving-fonts'+ | 'resolving-fallbacks'+ | 'layout'+ | 'drawing-shaders'detail?: stringcompleted?: numbertotal?: number@@ -45,7 +50,15 @@ function throwIfAborted(signal?: AbortSignal): void {}const MAX_CONCURRENT_FONT_LOADS = 4-export function createPageActions(ctx: EditorContext) {+/** What preparing a page needs drawn once it is laid out, such as its shaders' frames. */+interface PageDrawing {+ drawPageShaderFrames(+ pageId: string,+ options: { signal?: AbortSignal; onProgress?: (progress: PageSwitchProgress) => void }+ ): Promise<void>+}++export function createPageActions(ctx: EditorContext, drawing?: PageDrawing) {const pageViewportStore = createPageViewportStore(ctx)function syncPageColor() {ctx.state.pageColor = getPageColor(ctx.graph.getNode(ctx.state.currentPageId))@@ -179,6 +192,10 @@ export function createPageActions(ctx: EditorContext) {ctx.graph.applyDerivedLayoutDuring(() => computeAllLayouts(ctx.graph, pageId))}throwIfAborted(options.signal)+ if (generation !== pageSwitchGeneration) return null+ // Frames are drawn at the size layout gives each layer.+ await drawing?.drawPageShaderFrames(pageId, options)+ throwIfAborted(options.signal)return generation === pageSwitchGeneration ? { pageId, generation } : null}diff --git a/packages/core/src/editor/shaders/actions.ts b/packages/core/src/editor/shaders/actions.tsnew file mode 100644index 000000000..81ce72da5--- /dev/null+++ b/packages/core/src/editor/shaders/actions.ts@@ -0,0 +1,86 @@+import { pick } from 'es-toolkit'++import {+ createShaderPaint,+ shaderOfPaint,+ withShaderPaints,+ type SceneNode,+ type ShaderPreset,+ type Stroke+} from '@open-pencil/scene-graph'++import type { EditorContext } from '#core/editor/types'++/** What a stroke has beyond its paint. */+const STROKE_FIELDS = [+ 'weight',+ 'align',+ 'cap',+ 'join',+ 'dashPattern'+] as const satisfies readonly (keyof Stroke)[]++const replaceAt = <T>(items: readonly T[], index: number, item: T) =>+ items.map((existing, at) => (at === index ? item : existing))++/** Which of a layer's paint lists a shader is set in. */+export type ShaderPaintList = 'fills' | 'strokes'++interface NodeActions {+ updateNodeWithUndo(id: string, changes: Partial<SceneNode>, label?: string): void+}++/**+ * Shader paint commands. A shader paint is an image paint of the shader's frame plus the preset+ * in plugin data, so each command records both together, as one undo step.+ */+export function createShaderActions(ctx: EditorContext, nodes: NodeActions) {+ function setNodeShader(+ nodeId: string,+ list: ShaderPaintList,+ index: number,+ preset: ShaderPreset+ ): void {+ const node = ctx.graph.getNode(nodeId)+ const previous = node?.[list].at(index)+ if (!node || !previous) return+ const existing = shaderOfPaint(node, previous)+ const { paint, shader } = existing+ ? { paint: previous, shader: { image: existing.image, preset: structuredClone(preset) } }+ : createShaderPaint(preset)+ const shown = { ...paint, opacity: previous.opacity, visible: previous.visible }+ const fills = list === 'fills' ? replaceAt(node.fills, index, shown) : node.fills+ // A stroke keeps its weight and geometry; only its paint changes.+ const strokes =+ list === 'strokes'+ ? node.strokes.map((item, at) =>+ at === index ? { ...pick(item, STROKE_FIELDS), ...shown } : item+ )+ : node.strokes+ nodes.updateNodeWithUndo(nodeId, {+ [list]: list === 'fills' ? fills : strokes,+ pluginData: withShaderPaints(node, [...fills, ...strokes], [shader])+ })+ }++ /**+ * Makes paint `index` of `list` on each of `nodeIds` draw `preset`, as one undo step: a shader+ * paint keeps its image and takes the new preset, any other paint becomes a new shader paint.+ * Frames are drawn once the edit settles.+ */+ function setShaderPaint(+ nodeIds: readonly string[],+ list: ShaderPaintList,+ index: number,+ preset: ShaderPreset+ ): void {+ ctx.undo.beginBatch('Set shader')+ try {+ for (const id of nodeIds) setNodeShader(id, list, index, preset)+ } finally {+ ctx.undo.commitBatch()+ }+ }++ return { setShaderPaint }+}diff --git a/packages/core/src/editor/shaders/frames.ts b/packages/core/src/editor/shaders/frames.tsnew file mode 100644index 000000000..111c10475--- /dev/null+++ b/packages/core/src/editor/shaders/frames.ts@@ -0,0 +1,220 @@+import { isEqual } from 'es-toolkit/predicate'++import {+ isShaderFrameCurrent,+ readShaderPaints,+ shaderOfPaint,+ shaderPresetKey,+ withShaderPaints,+ type SceneNode,+ type ShaderPaint+} from '@open-pencil/scene-graph'+import type { Size } from '@open-pencil/scene-graph/primitives'++import type { EditorContext, EditorOptions } from '#core/editor/types'++import type { ShaderRasterizer } from './types'++/** Frames are drawn at twice the layer's size, for sharp high-density displays. */+const FRAME_SCALE = 2+/** The longest side a frame is drawn at, however large the layer. */+const MAX_FRAME_EDGE = 2048+/** How long a layer must keep its size before its frames are drawn again. */+const SETTLE_MS = 150++/** How far drawing a page's shader frames has got. */+export interface ShaderFramesProgress {+ phase: 'drawing-shaders'+ completed: number+ total: number+}++/** Fields whose change can leave a shader's frame out of date. */+const FRAME_FIELDS = ['fills', 'strokes', 'pluginData', 'width', 'height'] as const++/** The pixels a layer's frame is drawn at. */+function framePixels(size: Size): Size {+ const scale = Math.min(FRAME_SCALE, MAX_FRAME_EDGE / Math.max(size.width, size.height, 1))+ return {+ width: Math.max(1, Math.round(size.width * scale)),+ height: Math.max(1, Math.round(size.height * scale))+ }+}++const frameKey = (shader: ShaderPaint, size: Size) =>+ JSON.stringify([shader.preset, Math.round(size.width), Math.round(size.height)])++/** The shaders `node`'s paints show. */+function shownShaders(node: SceneNode): ShaderPaint[] {+ return [...node.fills, ...node.strokes].flatMap((paint) => {+ const shader = shaderOfPaint(node, paint)+ return shader ? [shader] : []+ })+}++/**+ * Keeps every shader paint's still frame drawn for its preset and its layer's size. A frame is+ * redrawn when its preset changes or its layer settles at a new size, and stored under the+ * image the paint already shows, so undoing any edit keeps paint and shader together. Frames+ * are drawn one at a time, since each one takes the GPU.+ */+export function createShaderFrames(+ ctx: EditorContext,+ options?: Pick<EditorOptions, 'shaderRasterizer'>+) {+ let rasterizer: ShaderRasterizer | null = options?.shaderRasterizer ?? null+ /** What this session drew under each image, which outranks a saved frame's size. */+ const drawn = new Map<string, string>()+ const pending = new Set<string>()+ let timer: ReturnType<typeof setTimeout> | null = null+ let drawing: Promise<void> | null = null++ function isCurrent(shader: ShaderPaint, size: Size): boolean {+ const key = drawn.get(shader.image)+ if (key !== undefined) return key === frameKey(shader, size)+ return isShaderFrameCurrent(shader, size) && ctx.graph.images.has(shader.image)+ }++ function schedule(id: string) {+ if (!rasterizer) return+ pending.add(id)+ if (timer) clearTimeout(timer)+ timer = setTimeout(() => {+ timer = null+ void drain()+ }, SETTLE_MS)+ }++ /** The shaders on `pageId` whose frames are missing or out of date. */+ function staleOnPage(pageId: string): { id: string; shader: ShaderPaint }[] {+ const graph = ctx.graph+ return [...graph.getAllNodes()].flatMap((node) => {+ if (readShaderPaints(node).length === 0 || !graph.isDescendant(node.id, pageId)) return []+ const size = { width: node.width, height: node.height }+ return shownShaders(node)+ .filter((shader) => !isCurrent(shader, size))+ .map((shader) => ({ id: node.id, shader }))+ })+ }++ async function drawFrame(id: string, shader: ShaderPaint, active: ShaderRasterizer) {+ const graph = ctx.graph+ const node = graph.getNode(id)+ if (!node) return+ const size = { width: node.width, height: node.height }+ const bytes = await active.render(shader.preset, framePixels(size))+ // The preset, the layer, or the document may have changed while the frame was drawn.+ const now = graph === ctx.graph ? graph.getNode(id) : undefined+ const current = now && shownShaders(now).find((item) => item.image === shader.image)+ if (!bytes || !now || !current || !isEqual(current.preset, shader.preset)) return+ graph.images.set(shader.image, bytes)+ drawn.set(shader.image, frameKey(shader, size))+ for (const renderer of ctx.getRenderers()) renderer.forgetImage(shader.image)+ // Not an edit of its own: the frame follows the preset and size an edit set.+ graph.updateNode(id, {+ pluginData: withShaderPaints(+ now,+ [...now.fills, ...now.strokes],+ [{ ...current, frame: { ...size, preset: shaderPresetKey(current.preset) } }]+ )+ })+ ctx.requestRender()+ }++ async function drawPending() {+ while (pending.size > 0) {+ const active = rasterizer+ if (!active) return+ const [id] = pending+ pending.delete(id)+ const node = ctx.graph.getNode(id)+ if (!node) continue+ const size = { width: node.width, height: node.height }+ for (const shader of shownShaders(node)) {+ if (isCurrent(shader, size)) continue+ // A frame that fails to draw keeps the one it has, and the next edit tries again.+ await drawFrame(id, shader, active).catch((error: unknown) => {+ console.warn('Could not draw a shader frame', error)+ })+ }+ }+ }++ /** Runs `work` once no other drawing is under way, so frames draw one at a time. */+ async function exclusive(work: () => Promise<void>): Promise<void> {+ if (drawing) {+ await drawing+ return exclusive(work)+ }+ drawing = work().finally(() => {+ drawing = null+ })+ await drawing+ }++ /** Draws what is pending, after any drawing already under way. */+ async function drain(): Promise<void> {+ if (pending.size > 0) await exclusive(drawPending)+ }++ /**+ * Draws the frames `pageId` needs before it is shown, as its fonts are loaded first, so the+ * page never appears with a shader missing. Without a rasterizer the saved frames stay.+ */+ async function drawPageShaderFrames(+ pageId: string,+ options: { signal?: AbortSignal; onProgress?: (progress: ShaderFramesProgress) => void } = {}+ ): Promise<void> {+ const active = rasterizer+ const stale = active ? staleOnPage(pageId) : []+ if (!active || stale.length === 0) return+ const progress = (completed: number) =>+ options.onProgress?.({ phase: 'drawing-shaders', completed, total: stale.length })+ progress(0)+ await exclusive(async () => {+ // A switch cancelled while other frames were drawing stops before drawing its own.+ for (const [index, { id, shader }] of stale.entries()) {+ options.signal?.throwIfAborted()+ await drawFrame(id, shader, active).catch((error: unknown) => {+ console.warn('Could not draw a shader frame', error)+ })+ options.signal?.throwIfAborted()+ progress(index + 1)+ }+ })+ }++ ctx.onEditorEvent('node:created', (node) => {+ if (readShaderPaints(node).length > 0) schedule(node.id)+ })+ ctx.onEditorEvent('node:updated', (id, changes) => {+ if (!FRAME_FIELDS.some((field) => field in changes)) return+ const node = ctx.graph.getNode(id)+ if (node && readShaderPaints(node).length > 0) schedule(id)+ })+ // A new document's frames are drawn page by page as each is prepared.+ ctx.onEditorEvent('graph:replaced', () => {+ drawn.clear()+ pending.clear()+ })++ /** Draws shader frames with `next`, or stops drawing them when null. */+ function setShaderRasterizer(next: ShaderRasterizer | null) {+ if (rasterizer === next) return+ rasterizer?.destroy?.()+ rasterizer = next+ }++ const hasShaderRasterizer = () => rasterizer !== null++ /** Resolves once every frame waiting to be drawn has been drawn. */+ async function settleShaderFrames(): Promise<void> {+ if (timer) {+ clearTimeout(timer)+ timer = null+ }+ await drain()+ }++ return { setShaderRasterizer, hasShaderRasterizer, settleShaderFrames, drawPageShaderFrames }+}diff --git a/packages/core/src/editor/shaders/types.ts b/packages/core/src/editor/shaders/types.tsnew file mode 100644index 000000000..b62732d38--- /dev/null+++ b/packages/core/src/editor/shaders/types.ts@@ -0,0 +1,12 @@+import type { ShaderPreset } from '@open-pencil/scene-graph'+import type { Size } from '@open-pencil/scene-graph/primitives'++/**+ * Draws a shader's still frame. Core has no GPU or DOM, so the canvas that has them supplies+ * one; without it, or where it cannot draw, a shader paint keeps the frame it was saved with.+ */+export interface ShaderRasterizer {+ /** PNG bytes of `preset`'s first frame at `size` pixels, or null when it cannot draw here. */+ render(preset: ShaderPreset, size: Size): Promise<Uint8Array | null>+ destroy?(): void+}diff --git a/packages/core/src/editor/types.ts b/packages/core/src/editor/types.tsindex 9c898730c..486b9f5e1 100644--- a/packages/core/src/editor/types.ts+++ b/packages/core/src/editor/types.ts@@ -25,6 +25,7 @@ import type {import type { InterfaceTheme } from '#core/constants'import type { PlayState } from '#core/editor/play/actions'import type { SnappingPreferences } from '#core/editor/preferences'+import type { ShaderRasterizer } from '#core/editor/shaders/types'import type { RotationPreview, ShapeHandleKind } from '#core/geometry'import type { IconProvider } from '#core/icons/provider'import type { TextEditor } from '#core/text/editor'@@ -204,6 +205,8 @@ export interface EditorOptions {getViewportSize?: () => { width: number; height: number }/** Where icons are searched and fetched; defaults to Iconify. */icons?: IconProvider+ /** What draws shader frames, which needs a GPU; without one, saved frames stay. */+ shaderRasterizer?: ShaderRasterizer | nullskipInitialGraphSetup?: boolean}@@ -223,6 +226,8 @@ export interface EditorContext {icons: IconProvidergetCk: () => CanvasKit | nullgetRenderer: () => SkiaRenderer | null+ /** Every canvas drawing the document, such as each pane's scene and overlays. */+ getRenderers: () => Iterable<SkiaRenderer>getTextEditor: () => TextEditor | nullrequestRender: () => void/** A document change the canvas does not draw: views and saving follow, nothing is redrawn. */diff --git a/packages/core/tests/design-jsx/export-round-trip.test.ts b/packages/core/tests/design-jsx/export-round-trip.test.tsindex 1d83ac274..903820d86 100644--- a/packages/core/tests/design-jsx/export-round-trip.test.ts+++ b/packages/core/tests/design-jsx/export-round-trip.test.ts@@ -6,7 +6,7 @@ import { pick } from 'es-toolkit'import { renderJSX } from '@open-pencil/core/design-jsx'import { sceneNodeToJSX } from '@open-pencil/design-jsx'-import { SceneGraph } from '@open-pencil/scene-graph'+import { SceneGraph, shaderOfPaint } from '@open-pencil/scene-graph'describe('attribute string round-trip', () => {it.each([@@ -73,3 +73,44 @@ describe('design JSX export round trip', () => {})})++describe('shader paints', () => {+ test('a shader fill keeps its preset in the layer and exports as shader()', async () => {+ const graph = new SceneGraph()+ const [source] = await renderJSX(+ graph,+ "<Frame w={80} h={40} fills={[shader([{ type: 'Aurora', props: { speed: 2 } }], { opacity: 0.5 })]} />"+ )+ const node = getNodeOrThrow(graph, source.id)+ const [paint] = node.fills+ expect(paint).toMatchObject({ type: 'IMAGE', opacity: 0.5 })+ expect(paint).not.toHaveProperty('shader')+ expect(shaderOfPaint(node, paint)?.preset).toEqual({+ components: [{ type: 'Aurora', props: { speed: 2 } }]+ })++ const jsx = sceneNodeToJSX(source.id, graph)+ expect(jsx).toContain('shader([{ type: "Aurora", props: { speed: 2 } }], { opacity: 0.5 })')++ const [result] = await renderJSX(graph, jsx)+ const copy = getNodeOrThrow(graph, result.id)+ expect(shaderOfPaint(copy, copy.fills[0])?.preset).toEqual(shaderOfPaint(node, paint)?.preset)+ })++ test('a shader takes a preset as shaders.com exports it', async () => {+ const graph = new SceneGraph()+ const [source] = await renderJSX(+ graph,+ "<Frame w={10} h={10} bg={shader({ components: [{ type: 'Swirl' }], structureVersion: 2 })} />"+ )+ const node = getNodeOrThrow(graph, source.id)+ expect(shaderOfPaint(node, node.fills[0])?.preset.structureVersion).toBe(2)+ })++ test('a shader without effects says what it expects', async () => {+ const graph = new SceneGraph()+ await expect(renderJSX(graph, '<Frame bg={shader([])} />')).rejects.toThrow(+ /shader\(\) expects a preset/+ )+ })+})diff --git a/packages/core/tests/editor/shaders.test.ts b/packages/core/tests/editor/shaders.test.tsnew file mode 100644index 000000000..ecd7b9836--- /dev/null+++ b/packages/core/tests/editor/shaders.test.ts@@ -0,0 +1,169 @@+import { describe, expect, test } from 'bun:test'++import { createEditor } from '@open-pencil/core/editor'+import {+ createShaderPaint,+ SceneGraph,+ shaderOfPaint,+ shaderPresetKey,+ withShaderPaints,+ type ShaderPreset+} from '@open-pencil/scene-graph'++const AURORA: ShaderPreset = { components: [{ type: 'Aurora' }] }+const SWIRL: ShaderPreset = { components: [{ type: 'Swirl' }] }++function setup() {+ const graph = new SceneGraph()+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, {+ width: 40,+ height: 20,+ fills: [{ type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 1, visible: true }]+ })+ const editor = createEditor({ graph })+ const drawn: { preset: ShaderPreset; width: number; height: number }[] = []+ editor.setShaderRasterizer({+ render: (preset, size) => {+ drawn.push({ preset, ...size })+ return Promise.resolve(new Uint8Array([drawn.length]))+ }+ })+ const read = () => {+ const current = editor.graph.getNode(node.id)+ if (!current) throw new Error('missing node')+ return current+ }+ return { editor, id: node.id, drawn, read }+}++describe('shader paints', () => {+ test('a fill becomes a shader as one undo step', () => {+ const { editor, id, read } = setup()+ const before = read().fills++ editor.setShaderPaint([id], 'fills', 0, AURORA)+ const [paint] = read().fills+ expect(paint.type).toBe('IMAGE')+ expect(shaderOfPaint(read(), paint)?.preset).toEqual(AURORA)++ editor.undoAction()+ expect(read().fills).toEqual(before)+ expect(shaderOfPaint(read(), paint)).toBeNull()+ })++ test('changing the preset keeps the paint’s image', () => {+ const { editor, id, read } = setup()+ editor.setShaderPaint([id], 'fills', 0, AURORA)+ const image = read().fills[0].imageHash++ editor.setShaderPaint([id], 'fills', 0, SWIRL)++ expect(read().fills[0].imageHash).toBe(image)+ expect(shaderOfPaint(read(), read().fills[0])?.preset).toEqual(SWIRL)+ })++ test('draws the frame at twice the layer’s size under the paint’s image', async () => {+ const { editor, id, drawn, read } = setup()+ editor.setShaderPaint([id], 'fills', 0, AURORA)+ await editor.settleShaderFrames()++ const [paint] = read().fills+ expect(drawn).toEqual([{ preset: AURORA, width: 80, height: 40 }])+ expect(editor.graph.images.get(paint.imageHash ?? '')).toEqual(new Uint8Array([1]))+ expect(shaderOfPaint(read(), paint)?.frame).toEqual({+ width: 40,+ height: 20,+ preset: shaderPresetKey(AURORA)+ })++ await editor.settleShaderFrames()+ expect(drawn).toHaveLength(1)+ })++ test('draws again after a resize and after undoing a preset change', async () => {+ const { editor, id, drawn } = setup()+ editor.setShaderPaint([id], 'fills', 0, AURORA)+ await editor.settleShaderFrames()++ editor.updateNodeWithUndo(id, { width: 60 })+ await editor.settleShaderFrames()+ editor.setShaderPaint([id], 'fills', 0, SWIRL)+ await editor.settleShaderFrames()+ editor.undoAction()+ await editor.settleShaderFrames()++ expect(drawn.map(({ preset, width }) => [preset, width])).toEqual([+ [AURORA, 80],+ [AURORA, 120],+ [SWIRL, 120],+ [AURORA, 120]+ ])+ })++ test('sets a shader on several layers as one undo step, each with its own image', () => {+ const { editor, id, read } = setup()+ const other = editor.graph.createNode('RECTANGLE', editor.graph.getPages()[0].id, {+ fills: [{ type: 'SOLID', color: { r: 0, g: 0, b: 1, a: 1 }, opacity: 1, visible: true }]+ })++ editor.setShaderPaint([id, other.id], 'fills', 0, AURORA)+ const images = [read().fills[0].imageHash, editor.graph.getNode(other.id)?.fills[0].imageHash]+ expect(new Set(images).size).toBe(2)++ editor.undoAction()+ expect(read().fills[0].type).toBe('SOLID')+ expect(editor.graph.getNode(other.id)?.fills[0].type).toBe('SOLID')+ })++ test('a stroke keeps its weight when it becomes a shader', () => {+ const { editor, id, read } = setup()+ editor.updateNodeWithUndo(id, {+ strokes: [+ {+ type: 'SOLID',+ color: { r: 0, g: 0, b: 0, a: 1 },+ opacity: 1,+ visible: true,+ weight: 3,+ align: 'CENTER'+ }+ ]+ })++ editor.setShaderPaint([id], 'strokes', 0, AURORA)++ expect(read().strokes[0]).toMatchObject({ type: 'IMAGE', weight: 3, align: 'CENTER' })+ })+})++describe('shader frames on page preparation', () => {+ test('a page is prepared only once its shaders have frames, without drawing in the background', async () => {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]+ const node = graph.createNode('RECTANGLE', page.id, { width: 40, height: 20 })+ const { paint, shader } = createShaderPaint(AURORA)+ graph.updateNode(node.id, {+ fills: [paint],+ pluginData: withShaderPaints(node, [paint], [shader])+ })+ const drawn: string[] = []+ const editor = createEditor({+ shaderRasterizer: {+ render: (preset) => {+ drawn.push(preset.components[0]?.type ?? '')+ return Promise.resolve(new Uint8Array([1]))+ }+ }+ })+ editor.replaceGraph(graph)+ await editor.settleShaderFrames()+ expect(drawn).toEqual([])++ const phases: string[] = []+ await editor.preparePage(page.id, { onProgress: (progress) => phases.push(progress.phase) })++ expect(drawn).toEqual(['Aurora'])+ expect(phases).toContain('drawing-shaders')+ expect(editor.graph.images.get(shader.image)).toEqual(new Uint8Array([1]))+ })+})diff --git a/packages/core/tests/io/formats/fig/roundtrip/shaders.test.ts b/packages/core/tests/io/formats/fig/roundtrip/shaders.test.tsnew file mode 100644index 000000000..c421d18df--- /dev/null+++ b/packages/core/tests/io/formats/fig/roundtrip/shaders.test.ts@@ -0,0 +1,51 @@+import { expect, setDefaultTimeout, test } from 'bun:test'++import { expectDefined } from '#core-tests/helpers/assert'+import { collectAllNodes } from '#core-tests/helpers/fig/traversal'++import { exportFigFile, initCodec, parseFigFile, SceneGraph } from '@open-pencil/core'+import {+ createShaderPaint,+ shaderOfPaint,+ shaderPresetKey,+ withShaderPaints,+ type ShaderPreset+} from '@open-pencil/scene-graph'++setDefaultTimeout(60_000)++const PRESET: ShaderPreset = { components: [{ type: 'Aurora', props: { colorA: '#ff3300' } }] }++/**+ * A shader paint is an image paint of its frame with the preset in plugin data, so a `.fig`+ * archive keeps both: Figma draws the frame, and OpenPencil reads the shader back.+ */+test('a shader fill keeps its frame and preset through a .fig archive', async () => {+ await initCodec()+ const graph = new SceneGraph()+ const { paint, shader } = createShaderPaint(PRESET)+ const frame = { width: 120, height: 80, preset: shaderPresetKey(PRESET) }+ const png = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10])+ graph.images.set(shader.image, png)+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, {+ name: 'Shader',+ width: 120,+ height: 80,+ fills: [paint]+ })+ graph.updateNode(node.id, {+ pluginData: withShaderPaints(node, [paint], [{ ...shader, frame }])+ })++ const bytes = await exportFigFile(graph)+ const read = await parseFigFile(bytes.buffer as ArrayBuffer)+ const copy = expectDefined(+ collectAllNodes(read).find((candidate) => candidate.name === 'Shader'),+ 'shader layer'+ )+ const [fill] = copy.fills++ expect(fill).toMatchObject({ type: 'IMAGE', imageHash: shader.image })+ expect(read.images.get(shader.image)).toEqual(png)+ expect(shaderOfPaint(copy, fill)).toEqual({ ...shader, frame })+})diff --git a/packages/design-jsx/src/export/paint.ts b/packages/design-jsx/src/export/paint.tsindex 93f72f061..035e5f2b8 100644--- a/packages/design-jsx/src/export/paint.ts+++ b/packages/design-jsx/src/export/paint.ts@@ -1,4 +1,11 @@-import type { Effect, Fill, GradientTransform, SceneNode, Stroke } from '@open-pencil/scene-graph'+import {+ shaderOfPaint,+ type Effect,+ type Fill,+ type GradientTransform,+ type SceneNode,+ type Stroke+} from '@open-pencil/scene-graph'import { isIdentityGradientTransform } from '@open-pencil/scene-graph/gradient'import { formatColor, formatShadow } from './helpers'@@ -27,8 +34,22 @@ function options(entries: Options): Options | undefined {return Object.values(entries).some((value) => value !== undefined) ? entries : undefined}-function fillValue(fill: Fill): JSXValue {+function fillValue(fill: Fill, node: SceneNode): JSXValue {const visible = fill.visible ? undefined : false+ const shader = shaderOfPaint(node, fill)+ if (shader) {+ // A preset with nothing but effects is written as just its effects.+ const { components, ...rest } = shader.preset+ return helperCall(+ 'shader',+ plainValue(Object.keys(rest).length === 0 ? components : shader.preset),+ options({+ opacity: fill.opacity === 1 ? undefined : fill.opacity,+ visible,+ blendMode: blendMode(fill.blendMode)+ })+ )+ }if (fill.type === 'SOLID') {const color = formatColor(fill.color, fill.color.a)const opacity = fill.opacity === fill.color.a ? undefined : fill.opacity@@ -75,7 +96,7 @@ export function collectFillProps(node: SceneNode, props: JSXProp[]): void {if (node.fills.length === 0) returnconst shorthand = solidShorthand(node.fills)if (shorthand) props.push([node.type === 'TEXT' ? 'color' : 'bg', shorthand])- else props.push(['fills', node.fills.map(fillValue)])+ else props.push(['fills', node.fills.map((fill) => fillValue(fill, node))])}interface StrokeValue {diff --git a/packages/design-jsx/src/helpers.ts b/packages/design-jsx/src/helpers.tsindex 22de562f3..cca80ebdb 100644--- a/packages/design-jsx/src/helpers.ts+++ b/packages/design-jsx/src/helpers.ts@@ -16,6 +16,7 @@ import {gradient,linearGradient,radialGradient,+ shader,solid,type GradientPaintOptions,type SolidPaintOptions@@ -96,6 +97,7 @@ export function designJSXHelpers(warnings: string[]) {backgroundBlur: checked(warnings, 'backgroundBlur', backgroundBlur, BLUR_OPTIONS, 0),foregroundBlur: checked(warnings, 'foregroundBlur', foregroundBlur, BLUR_OPTIONS, 0),solid: checked(warnings, 'solid', solid, SOLID_OPTIONS, 1),+ shader: checked(warnings, 'shader', shader, SOLID_OPTIONS, 1),gradient: checked(warnings, 'gradient', gradient, GRADIENT_OPTIONS, 2),linearGradient: checked(warnings, 'linearGradient', linearGradient, GRADIENT_OPTIONS, 1),radialGradient: checked(warnings, 'radialGradient', radialGradient, GRADIENT_OPTIONS, 1),diff --git a/packages/design-jsx/src/index.ts b/packages/design-jsx/src/index.tsindex 2f901b824..ed5bd5ebd 100644--- a/packages/design-jsx/src/index.ts+++ b/packages/design-jsx/src/index.ts@@ -68,10 +68,12 @@ export {gradient,linearGradient,radialGradient,+ shader,solid,type GradientPaintOptions,type PaintColor,type PaintStop,+ type ShaderFill,type SolidPaintOptions} from './paints'diff --git a/packages/design-jsx/src/paints.ts b/packages/design-jsx/src/paints.tsindex e93f54b95..0e81b10fd 100644--- a/packages/design-jsx/src/paints.ts+++ b/packages/design-jsx/src/paints.ts@@ -1,11 +1,15 @@import * as v from 'valibot'-import type {- BlendMode,- Fill,- FillType,- GradientStop,- GradientTransform+import {+ createShaderPaint,+ shaderPresetSchema,+ type BlendMode,+ type Fill,+ type FillType,+ type GradientStop,+ type GradientTransform,+ type ShaderComponent,+ type ShaderPreset} from '@open-pencil/scene-graph'import { colorToFill, parseColor } from '@open-pencil/scene-graph/color'import { TRANSPARENT } from '@open-pencil/scene-graph/constants'@@ -96,6 +100,52 @@ export function gradient(}}+/** A paint that draws a shader, carrying its preset until the layer it fills stores it. */+export interface ShaderFill extends Fill {+ shader: ShaderPreset+}++/** Whether a paint value came from {@link shader}. */+export function isShaderFill(value: Fill): value is ShaderFill {+ return 'shader' in value+}++const shaderInput = v.union(+ [+ shaderPresetSchema,+ v.pipe(+ v.array(v.unknown()),+ v.transform((components) => ({ components })),+ shaderPresetSchema+ )+ ],+ (issue) =>+ `Expected a preset { components: [...] } or an array of effects but received ${issue.received}`+)++/**+ * A paint that draws a shader from the `shaders` library: a preset as shaders.com exports it, or+ * just its effects, such as `shader([{ type: 'Aurora' }])`.+ */+export function shader(+ preset: ShaderPreset | ShaderComponent[],+ options: SolidPaintOptions = {}+): ShaderFill {+ const parsed = parseScriptInput(+ "shader() expects a preset such as { components: [{ type: 'Aurora' }] } or an array of effects",+ shaderInput,+ preset+ )+ const { paint } = createShaderPaint(parsed)+ return {+ ...paint,+ opacity: options.opacity ?? 1,+ visible: options.visible ?? true,+ blendMode: options.blendMode,+ shader: parsed+ }+}+export function linearGradient(stops: PaintStop[], options?: GradientPaintOptions): Fill {return gradient('GRADIENT_LINEAR', stops, options)}diff --git a/packages/design-jsx/src/props-overrides.ts b/packages/design-jsx/src/props-overrides.tsindex 7a6e9ac3a..ae2ac2fa5 100644--- a/packages/design-jsx/src/props-overrides.ts+++ b/packages/design-jsx/src/props-overrides.ts@@ -4,8 +4,10 @@ import {parseAutoLayoutDirection,parseCounterAxisAlign,parsePrimaryAxisAlign,+ withShaderPaints,type Fill,- type SceneNode+ type SceneNode,+ type ShaderPaint} from '@open-pencil/scene-graph'import { colorToFill } from '@open-pencil/scene-graph/color'import { parseCSSGridTracks, parseCSSNumber } from '@open-pencil/scene-graph/css'@@ -14,6 +16,7 @@ import type { Color, JSONObject } from '@open-pencil/scene-graph/primitives'import { applyEffectOverrides } from './overrides/effects'import { applyStateOverrides } from './overrides/state'import { applyStrokeOverrides } from './overrides/strokes'+import { isShaderFill } from './paints'import { DESIGN_JSX_STYLE_KEYS, designJSXProp } from './schema'const WEIGHT_MAP: Record<string, number> = {@@ -128,6 +131,25 @@ function fillFromValue(value: string | Color | Fill): Fill {return isFill(value) ? structuredClone(value) : colorToFill(value)}+/**+ * Sets the layer's fills, moving each shader paint's preset into the layer's plugin data,+ * where a shader paint keeps it.+ */+function setFills(values: (string | Color | Fill)[], o: Partial<SceneNode>): void {+ const shaders: ShaderPaint[] = []+ const fills = values.map((value) => {+ const fill = fillFromValue(value)+ if (!isShaderFill(fill)) return fill+ const { shader, ...paint } = fill+ if (paint.imageHash) shaders.push({ image: paint.imageHash, preset: shader })+ return paint+ })+ if (fills.length === 0) return+ o.fills = fills+ if (shaders.length > 0)+ o.pluginData = withShaderPaints({ pluginData: o.pluginData ?? [] }, fills, shaders)+}+function isColor(value: unknown): value is Color {return (value !== null &&@@ -141,13 +163,12 @@ function isColor(value: unknown): value is Color {function applyFillOverride(props: Record<string, unknown>, o: Partial<SceneNode>): void {if (Array.isArray(props.fills)) {- const fills = props.fills.filter(isFillValue).map(fillFromValue)- if (fills.length > 0) o.fills = fills+ setFills(props.fills.filter(isFillValue), o)return}const bg = designJSXProp(props, 'bg')- if (isFillValue(bg)) o.fills = [fillFromValue(bg)]+ if (isFillValue(bg)) setFills([bg], o)}function applyCornerOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {diff --git a/packages/design-jsx/src/reference/authoring.md b/packages/design-jsx/src/reference/authoring.mdindex f1b256ff7..8c5632968 100644--- a/packages/design-jsx/src/reference/authoring.md+++ b/packages/design-jsx/src/reference/authoring.md@@ -18,6 +18,7 @@ This reference describes scene creation, not React DOM output. Use the `render`## Paint, text, and artwork- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`. Without a `transform` a linear gradient runs left to right and the others are centred; `transform` is Figma's `gradientTransform`, mapping the layer's unit square onto gradient space.+- `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. The canvas shows the shader's first frame, preview plays it, and files and other formats keep that frame as an image.- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow` takes a CSS `box-shadow` list, such as `shadow="0 4 8 #0002, inset 0 1 0 #fff"`, and `blur` a layer blur radius; both are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning.diff --git a/packages/design-jsx/src/render.ts b/packages/design-jsx/src/render.tsindex e97fea5b0..601ba922b 100644--- a/packages/design-jsx/src/render.ts+++ b/packages/design-jsx/src/render.ts@@ -81,6 +81,7 @@ export function buildComponent(jsxString: string, warnings: string[] = []): Reacconst backgroundBlur = __helpers.backgroundBlurconst foregroundBlur = __helpers.foregroundBlurconst solid = __helpers.solid+ const shader = __helpers.shaderconst gradient = __helpers.gradientconst linearGradient = __helpers.linearGradientconst radialGradient = __helpers.radialGradientdiff --git a/packages/design-jsx/src/schema.ts b/packages/design-jsx/src/schema.tsindex 7334f8d90..b1f43e46f 100644--- a/packages/design-jsx/src/schema.ts+++ b/packages/design-jsx/src/schema.ts@@ -258,6 +258,7 @@ export const DESIGN_JSX_HELPERS: DesignJSXHelperDefinition[] = ['radialGradient','angularGradient','diamondGradient',+ 'shader','dropShadow','innerShadow','layerBlur',diff --git a/packages/docs/programmable/cli/exporting.md b/packages/docs/programmable/cli/exporting.mdindex 8e8730440..4f3793918 100644--- a/packages/docs/programmable/cli/exporting.md+++ b/packages/docs/programmable/cli/exporting.md@@ -199,7 +199,9 @@ Tabs take the open tab as `value` (`v-model:value`, or `defaultValue` and `onValInside a generated component, an instance of another component generated in the same export uses that component rather than drawing its layers: `<Switch>` imported from `./Switch.vue` or `./Switch`, set to the variant, text, and `disabled` values the instance shows. A control the design draws on, such as a checked switch, starts on and stays operable: a local `ref` bound with `v-model` in Vue, `defaultChecked` in React. The parent only places it, so the control keeps its own look and state styles. Only an instance that draws exactly as its variant, apart from its property values and where its parent places it, uses the component; one with edited text no property sets, a fill of its own, or other changes draws its own layers, so the story keeps what the design shows. Instances of components not exported, or exported only as static stories, are drawn as before. A variant property whose default the document leaves empty starts at the default variant, the one in the set's top-left corner, which opens its stories. An icon placed from a set, unless its paths were edited, is Iconify's component, `<Icon icon="lucide:bell">` from `@iconify/vue` or `@iconify/react`, which loads the icon when shown.-Its stories render the component, and a play function clicks the control and checks its state, so the Storybook project needs `reka-ui` or `radix-ui` installed, and `@iconify/vue` or `@iconify/react` when components hold icons.+A layer filled with a shader plays it behind its content with the `shaders` library's `<Shader>` from `shaders/vue` or `shaders/react`, each effect a child with the preset's props and telemetry off (`disableTelemetry`); browsers without WebGPU show nothing in its place.++Its stories render the component, and a play function clicks the control and checks its state, so the Storybook project needs `reka-ui` or `radix-ui` installed, and `@iconify/vue` or `@iconify/react` when components hold icons, and `shaders` when layers fill with shaders.Stories carry `parameters.design` entries for [`@storybook/addon-designs`](https://github.com/storybookjs/addon-designs):diff --git a/packages/docs/reference/design-authoring.md b/packages/docs/reference/design-authoring.mdindex c9197f51f..2d4945160 100644--- a/packages/docs/reference/design-authoring.md+++ b/packages/docs/reference/design-authoring.md@@ -20,6 +20,7 @@ This reference describes scene creation, not React DOM output. Use the `render`## Paint, text, and artwork- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`. Without a `transform` a linear gradient runs left to right and the others are centred; `transform` is Figma's `gradientTransform`, mapping the layer's unit square onto gradient space.+- `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. The canvas shows the shader's first frame, preview plays it, and files and other formats keep that frame as an image.- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow` takes a CSS `box-shadow` list, such as `shadow="0 4 8 #0002, inset 0 1 0 #fff"`, and `blur` a layer blur radius; both are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning.@@ -88,6 +89,6 @@ Generated from the renderer metadata. This inventory lists accepted names, not a**Elements:** `Frame`, `Text`, `Rectangle`, `Ellipse`, `Line`, `Star`, `Polygon`, `Vector`, `Group`, `Section`, `Component`, `ComponentSet`, `Instance`, `View`, `Rect`, `Icon`, `Button.Root`, `Toggle.Root`, `Switch.Root`, `Switch.Thumb`, `Checkbox.Root`, `Checkbox.Indicator`, `RadioGroup.Root`, `RadioGroup.Item`, `RadioGroup.Indicator`, `ToggleGroup.Root`, `ToggleGroup.Item`, `Slider.Root`, `Slider.Track`, `Slider.Range`, `Slider.Thumb`, `Progress.Root`, `Progress.Track`, `Progress.Indicator`, `Tabs.Root`, `Tabs.List`, `Tabs.Trigger`, `Tabs.Content`, `Collapsible.Root`, `Collapsible.Trigger`, `Collapsible.Content`, `Accordion.Root`, `Accordion.Item`, `Accordion.Header`, `Accordion.Trigger`, `Accordion.Content`, `NumberField.Root`, `NumberField.Input`, `NumberField.Increment`, `NumberField.Decrement`, `TextField.Root`, `TextField.Input`, `Textarea.Root`, `Textarea.Input`.-**Helpers:** `solid`, `gradient`, `linearGradient`, `radialGradient`, `angularGradient`, `diamondGradient`, `dropShadow`, `innerShadow`, `layerBlur`, `backgroundBlur`, `foregroundBlur`, `designVar`, `defineVars`.+**Helpers:** `solid`, `gradient`, `linearGradient`, `radialGradient`, `angularGradient`, `diamondGradient`, `shader`, `dropShadow`, `innerShadow`, `layerBlur`, `backgroundBlur`, `foregroundBlur`, `designVar`, `defineVars`.**Properties:** `name`, `key`, `flex`, `flow`, `dir`, `gap`, `wrap`, `rowGap`, `columnGap`, `justify`, `justifyContent`, `items`, `align`, `alignItems`, `grow`, `w`, `h`, `width`, `height`, `minW`, `maxW`, `minH`, `maxH`, `x`, `y`, `top`, `left`, `position`, `constraints`, `p`, `padding`, `px`, `py`, `pt`, `pr`, `pb`, `pl`, `bg`, `fill`, `fills`, `background`, `backgroundColor`, `stroke`, `border`, `borderColor`, `strokeWidth`, `borderWidth`, `strokeAlign`, `strokeDash`, `strokeCap`, `strokeJoin`, `dashPattern`, `strokes`, `strokeWeights`, `rounded`, `borderRadius`, `roundedTL`, `roundedTR`, `roundedBL`, `roundedBR`, `cornerRadius`, `cornerSmoothing`, `opacity`, `blendMode`, `rotate`, `rotation`, `overflow`, `mask`, `visible`, `locked`, `shadow`, `blur`, `effects`, `size`, `fontSize`, `font`, `fontFamily`, `weight`, `fontWeight`, `italic`, `color`, `text`, `characters`, `content`, `value`, `title`, `textAlign`, `textAlignHorizontal`, `textHorizontalAlignment`, `textAlignVertical`, `textVerticalAlignment`, `textAutoResize`, `lineHeight`, `letterSpacing`, `textDecoration`, `textCase`, `maxLines`, `truncate`, `grid`, `columns`, `rows`, `colStart`, `rowStart`, `col`, `row`, `colSpan`, `rowSpan`, `points`, `pointCount`, `innerRadius`, `label`, `style`, `bind`, `component`, `componentId`, `properties`, `propertyRefs`, `of`, `modelValue`, `open`, `disabled`, `filled`, `states`, `min`, `max`, `step`, `defaultValue`.\ No newline at end of filediff --git a/packages/dom-css/package.json b/packages/dom-css/package.jsonindex ba51f43ae..f0c865ee0 100644--- a/packages/dom-css/package.json+++ b/packages/dom-css/package.json@@ -89,6 +89,7 @@"react": "^19.3.0","react-dom": "^19.3.0","reka-ui": "^2.10.5",+ "shaders": "4.0.4","storybook": "^10.6.1","tsdown": "^0.22.14","typescript": "~5.8.3",diff --git a/packages/dom-css/src/export/components/model.ts b/packages/dom-css/src/export/components/model.tsindex fc532e067..c7dfa7ab3 100644--- a/packages/dom-css/src/export/components/model.ts+++ b/packages/dom-css/src/export/components/model.ts@@ -17,7 +17,8 @@ import {variantDefaultValue,type Behaviour,type SceneGraph,- type SceneNode+ type SceneNode,+ type ShaderPreset} from '@open-pencil/scene-graph'import type { SceneGraphToDesignOptions } from '../projection'@@ -56,6 +57,7 @@ import {type UsedLayer} from './references'import { activeTriggers, tabParts, type ChoiceModel, type RepeatedPart } from './repeats'+import { shaderLayer, shaderLayers, type ShaderLayer } from './shaders'/** Kinds generated as components so far; the rest keep static stories. */export const GENERATED_KINDS = [@@ -123,6 +125,8 @@ export interface ComponentElement {slot?: string/** The value a repeated part stands for, such as a tab trigger's tab. */value?: string+ /** The shader the layer fills with, which plays behind its content. */+ shader?: ShaderLayerchildren: ComponentNode[]}@@ -201,6 +205,8 @@ interface TreeLabels {shownBy: Map<StateElement, string>/** The slot prop each slot frame shows. */slotOf: Map<StateElement, string>+ /** The shader each layer fills with. */+ shaders: Map<StateElement, ShaderPreset>}function componentTree(@@ -211,16 +217,18 @@ function componentTree(const repeated = labels.repeated.get(node)const part = repeated?.part ?? labels.parts.get(node) ?? nullconst text = labels.texts.get(node)+ const className = labels.classes.get(node) ?? ''return {type: 'element',part,...(repeated ? { value: repeated.value } : {}),tag: node.tagName,- className: labels.classes.get(node) ?? '',+ className,attrs: node.attrs,bindings: part === 'root' ? bindings : [],shownBy: labels.shownBy.get(node),slot: labels.slotOf.get(node),+ shader: shaderLayer(node, className, labels.shaders.get(node)),// A bound text layer draws its prop in place of the design's words and their runs.children: text? [{ type: 'textProp', name: text }]@@ -554,7 +562,8 @@ export function componentModel(repeated,input,shownBy,- slotOf+ slotOf,+ shaders: shaderLayers(graph, styles.restId, elements)},rootBindings(model, !!options.itemOf, disabled, props)),diff --git a/packages/dom-css/src/export/components/react/index.ts b/packages/dom-css/src/export/components/react/index.tsindex 2c3a46b45..42e3961b6 100644--- a/packages/dom-css/src/export/components/react/index.ts+++ b/packages/dom-css/src/export/components/react/index.ts@@ -1,10 +1,12 @@import { HEADING_RESET } from '#dom-css/behaviours/reset'import { stateStylesToCSS } from '#dom-css/behaviours/states/css'import dedent from 'dedent'+import { compact } from 'es-toolkit/array'import { omit } from 'es-toolkit/object'import { camelCase, upperFirst } from 'es-toolkit/string'import { es, jsx } from '@open-pencil/emit'+import type { ShaderComponent } from '@open-pencil/scene-graph'import type {ComponentElement,@@ -15,6 +17,15 @@ import type {GeneratedKind} from '../model'import type { ComponentReference } from '../references'+import {+ drawnEffects,+ SHADER_CANVAS,+ shaderCSS,+ shaderEffectAttributes,+ shaderEffectName,+ shaderImport,+ type ShaderLayer+} from '../shaders'import {INPUT_PROPS,inputElement,@@ -219,6 +230,37 @@ function bindingAttributes(})}+/** A shader playing behind its layer's content, with telemetry off, as every export has it. */+function shaderCanvas(shader: ShaderLayer, uses: MarkupUses, depth: number): es.SyntaxNode {+ const effect =+ (at: number) =>+ (component: ShaderComponent): es.SyntaxNode => {+ uses.shaders.add(component.type)+ const children = drawnEffects(component.children).map(effect(at + 1))+ return jsx.element(+ shaderEffectName(component.type),+ shaderEffectAttributes(component),+ children,+ at+ )+ }+ return jsx.element(+ SHADER_CANVAS,+ [+ jsx.attribute('className', jsx.container(moduleClass(shader.className))),+ jsx.attribute('disableTelemetry', null)+ ],+ drawnEffects(shader.preset.components).map(effect(depth + 1)),+ depth+ )+}++/** Words alone stay on the element's line, where a line break would add a space. */+function wordsOnly(node: ComponentElement): boolean {+ const only = node.children.length === 1 ? node.children[0]?.type : undefined+ return !node.slot && !node.shader && (only === 'text' || only === 'textProp')+}+function designElement(node: ComponentElement, uses: MarkupUses, depth: number): es.SyntaxNode {const kind = uses.kindconst root = node.part === 'root'@@ -237,17 +279,16 @@ function designElement(node: ComponentElement, uses: MarkupUses, depth: number):jsx.attribute('className', jsx.container(classNameOf(node, root))),...bindingAttributes(node, kind, native)]- // Words alone stay on the element's line, where a line break would add a space.- const only = node.children.length === 1 ? node.children[0]?.type : undefined- const inline = !node.slot && (only === 'text' || only === 'textProp')+ const inline = wordsOnly(node)// Radix puts an accordion item's trigger in a header, which carries the heading level.const header = kind === 'accordionItem' && node.part === 'trigger'const childDepth = depth + (header ? 2 : 1)- const children = node.children.map((child) => element(child, uses, childDepth))+ const content = node.children.map((child) => element(child, uses, childDepth))+ const shader = node.shader ? [shaderCanvas(node.shader, uses, childDepth)] : []const drawn = jsx.element(tagOf(node, kind),attributes,- node.slot ? [slotContent(node.slot, children, childDepth)] : children,+ [...shader, ...(node.slot ? [slotContent(node.slot, content, childDepth)] : content)],depth + (header ? 1 : 0),inline)@@ -450,11 +491,13 @@ export const reactComponent: ComponentGenerator = async (component) => {kind: component.kind,choice: component.choice,components: new Set(),- icons: false+ icons: false,+ shaders: new Set()}const body = element(component.tree, uses, 1)const imports = [...(uses.icons ? ICONIFY_IMPORT : []),+ ...(uses.shaders.size > 0 ? [shaderImport('shaders/react', uses.shaders)] : []),...(radix? es.fill(RADIX_IMPORT, {$Namespace: es.identifier(radix.namespace),@@ -499,7 +542,7 @@ export const reactComponent: ComponentGenerator = async (component) => {files: [...(item?.files ?? []),{ path: `${component.name}.tsx`, content: `${jsx.printModule(program)}\n` },- { path: stylesPath, content: css }+ { path: stylesPath, content: compact([css, shaderCSS(component.tree)]).join('\n') }],entry: { path: `./${component.name}`, named: true },...valueArg(component.kind, model)diff --git a/packages/dom-css/src/export/components/react/shared.ts b/packages/dom-css/src/export/components/react/shared.tsindex cd6133260..d483e138d 100644--- a/packages/dom-css/src/export/components/react/shared.ts+++ b/packages/dom-css/src/export/components/react/shared.ts@@ -23,6 +23,8 @@ export interface MarkupUses {/** Other generated components. */components: Set<string>icons: boolean+ /** Effects of the `shaders` library its layers fill with. */+ shaders: Set<string>}export const numeric = (name: string, value: number) =>diff --git a/packages/dom-css/src/export/components/shaders.ts b/packages/dom-css/src/export/components/shaders.tsnew file mode 100644index 000000000..ae5de7322--- /dev/null+++ b/packages/dom-css/src/export/components/shaders.ts@@ -0,0 +1,123 @@+import type { StateElement } from '#dom-css/behaviours/states/types'++import { es, jsx } from '@open-pencil/emit'+import {+ findLayerByPath,+ shaderOfPaint,+ type SceneGraph,+ type ShaderComponent,+ type ShaderPreset+} from '@open-pencil/scene-graph'++import { isPositioned } from '../projection'+import type { ComponentElement, ComponentNode } from './model'++/** A shader a layer fills with, drawn by the `shaders` library behind the layer's content. */+export interface ShaderLayer {+ preset: ShaderPreset+ /** The readable class the shader's canvas is styled by. */+ className: string+ /** Whether the layer is positioned already, so the shader can be placed inside it. */+ positioned: boolean+}++export function shaderLayer(+ node: StateElement,+ className: string,+ preset: ShaderPreset | undefined+): ShaderLayer | undefined {+ if (!preset) return undefined+ const position = node.base.position+ return {+ preset,+ className: `${className}-shader`,+ positioned: isPositioned(position)+ }+}++/**+ * The shader each element's layer fills with in the rest variant: the topmost visible shader+ * fill, which the component plays behind the layer's content.+ */+export function shaderLayers(+ graph: SceneGraph,+ restId: string,+ elements: readonly StateElement[]+): Map<StateElement, ShaderPreset> {+ const shaders = new Map<StateElement, ShaderPreset>()+ for (const element of elements) {+ const layer = findLayerByPath(graph, restId, element.key.split('\0')[0] ?? '')+ if (!layer) continue+ const shader = layer.fills+ .filter((fill) => fill.visible)+ .map((fill) => shaderOfPaint(layer, fill))+ .findLast((found) => found !== null)+ if (shader) shaders.set(element, shader.preset)+ }+ return shaders+}++/** Whether an effect's name can be imported, as every effect the library has can. */+const isEffectName = (type: string) => /^[A-Za-z_$][\w$]*$/.test(type)++/** The effects of `components` a generated component draws: those it can import by name. */+export const drawnEffects = (components: readonly ShaderComponent[] | undefined) =>+ (components ?? []).filter((component) => isEffectName(component.type))++/**+ * `import { Shader as ShaderCanvas, Aurora as ShaderAurora } from 'shaders/vue'`: the canvas and+ * effects a component draws, under names apart from its own.+ */+export function shaderImport(source: 'shaders/vue' | 'shaders/react', effects: Iterable<string>) {+ const names = ['Shader', ...[...effects].sort()]+ return {+ type: 'ImportDeclaration',+ importKind: 'value',+ specifiers: names.map((name) => ({+ type: 'ImportSpecifier',+ imported: es.identifier(name),+ local: es.identifier(name === 'Shader' ? SHADER_CANVAS : shaderEffectName(name))+ })),+ source: es.string(source)+ }+}++/** The local name a generated component imports effect `type` as, apart from its own names. */+export const shaderEffectName = (type: string) => `Shader${type}`++/** An effect's props as JSX attributes: strings as they are, anything else as JSON. */+export function shaderEffectAttributes(component: ShaderComponent): es.SyntaxNode[] {+ return Object.entries(component.props ?? {}).map(([key, value]) =>+ jsx.attribute(+ key,+ typeof value === 'string' ? jsx.stringValue(value) : jsx.container(es.json(value))+ )+ )+}++/** The local name of the library's `Shader` canvas. */+export const SHADER_CANVAS = 'ShaderCanvas'++/** The elements of a component's tree that fill with a shader. */+function shaderElements(node: ComponentNode): ComponentElement[] {+ if (node.type !== 'element') return []+ return [...(node.shader ? [node] : []), ...node.children.flatMap(shaderElements)]+}++/**+ * The style that puts each shader behind its layer's content: the layer becomes its own+ * stacking context, positioned if it is not already, and the shader fills it beneath+ * everything it holds, clipped to its corners.+ */+export function shaderCSS(tree: ComponentElement): string {+ return shaderElements(tree)+ .flatMap(({ className, shader }) =>+ shader+ ? [+ `.${className} { isolation: isolate;${shader.positioned ? '' : ' position: relative;'} }`,+ `.${shader.className} { position: absolute; inset: 0; z-index: -1; border-radius: inherit; overflow: hidden; pointer-events: none; }`+ ]+ : []+ )+ .join('\n')+}diff --git a/packages/dom-css/src/export/components/vue.ts b/packages/dom-css/src/export/components/vue.tsindex 9b87ca6b8..ca70821da 100644--- a/packages/dom-css/src/export/components/vue.ts+++ b/packages/dom-css/src/export/components/vue.ts@@ -3,9 +3,10 @@ import { stateStylesToCSS } from '#dom-css/behaviours/states/css'import dedent from 'dedent'import { compact } from 'es-toolkit/array'import { omit } from 'es-toolkit/object'-import { camelCase, upperFirst } from 'es-toolkit/string'+import { camelCase, kebabCase, upperFirst } from 'es-toolkit/string'import { es, vue } from '@open-pencil/emit'+import type { ShaderComponent } from '@open-pencil/scene-graph'import { claimName } from '../storybook/names'import { progressWidth } from './fields'@@ -18,6 +19,14 @@ import type {GeneratedKind} from './model'import type { ComponentReference } from './references'+import {+ drawnEffects,+ SHADER_CANVAS,+ shaderCSS,+ shaderEffectName,+ shaderImport,+ type ShaderLayer+} from './shaders'/** The Reka UI components a kind renders, by part, and the prop its model binds. */const REKA: Record<@@ -157,6 +166,8 @@ interface TemplateUses {/** Other generated components. */components: Set<string>icons: boolean+ /** Effects of the `shaders` library its layers fill with. */+ shaders: Set<string>/** A ref per nested control drawn on, which its `v-model` binds. */models: { name: string }[]taken: Set<string>@@ -204,17 +215,44 @@ function elementAttributes(]}+/** A shader playing behind its layer's content, with telemetry off, as every export has it. */+function shaderCanvas(shader: ShaderLayer, uses: TemplateUses): vue.VueNode {+ const effect = (component: ShaderComponent): vue.VueNode => {+ uses.shaders.add(component.type)+ return vue.element(+ shaderEffectName(component.type),+ Object.entries(component.props ?? {}).map(([key, value]) =>+ typeof value === 'string'+ ? vue.attribute(kebabCase(key), value)+ : vue.bound(kebabCase(key), es.json(value))+ ),+ drawnEffects(component.children).map(effect)+ )+ }+ return vue.element(+ SHADER_CANVAS,+ [vue.attribute('class', shader.className), vue.bound('disable-telemetry', es.json(true))],+ drawnEffects(shader.preset.components).map(effect)+ )+}+function designElement(node: ComponentElement, uses: TemplateUses): vue.VueNode {const reka = node.part ? REKA[uses.kind].parts[node.part] : undefinedif (reka) uses.reka.add(reka)const native = node.part === 'root' && !rekaconst button = native && uses.kind === 'button'- const children = node.children.map((child) => templateNode(child, uses))+ const content = node.children.map((child) => templateNode(child, uses))+ const children = node.shader ? [shaderCanvas(node.shader, uses), ...content] : contentconst element = vue.element(reka ?? (button ? 'button' : node.tag),elementAttributes(node, uses, { native, button }),// A slot frame shows what the caller passes, or the design's content.- node.slot ? [vue.element('slot', [vue.attribute('name', node.slot)], children)] : children+ node.slot+ ? [+ ...(node.shader ? [shaderCanvas(node.shader, uses)] : []),+ vue.element('slot', [vue.attribute('name', node.slot)], content)+ ]+ : children)// Reka puts an accordion item's trigger in a header, which carries the heading level.if (uses.kind !== 'accordionItem' || node.part !== 'trigger') return element@@ -319,6 +357,7 @@ function scriptImports(uses: TemplateUses): es.SyntaxNode[] {...(helpers.length > 0 ? [namedImport(helpers, 'vue')] : []),...(uses.icons ? [namedImport(['Icon'], '@iconify/vue', () => ICONIFY)] : []),...(uses.reka.size > 0 ? [namedImport([...uses.reka].sort(), 'reka-ui')] : []),+ ...(uses.shaders.size > 0 ? [shaderImport('shaders/vue', uses.shaders)] : []),...[...uses.components].sort().flatMap((name) =>es.fill(IMPORT_COMPONENT, {@@ -388,6 +427,7 @@ export const vueComponent: ComponentGenerator = async (component) => {reka: new Set(),components: new Set(),icons: false,+ shaders: new Set(),models: [],// Refs share the script with the component's own props and model.taken@@ -402,7 +442,11 @@ export const vueComponent: ComponentGenerator = async (component) => {...(item?.files ?? []),{path,- content: vue.printComponent({ script: script(component, uses), template, style: css })+ content: vue.printComponent({+ script: script(component, uses),+ template,+ style: compact([css, shaderCSS(component.tree)]).join('\n')+ })}],entry: { path: `./${path}`, named: false },diff --git a/packages/dom-css/src/export/html.ts b/packages/dom-css/src/export/html.tsindex 0025502be..3d6c09d00 100644--- a/packages/dom-css/src/export/html.ts+++ b/packages/dom-css/src/export/html.ts@@ -99,12 +99,22 @@ function serializeAttrs(node: DesignElement, options: SerializeHTMLOptions): strreturn ` ${serialized.join(' ')}`}+/** Static HTML cannot play a shader, so its layer shows the still frame behind its content. */+const SHADER_FRAME_STYLE =+ 'position:absolute;inset:0;width:100%;height:100%;object-fit:cover;z-index:-1;border-radius:inherit;pointer-events:none'++function shaderFrame(node: DesignElement): string {+ const frame = node.shader?.frame+ return frame ? `<img src="${escapeAttr(frame)}" alt="" style="${SHADER_FRAME_STYLE}">` : ''+}+function serializeElement(node: DesignElement, options: SerializeHTMLOptions): string {// SVG names such as `linearGradient` keep their case, as an SVG parser needs them.const tagName = node.tagNameconst attrs = serializeAttrs(node, options)if (VOID_ELEMENTS.has(tagName.toLowerCase())) return `<${tagName}${attrs}>`- return `<${tagName}${attrs}>${node.children.map((child) => serializeNode(child, options)).join('')}</${tagName}>`+ const children = node.children.map((child) => serializeNode(child, options)).join('')+ return `<${tagName}${attrs}>${shaderFrame(node)}${children}</${tagName}>`}export function serializeNode(node: DesignNode, options: SerializeHTMLOptions = {}): string {diff --git a/packages/dom-css/src/export/projection.ts b/packages/dom-css/src/export/projection.tsindex 2e739c42d..11e6aeda2 100644--- a/packages/dom-css/src/export/projection.ts+++ b/packages/dom-css/src/export/projection.ts@@ -3,6 +3,7 @@ import { fromUint8Array } from 'js-base64'import {layoutSizingInParent,readIcon,+ shaderOfPaint,type SceneGraph,type SceneNode} from '@open-pencil/scene-graph'@@ -239,9 +240,35 @@ function addFlexGap(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSstyle['column-gap'] = counter}+/**+ * The shader a layer's fill draws, which the projection keeps as the shader rather than the+ * image of its still frame: the shader of its first fill, as the projection reads one fill.+ */+function layerShader(graph: SceneGraph, node: SceneNode): DesignElement['shader'] {+ const fill = node.fills.at(0)+ const shader = fill?.visible ? shaderOfPaint(node, fill) : null+ if (!shader) return undefined+ const bytes = graph.images.get(shader.image)+ const frame = bytes ? `data:image/png;base64,${fromUint8Array(bytes)}` : undefined+ return { preset: shader.preset, frame }+}++const POSITIONED = new Set(['relative', 'absolute', 'fixed', 'sticky'])++/** Whether a `position` value makes an element the containing block of what it places. */+export function isPositioned(position: unknown): boolean {+ return typeof position === 'string' && POSITIONED.has(position)+}++/** A shader layer contains what plays behind its content, beneath everything it holds. */+function addShaderLayerStyle(style: DesignStyleDeclaration): void {+ if (!isPositioned(style.position)) style.position = 'relative'+ style.isolation = 'isolate'+}+function addImageStyle(style: DesignStyleDeclaration, node: SceneNode): void {const fill = node.fills.at(0)- if (fill?.type !== 'IMAGE' || !fill.visible) return+ if (fill?.type !== 'IMAGE' || !fill.visible || shaderOfPaint(node, fill)) returnif (node.width > 0 && node.height > 0) style['aspect-ratio'] = `${node.width} / ${node.height}`if (fill.imageScaleMode === 'FIT') style['object-fit'] = 'contain'if (fill.imageScaleMode === 'FILL') style['object-fit'] = 'cover'@@ -368,7 +395,7 @@ function attrsForNode(const sourceURL = imageSourceURL(node)if (sourceURL) attrs.src = sourceURLconst fill = node.fills.at(0)- if (fill?.type !== 'IMAGE' || !fill.imageHash) return attrs+ if (fill?.type !== 'IMAGE' || !fill.imageHash || shaderOfPaint(node, fill)) return attrsconst bytes = graph.images.get(fill.imageHash)if (!bytes) return attrsreturn { ...attrs, src: `data:image/png;base64,${fromUint8Array(bytes)}` }@@ -377,7 +404,8 @@ function attrsForNode(function tagNameForNode(node: SceneNode): string {if (node.type === 'SECTION') return 'section'const fill = node.fills.at(0)- if ((fill?.type === 'IMAGE' || imageSourceURL(node)) && node.childIds.length === 0) return 'img'+ const image = fill?.type === 'IMAGE' && !shaderOfPaint(node, fill)+ if ((image || imageSourceURL(node)) && node.childIds.length === 0) return 'img'return 'div'}@@ -460,14 +488,18 @@ function sceneNodeToDesignNode(}}+ const style = styleFromSceneNode(node, parent, css)+ const shader = layerShader(graph, node)+ if (shader) addShaderLayerStyle(style)return {type: 'element',tagName: tagNameForNode(node),attrs,- inlineStyle: styleFromSceneNode(node, parent, css),+ inlineStyle: style,sourceSceneNodeId: node.id,sourceSceneNode: node,- children+ children,+ shader}}diff --git a/packages/dom-css/src/export/tailwind-jsx.ts b/packages/dom-css/src/export/tailwind-jsx.tsindex c6ccf5561..ed1d247e9 100644--- a/packages/dom-css/src/export/tailwind-jsx.ts+++ b/packages/dom-css/src/export/tailwind-jsx.ts@@ -1,7 +1,8 @@import { jsx, type SyntaxNode } from '@open-pencil/emit'-import type { SceneGraph } from '@open-pencil/scene-graph'+import type { SceneGraph, ShaderComponent, ShaderPreset } from '@open-pencil/scene-graph'import type { DesignDocument, DesignElement, DesignNode } from '../types'+import { shaderEffectAttributes } from './components/shaders'import { mergeClassNames, serializeTailwindClasses } from './html'import { sceneNodeToDesignDocument, type SceneGraphToDesignOptions } from './projection'@@ -41,12 +42,51 @@ function attributes(node: DesignElement, themeVariables: readonly string[]): Syn)}+/** Where a layer's shader plays: filling the layer, beneath its content, inside its corners. */+const SHADER_CLASSES = 'pointer-events-none absolute inset-0 -z-10 rounded-[inherit]'++/**+ * A layer's shader as the `shaders` library's `<Shader>`, each effect a child with the preset's+ * props, with telemetry off as every OpenPencil export has it.+ */+function shaderElement(preset: ShaderPreset, depth: number): SyntaxNode {+ const effect =+ (at: number) =>+ (component: ShaderComponent): SyntaxNode =>+ jsx.element(+ component.type,+ shaderEffectAttributes(component),+ (component.children ?? []).map(effect(at + 1)),+ at+ )+ return jsx.element(+ 'Shader',+ [+ jsx.attribute('className', jsx.stringValue(SHADER_CLASSES)),+ jsx.attribute('disableTelemetry', null)+ ],+ preset.components.map(effect(depth + 1)),+ depth+ )+}++/** How many elements a shader prints, which no layer produced. */+function shaderElementCount(components: readonly ShaderComponent[] = []): number {+ return components.reduce(+ (count, component) => count + 1 + shaderElementCount(component.children),+ 0+ )+}+function element(node: DesignElement,depth: number,themeVariables: readonly string[]): SyntaxNode {- const children = node.children.map((child) => jsxNode(child, depth + 1, themeVariables))+ const children = [+ ...(node.shader ? [shaderElement(node.shader.preset, depth + 1)] : []),+ ...node.children.map((child) => jsxNode(child, depth + 1, themeVariables))+ ]// A lone text child stays on the element's line.const inline = children.length === 1 && node.children[0]?.type === 'text'return jsx.element(tagName(node), attributes(node, themeVariables), children, depth, inline)@@ -83,6 +123,10 @@ export interface TailwindJSXWithLayers {function collectLayerIds(node: DesignNode, layerIds: Array<string | null>): void {if (node.type === 'text') returnlayerIds.push(node.sourceSceneNodeId ?? node.sourceSceneNode?.id ?? null)+ if (node.shader) {+ const count = 1 + shaderElementCount(node.shader.preset.components)+ for (let index = 0; index < count; index++) layerIds.push(null)+ }for (const child of node.children) collectLayerIds(child, layerIds)}diff --git a/packages/dom-css/src/types.ts b/packages/dom-css/src/types.tsindex 9ca3cb77e..a74b3a7ee 100644--- a/packages/dom-css/src/types.ts+++ b/packages/dom-css/src/types.ts@@ -1,4 +1,4 @@-import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'+import type { SceneGraph, SceneNode, ShaderPreset } from '@open-pencil/scene-graph'import type { DesignTokens } from './tokens/references'@@ -22,6 +22,11 @@ export interface DesignElement {computedStyle?: DesignStyleDeclarationsourceSceneNodeId?: stringsourceSceneNode?: SceneNode+ /**+ * The shader the layer fills with, which plays behind its content: a framework draws it with+ * the `shaders` library, static HTML shows `frame`, its still frame as a data URL.+ */+ shader?: { preset: ShaderPreset; frame?: string }}export interface DesignText {diff --git a/packages/dom-css/tests/export/components/react.test.ts b/packages/dom-css/tests/export/components/react.test.tsindex 78403087c..82cba6562 100644--- a/packages/dom-css/tests/export/components/react.test.ts+++ b/packages/dom-css/tests/export/components/react.test.ts@@ -21,7 +21,8 @@ import {tabsComponent,toggleGroupComponent} from '#dom-css-tests/behaviours/fixtures'-import { cssRules } from '#dom-css-tests/helpers'+import { shaderBackdropSet, shaderHeroSet } from '#dom-css-tests/export/components/shader-fixtures'+import { cssRules, fileText } from '#dom-css-tests/helpers'import { exportStorybook } from '#dom-css/index'import { createElement, type ComponentType } from 'react'import { renderToStaticMarkup } from 'react-dom/server'@@ -33,6 +34,7 @@ const APP_PACKAGES = ['react-dom','radix-ui','@iconify/react',+ 'shaders','storybook','@types/react']@@ -114,6 +116,42 @@ describe('generated React plain components', () => {})})+describe('generated React shader fills', () => {+ test('play the shader behind the layer, with telemetry off, and type-check', async () => {+ const { files, folder } = await generate(shaderHeroSet())+ const source = fileText(files, 'Hero.tsx')+ const css = fileText(files, 'Hero.module.css')++ expect(await typeErrors(folder, 'Hero')).toEqual([])+ expect(source).toMatch(+ /import \{\s*Shader as ShaderCanvas,\s*Aurora as ShaderAurora,\s*FilmGrain as ShaderFilmGrain\s*\} from "shaders\/react"/+ )+ expect(source).toContain('<ShaderCanvas className={styles["hero-shader"]} disableTelemetry>')+ expect(source).toContain(+ '<ShaderAurora colorA="#ff3300" curtainCount={2} center={{ x: 0.5, y: 0 }} />'+ )+ // The shader comes first, so the title draws over it.+ expect(source.indexOf('<ShaderCanvas')).toBeLessThan(source.indexOf('Northern lights'))+ const styles = [...cssRules(css).values()]+ expect(styles).toContainEqual(expect.objectContaining({ isolation: 'isolate' }))+ expect(styles).toContainEqual(+ expect.objectContaining({ position: 'absolute', inset: '0', 'z-index': '-1' })+ )+ })+})++describe('generated React shader backdrops', () => {+ test('a layer with nothing in it plays its shader, not an image of it', async () => {+ const { files, folder } = await generate(shaderBackdropSet())+ const source = fileText(files, 'Card.tsx')++ expect(await typeErrors(folder, 'Card')).toEqual([])+ expect(source).not.toContain('<img')+ expect(source).not.toContain('data:image')+ expect(source).toContain('<ShaderCanvas')+ })+})+describe('generated React form controls', () => {test('import the types a number field with a slot uses, and type-check', async () => {const fixture = numberFieldComponent()diff --git a/packages/dom-css/tests/export/components/shader-fixtures.ts b/packages/dom-css/tests/export/components/shader-fixtures.tsnew file mode 100644index 000000000..99fb9db51--- /dev/null+++ b/packages/dom-css/tests/export/components/shader-fixtures.ts@@ -0,0 +1,71 @@+import { createShaderPaint, SceneGraph, withShaderPaints } from '@open-pencil/scene-graph'++/** The preset the hero plays: an aurora with a few props, and film grain over it. */+export const HERO_PRESET = {+ components: [+ { type: 'Aurora', props: { colorA: '#ff3300', curtainCount: 2, center: { x: 0.5, y: 0 } } },+ { type: 'FilmGrain' }+ ]+}++/** A plain component whose root fills with a shader behind its title. */+export function shaderHeroSet() {+ const graph = new SceneGraph()+ const set = graph.createNode('COMPONENT_SET', graph.getPages()[0].id, {+ name: 'Hero',+ componentPropertyDefinitions: [+ {+ id: 'size',+ name: 'Size',+ type: 'VARIANT',+ defaultValue: 'Large',+ variantOptions: ['Large']+ }+ ]+ })+ const { paint, shader } = createShaderPaint(HERO_PRESET)+ const variant = graph.createNode('COMPONENT', set.id, {+ name: 'Size=Large',+ componentPropertyValues: { Size: 'Large' },+ width: 320,+ height: 160,+ cornerRadius: 16,+ fills: [paint]+ })+ graph.updateNode(variant.id, { pluginData: withShaderPaints(variant, [paint], [shader]) })+ graph.createNode('TEXT', variant.id, { name: 'Title', text: 'Northern lights' })+ return { graph, set: graph.getNode(set.id) ?? set }+}++/** A plain component whose shader fills a child layer with nothing in it, a backdrop. */+export function shaderBackdropSet() {+ const graph = new SceneGraph()+ const set = graph.createNode('COMPONENT_SET', graph.getPages()[0].id, {+ name: 'Card',+ componentPropertyDefinitions: [+ {+ id: 'size',+ name: 'Size',+ type: 'VARIANT',+ defaultValue: 'Large',+ variantOptions: ['Large']+ }+ ]+ })+ const variant = graph.createNode('COMPONENT', set.id, {+ name: 'Size=Large',+ componentPropertyValues: { Size: 'Large' },+ width: 240,+ height: 120+ })+ const { paint, shader } = createShaderPaint(HERO_PRESET)+ graph.images.set(shader.image, new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]))+ const backdrop = graph.createNode('RECTANGLE', variant.id, {+ name: 'Backdrop',+ width: 240,+ height: 120,+ fills: [paint]+ })+ graph.updateNode(backdrop.id, { pluginData: withShaderPaints(backdrop, [paint], [shader]) })+ return { graph, set: graph.getNode(set.id) ?? set }+}diff --git a/packages/dom-css/tests/export/components/vue.test.ts b/packages/dom-css/tests/export/components/vue.test.tsindex fcb34136e..f2b4bf2a6 100644--- a/packages/dom-css/tests/export/components/vue.test.ts+++ b/packages/dom-css/tests/export/components/vue.test.ts@@ -20,14 +20,15 @@ import {tabsComponent,toggleGroupComponent} from '#dom-css-tests/behaviours/fixtures'-import { cssRules } from '#dom-css-tests/helpers'+import { shaderBackdropSet, shaderHeroSet } from '#dom-css-tests/export/components/shader-fixtures'+import { cssRules, fileText } from '#dom-css-tests/helpers'import { exportStorybook } from '#dom-css/index'import { createSSRApp, h, type Component } from 'vue'import { compileScript, parse } from 'vue/compiler-sfc'import { renderToString } from 'vue/server-renderer'/** The packages generated code imports, linked so it resolves them as an app would. */-const APP_PACKAGES = ['vue', 'reka-ui', '@iconify/vue', 'storybook']+const APP_PACKAGES = ['vue', 'reka-ui', '@iconify/vue', 'storybook', 'shaders']const output = await mkdtemp(join(tmpdir(), 'open-pencil-vue-'))await mkdir(join(output, 'node_modules'))@@ -247,6 +248,36 @@ describe('generated Vue plain components', () => {})})+describe('generated Vue shader fills', () => {+ test('play the shader behind the layer, with telemetry off', async () => {+ const { files, component } = await generate(shaderHeroSet())+ const source = fileText(files, 'Hero.vue')++ expect(source).toMatch(+ /import \{\s*Shader as ShaderCanvas,\s*Aurora as ShaderAurora,\s*FilmGrain as ShaderFilmGrain\s*\} from ["']shaders\/vue["']/+ )+ expect(source).toContain('<ShaderCanvas class="hero-shader" :disable-telemetry="true">')+ expect(source).toMatch(/<ShaderAurora[^>]*color-a="#ff3300"[^>]*:curtain-count="2"/)+ expect(source).toContain(':center="({ x: 0.5, y: 0 })"')+ const styles = [...cssRules(source.slice(source.indexOf('<style'))).values()]+ expect(styles).toContainEqual(expect.objectContaining({ isolation: 'isolate' }))+ // The shader comes first, so the title draws over it.+ const html = await render(component)+ expect(html.indexOf('hero-shader')).toBeLessThan(html.indexOf('Northern lights'))+ })+})++describe('generated Vue shader backdrops', () => {+ test('a layer with nothing in it plays its shader, not an image of it', async () => {+ const { files } = await generate(shaderBackdropSet())+ const source = fileText(files, 'Card.vue')++ expect(source).not.toContain('<img')+ expect(source).not.toContain('data:image')+ expect(source).toContain('<ShaderCanvas')+ })+})+describe('generated Vue form controls', () => {test('a slider binds one number within its range, the thumb and range placed by Reka', async () => {const { files, component } = await generate(sliderComponent())diff --git a/packages/dom-css/tests/export/shaders.test.ts b/packages/dom-css/tests/export/shaders.test.tsnew file mode 100644index 000000000..c251863da--- /dev/null+++ b/packages/dom-css/tests/export/shaders.test.ts@@ -0,0 +1,55 @@+import { describe, expect, test } from 'bun:test'++import {+ sceneNodeToDesignDocument,+ sceneNodesToTailwindJSXWithLayers,+ serializeHTML+} from '#dom-css/index'++import { createShaderPaint, SceneGraph, withShaderPaints } from '@open-pencil/scene-graph'++const PRESET = {+ components: [{ type: 'Aurora', props: { colorA: '#ff3300', speed: 2 } }, { type: 'FilmGrain' }]+}+const FRAME = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10])++/** A layer with no children that fills with a shader, whose still frame is saved. */+function shaderLayer() {+ const graph = new SceneGraph()+ const { paint, shader } = createShaderPaint(PRESET)+ graph.images.set(shader.image, FRAME)+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, {+ name: 'Glow',+ width: 200,+ height: 120,+ fills: [paint]+ })+ graph.updateNode(node.id, { pluginData: withShaderPaints(node, [paint], [shader]) })+ return { graph, id: node.id }+}++describe('shader fills in DOM exports', () => {+ test('Tailwind JSX plays the shader behind the layer instead of inlining its still frame', () => {+ const { graph, id } = shaderLayer()+ const { code, layerIds } = sceneNodesToTailwindJSXWithLayers(graph, [id])++ expect(code).not.toContain('data:image')+ expect(code).not.toContain('<img')+ expect(code).toContain('relative')+ expect(code).toContain('isolate')+ expect(code).toMatch(/<Shader className="[^"]*absolute[^"]*" disableTelemetry>/)+ expect(code).toContain('<Aurora colorA="#ff3300" speed={2} />')+ expect(code).toContain('<FilmGrain />')+ // The layer, then the shader and its two effects, which no layer produced.+ expect(layerIds).toEqual([id, null, null, null])+ })++ test('static HTML shows the still frame behind the layer', () => {+ const { graph, id } = shaderLayer()+ const html = serializeHTML(sceneNodeToDesignDocument(graph, id))++ expect(html).toMatch(+ /^<div[^>]*><img src="data:image\/png;base64,[^"]+" alt="" style="[^"]*z-index:-1/+ )+ })+})diff --git a/packages/dom-css/tests/helpers.ts b/packages/dom-css/tests/helpers.tsindex 1d10ede97..3286c6edc 100644--- a/packages/dom-css/tests/helpers.ts+++ b/packages/dom-css/tests/helpers.ts@@ -164,3 +164,14 @@ export function cssRules(css: string): Map<string, Record<string, string>> {visit(parse(css).cssRules)return rules}++/** The text of an exported file, such as a generated component or its stylesheet. */+export function fileText(+ files: readonly { path: string; content: string | Uint8Array }[],+ path: string+) {+ const content = files.find((file) => file.path === path)?.content+ if (content === undefined)+ throw new Error(`No ${path} among ${files.map((file) => file.path).join(', ')}`)+ return typeof content === 'string' ? content : new TextDecoder().decode(content)+}diff --git a/packages/emit/src/estree.ts b/packages/emit/src/estree.tsindex d03d36a50..1f6970482 100644--- a/packages/emit/src/estree.ts+++ b/packages/emit/src/estree.ts@@ -137,6 +137,20 @@ export function object(entries: (readonly [string, SyntaxNode])[]): SyntaxNode {export const array = (elements: SyntaxNode[]): SyntaxNode => ({ type: 'ArrayExpression', elements })+/**+ * A value read from JSON written as source: strings, finite numbers, booleans, null, and lists+ * and objects of them. Anything else, such as a function or an infinite number, is null.+ */+export function json(value: unknown): SyntaxNode {+ if (typeof value === 'string') return string(value)+ if (typeof value === 'number') return Number.isFinite(value) ? number(value) : json(null)+ if (typeof value === 'boolean' || value === null) return { type: 'Literal', value }+ if (Array.isArray(value)) return array(value.map(json))+ if (typeof value === 'object')+ return object(Object.entries(value).map(([key, item]) => [key, json(item)]))+ return json(null)+}+/** A call of a function by name, such as a helper inside an exported prop value. */export const call = (callee: string, args: SyntaxNode[]): SyntaxNode => ({type: 'CallExpression',diff --git a/packages/emit/tests/estree.test.ts b/packages/emit/tests/estree.test.tsnew file mode 100644index 000000000..0642d6df0--- /dev/null+++ b/packages/emit/tests/estree.test.ts@@ -0,0 +1,18 @@+import { describe, expect, test } from 'bun:test'++import { es } from '#emit/index'++describe('es.json', () => {+ test('writes a JSON value as source', () => {+ const value = { color: '#ff3300', count: -2, on: true, none: null, 'a-b': [0.5, { x: 1 }] }++ expect(es.printExpression(es.json(value))).toBe(+ "({\n color: '#ff3300',\n count: -2,\n on: true,\n none: null,\n 'a-b': [0.5, { x: 1 }]\n})"+ )+ })++ test('writes what JSON cannot hold as null', () => {+ expect(es.printExpression(es.json(Number.POSITIVE_INFINITY))).toBe('null')+ expect(es.printExpression(es.json(() => 1))).toBe('null')+ })+})diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.tsindex 8617bf121..811f8ca9f 100644--- a/packages/scene-graph/src/index.ts+++ b/packages/scene-graph/src/index.ts@@ -31,6 +31,8 @@ export * from './behaviours/kinds'export * from './icons/schema'export * from './icons/model'export * from './icons/glyph'+export * from './shaders/schema'+export * from './shaders/model'export * from './behaviours/layers'export * from './behaviours/model'export * from './behaviours/spec'diff --git a/packages/scene-graph/src/plugin-data/fields.ts b/packages/scene-graph/src/plugin-data/fields.tsindex 8d22124e9..2f386cf87 100644--- a/packages/scene-graph/src/plugin-data/fields.ts+++ b/packages/scene-graph/src/plugin-data/fields.ts@@ -6,6 +6,7 @@ import { isExportFormatId, type ExportFormatId } from '../export-format'import { clampExportScale } from '../export-scale'import { iconSchema, iconTintSchema } from '../icons/schema'import type { Rect } from '../primitives'+import { shaderPaintSchema } from '../shaders/schema'import {MODE_ATTRIBUTE_PATTERN,TOKEN_UNITS,@@ -138,14 +139,16 @@ const commentAuthorColor = v.fallback(undefined)-const commentReply = v.object({- id: v.string(),+/** What a comment and each reply say, who wrote them and when, and whether they were deleted. */+const commentEntry = {author: v.fallback(v.string(), ''),authorColor: commentAuthorColor,text: v.fallback(v.string(), ''),createdAt: v.fallback(v.string(), ''),deleted: v.optional(v.boolean())-})+}++const commentReply = v.object({ id: v.string(), ...commentEntry })const commentThread = v.object({id: v.string(),@@ -157,14 +160,10 @@ const commentThread = v.object({offsetY: v.optional(v.number()),x: v.fallback(v.number(), 0),y: v.fallback(v.number(), 0),- author: v.fallback(v.string(), ''),- authorColor: commentAuthorColor,- text: v.fallback(v.string(), ''),- createdAt: v.fallback(v.string(), ''),+ ...commentEntry,updatedAt: v.fallback(v.string(), ''),resolved: v.fallback(v.boolean(), false),resolvedAt: v.optional(v.nullable(v.string())),- deleted: v.optional(v.boolean()),replies: v.fallback(v.array(commentReply), [])})@@ -226,7 +225,9 @@ export const OPEN_PENCIL_PLUGIN_DATA = {/** The icon a frame draws, such as `lucide:home`, on the frame its paths are in. */icon: jsonPluginDataField('icon', 'content', iconSchema),/** Which of an icon path's paints its icon's color sets, on each path the source drew in it. */- iconTint: jsonPluginDataField('iconTint', 'content', iconTintSchema)+ iconTint: jsonPluginDataField('iconTint', 'content', iconTintSchema),+ /** One entry per paint that draws a shader, matched to its image paint by the frame's hash. */+ shader: jsonPluginDataField('shader', 'content', shaderPaintSchema)} satisfies Record<string, PluginDataKey>const NOT_CONTENT = Object.values(OPEN_PENCIL_PLUGIN_DATA).filter(diff --git a/packages/scene-graph/src/shaders/model.ts b/packages/scene-graph/src/shaders/model.tsnew file mode 100644index 000000000..e28fbc201--- /dev/null+++ b/packages/scene-graph/src/shaders/model.ts@@ -0,0 +1,98 @@+import { TRANSPARENT } from '../constants'+import { computeImageHash } from '../images'+import { readAllPluginData, withAllPluginData } from '../plugin-data/field'+import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'+import { randomHex } from '../random'+import type { Fill, SceneNode } from '../types'+import type { ShaderPaint, ShaderPreset } from './schema'++/** What shader paints read of a node. */+type ShaderNode = Pick<SceneNode, 'fills' | 'strokes' | 'pluginData'>++/** Every shader a node's fills and strokes draw, whether or not a paint still shows it. */+export function readShaderPaints(node: Pick<SceneNode, 'pluginData'>): ShaderPaint[] {+ return readAllPluginData(node.pluginData, OPEN_PENCIL_PLUGIN_DATA.shader)+}++/**+ * The shader a fill or stroke of `node` draws: an image paint whose image is a shader's frame.+ * Null for any other paint, including one whose image was replaced, which then draws as the+ * image it holds.+ */+export function shaderOfPaint(+ node: Pick<SceneNode, 'pluginData'>,+ paint: Fill+): ShaderPaint | null {+ if (paint.type !== 'IMAGE' || !paint.imageHash) return null+ return readShaderPaints(node).find((shader) => shader.image === paint.imageHash) ?? null+}++/** Whether any paint of `node` draws a shader. */+export function hasShaderPaint(node: ShaderNode): boolean {+ return [...node.fills, ...node.strokes].some((paint) => shaderOfPaint(node, paint) !== null)+}++/**+ * The node's plugin data with `shaders` set by their image, keeping only shaders a fill or+ * stroke of `paints` still shows, so removing or replacing a paint leaves no entry behind.+ */+export function withShaderPaints(+ node: Pick<SceneNode, 'pluginData'>,+ paints: readonly Fill[],+ shaders: readonly ShaderPaint[] = []+): SceneNode['pluginData'] {+ const shown = new Set(+ paints.flatMap((paint) => (paint.type === 'IMAGE' && paint.imageHash ? [paint.imageHash] : []))+ )+ const byImage = new Map(readShaderPaints(node).map((shader) => [shader.image, shader]))+ for (const shader of shaders) byImage.set(shader.image, shader)+ return withAllPluginData(+ node.pluginData,+ OPEN_PENCIL_PLUGIN_DATA.shader,+ [...byImage.values()].filter((shader) => shown.has(shader.image))+ )+}++/**+ * A new paint that draws `preset`, and the shader entry it shows. Its image names a frame not yet+ * rendered, unique to this paint, so no other paint's entry claims it until the frame is drawn.+ */+export function createShaderPaint(preset: ShaderPreset): {+ paint: Fill+ shader: ShaderPaint+} {+ const image = randomHex(20)+ return {+ paint: {+ type: 'IMAGE',+ color: TRANSPARENT,+ opacity: 1,+ visible: true,+ imageHash: image,+ imageScaleMode: 'FILL'+ },+ shader: { image, preset: structuredClone(preset) }+ }+}++/** A short key that tells one preset from another, which a rendered frame records. */+export function shaderPresetKey(preset: ShaderPreset): string {+ return computeImageHash(new TextEncoder().encode(JSON.stringify(preset)))+}++/**+ * Whether a shader's frame was rendered for its preset at the size `node` has now, so undoing a+ * preset change after reopening the document does not keep a frame of the newer preset.+ */+export function isShaderFrameCurrent(+ shader: ShaderPaint,+ size: { width: number; height: number }+): boolean {+ const { frame } = shader+ return (+ frame !== undefined &&+ frame.preset === shaderPresetKey(shader.preset) &&+ Math.round(frame.width) === Math.round(size.width) &&+ Math.round(frame.height) === Math.round(size.height)+ )+}diff --git a/packages/scene-graph/src/shaders/schema.ts b/packages/scene-graph/src/shaders/schema.tsnew file mode 100644index 000000000..bf9ed9fe0--- /dev/null+++ b/packages/scene-graph/src/shaders/schema.ts@@ -0,0 +1,48 @@+import * as v from 'valibot'++/** One effect of a shader preset, as the `shaders` library names it, with the effects it holds. */+export interface ShaderComponent {+ type: string+ id?: string+ props?: Record<string, unknown>+ children?: ShaderComponent[]+}++const shaderComponent: v.GenericSchema<unknown, ShaderComponent> = v.object({+ type: v.pipe(v.string(), v.nonEmpty(), v.maxLength(100)),+ id: v.optional(v.string()),+ props: v.optional(v.record(v.string(), v.unknown())),+ children: v.optional(v.array(v.lazy(() => shaderComponent)))+})++/**+ * A shader as the `shaders` library describes it and shaders.com exports it: a tree of effects,+ * drawn first to last.+ */+export const shaderPresetSchema = v.object({+ components: v.pipe(v.array(shaderComponent), v.minLength(1)),+ structureVersion: v.optional(v.number())+})++export type ShaderPreset = v.InferOutput<typeof shaderPresetSchema>++/**+ * A paint that draws a shader. The paint itself is an image fill of the shader's still frame, so+ * every format and renderer that reads images shows it; this entry says which image it is, the+ * preset that draws it, and the layer size and preset the frame was rendered for, which is+ * missing until it is first rendered.+ */+export const shaderPaintSchema = v.object({+ image: v.pipe(v.string(), v.nonEmpty()),+ preset: shaderPresetSchema,+ frame: v.optional(+ v.object({+ width: v.pipe(v.number(), v.finite(), v.minValue(0)),+ height: v.pipe(v.number(), v.finite(), v.minValue(0)),+ /** The preset's key (`shaderPresetKey`) when the frame was rendered. */+ preset: v.pipe(v.string(), v.nonEmpty())+ })+ )+})++export type ShaderPaint = v.InferOutput<typeof shaderPaintSchema>diff --git a/packages/scene-graph/tests/shaders/model.test.ts b/packages/scene-graph/tests/shaders/model.test.tsnew file mode 100644index 000000000..16fbe2164--- /dev/null+++ b/packages/scene-graph/tests/shaders/model.test.ts@@ -0,0 +1,110 @@+import { describe, expect, test } from 'bun:test'++import {+ createShaderPaint,+ isShaderFrameCurrent,+ OPEN_PENCIL_PLUGIN_DATA,+ pluginDataEntry,+ readShaderPaints,+ SceneGraph,+ shaderOfPaint,+ shaderPresetKey,+ withShaderPaints,+ type ShaderPreset+} from '@open-pencil/scene-graph'++const AURORA: ShaderPreset = {+ components: [{ type: 'Aurora', props: { colorA: '#7c3aed' } }]+}+const SWIRL: ShaderPreset = { components: [{ type: 'Swirl' }] }++function rectangle() {+ const graph = new SceneGraph()+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, {+ width: 100,+ height: 50+ })+ return { graph, node }+}++describe('shader paints', () => {+ test('an image paint draws the shader whose frame it shows', () => {+ const { graph, node } = rectangle()+ const { paint, shader } = createShaderPaint(AURORA)+ graph.updateNode(node.id, {+ fills: [paint],+ pluginData: withShaderPaints(node, [paint], [shader])+ })+ const stored = graph.getNode(node.id)+ if (!stored) throw new Error('missing node')++ expect(shaderOfPaint(stored, paint)?.preset).toEqual(AURORA)+ expect(shaderOfPaint(stored, { ...paint, imageHash: 'other' })).toBeNull()+ })++ test('keeps the entry with its paint when fills are reordered', () => {+ const { graph, node } = rectangle()+ const aurora = createShaderPaint(AURORA)+ const swirl = createShaderPaint(SWIRL)+ const fills = [aurora.paint, swirl.paint]+ graph.updateNode(node.id, {+ fills,+ pluginData: withShaderPaints(node, fills, [aurora.shader, swirl.shader])+ })+ const stored = graph.getNode(node.id)+ if (!stored) throw new Error('missing node')++ expect(shaderOfPaint(stored, swirl.paint)?.preset).toEqual(SWIRL)+ expect(shaderOfPaint(stored, aurora.paint)?.preset).toEqual(AURORA)+ })++ test('drops shaders no paint shows any more', () => {+ const { node } = rectangle()+ const aurora = createShaderPaint(AURORA)+ const swirl = createShaderPaint(SWIRL)+ const both = {+ ...node,+ pluginData: withShaderPaints(node, [aurora.paint, swirl.paint], [aurora.shader, swirl.shader])+ }++ const kept = withShaderPaints(both, [swirl.paint])++ expect(readShaderPaints({ pluginData: kept }).map((shader) => shader.preset)).toEqual([SWIRL])+ })++ test('ignores an entry that is not a shader', () => {+ const entry = pluginDataEntry(OPEN_PENCIL_PLUGIN_DATA.shader, {+ image: 'abc',+ preset: AURORA+ })++ expect(+ readShaderPaints({+ pluginData: [{ ...entry, value: '{"image":"abc","preset":{"components":[]}}' }]+ })+ ).toEqual([])+ expect(readShaderPaints({ pluginData: [entry] })).toHaveLength(1)+ })++ test('a frame drawn for another preset is not current', () => {+ const { shader } = createShaderPaint(SWIRL)+ const drawnForAurora = {+ ...shader,+ frame: { width: 100, height: 50, preset: shaderPresetKey(AURORA) }+ }++ expect(isShaderFrameCurrent(drawnForAurora, { width: 100, height: 50 })).toBe(false)+ })++ test('a frame is current for the size it was rendered at', () => {+ const { shader } = createShaderPaint(AURORA)++ expect(isShaderFrameCurrent(shader, { width: 100, height: 50 })).toBe(false)+ const rendered = {+ ...shader,+ frame: { width: 100, height: 50, preset: shaderPresetKey(AURORA) }+ }+ expect(isShaderFrameCurrent(rendered, { width: 100.2, height: 50 })).toBe(true)+ expect(isShaderFrameCurrent(rendered, { width: 120, height: 50 })).toBe(false)+ })+})diff --git a/packages/vue/package.json b/packages/vue/package.jsonindex 2303e489e..37155eeeb 100644--- a/packages/vue/package.json+++ b/packages/vue/package.json@@ -51,7 +51,8 @@"es-toolkit": "^1.52.0","fuse.js": "^7.5.0","nanostores": "^1.5.4",- "reka-ui": "^2.10.5"+ "reka-ui": "^2.10.5",+ "shaders": "4.0.4"},"devDependencies": {"tsdown": "^0.22.14",diff --git a/packages/vue/src/canvas/surface/lifecycle.ts b/packages/vue/src/canvas/surface/lifecycle.tsindex 4d128f908..dbe6efed7 100644--- a/packages/vue/src/canvas/surface/lifecycle.ts+++ b/packages/vue/src/canvas/surface/lifecycle.ts@@ -20,6 +20,7 @@ import { createImagePreviewDecoder } from '#vue/canvas/surface/image-preview'import { useCanvasKitLoader } from '#vue/canvas/surface/kit-loader'import { createCanvasRenderLoop } from '#vue/canvas/surface/render-loop'import { useCanvasResizeObserver } from '#vue/canvas/surface/resize-observer'+import { createShaderRasterizer } from '#vue/canvas/surface/shader-rasterizer'import type { UseCanvasOptions } from '#vue/canvas/surface/types'type SurfaceManagerState = {@@ -111,6 +112,9 @@ export function createCanvasSurfaceManager({state.renderer.tracksSceneSettlement = options?.layer !== 'overlays'state.renderer.tiledSceneEnabled = options?.sceneRenderer === 'tiled'editor.setCanvasKit(ck, state.renderer)+ // The scene surface draws shader frames; an overlay surface has none to draw.+ if (options?.layer !== 'overlays' && !editor.hasShaderRasterizer())+ editor.setShaderRasterizer(createShaderRasterizer())canvas.dataset.ready = '1'// When the surface is recreated after a resize fallback, destroyRendererdiff --git a/packages/vue/src/canvas/surface/shader-playback.ts b/packages/vue/src/canvas/surface/shader-playback.tsnew file mode 100644index 000000000..e22ed3d3e--- /dev/null+++ b/packages/vue/src/canvas/surface/shader-playback.ts@@ -0,0 +1,304 @@+import { useRafFn, watchThrottled } from '@vueuse/core'+import type { createRendererFromJSON } from 'shaders/core'+import { onScopeDispose, watch } from 'vue'++import type { SkiaRenderer } from '@open-pencil/core/canvas'+import type { Editor, EditorState } from '@open-pencil/core/editor'+import { readShaderPaints, shaderOfPaint, type ShaderPreset } from '@open-pencil/scene-graph'+import type { Rect } from '@open-pencil/scene-graph/primitives'++import { canDrawShaders } from '#vue/canvas/surface/shader-rasterizer'++/** The longest side a playing shader is drawn at, however large it is on screen. */+const MAX_PLAY_EDGE = 1024+/**+ * How long a frame may spend drawing shaders and handing them to the canvas. A browser where+ * that is slow, such as Safari, which copies each WebGPU canvas into WebGL synchronously, draws+ * as many as fit and the rest on the next frames, in turn, rather than stalling every frame.+ */+const FRAME_BUDGET_MS = 8+/** The least time between frames where each frame is copied. */+const COPY_FRAME_MS = 1000 / 30+/** How often what is on screen is checked while the view moves. */+const VIEW_CHECK_MS = 200+/** How much a shader's size on screen may change before it is drawn at the new size. */+const RESIZE_FACTOR = 1.5++/** A shader playing on its own WebGPU canvas, shown wherever its image is painted. */+interface Player {+ preset: string+ nodeIds: Set<string>+ canvas: HTMLCanvasElement+ renderer: ReturnType<typeof createRendererFromJSON> | null+ /** Whether it is on screen, the only time it draws. */+ visible: boolean+ disposed: boolean+ /** The copy of its last frame shown, where frames are copied, closed when the next replaces it. */+ copied: ImageBitmap | null+ /** When it last drew, so its clock advances by the time since then however often it draws. */+ drawnAt: number+}++interface PageShader {+ preset: ShaderPreset+ nodeIds: Set<string>+ /** Where the layers that paint it are, on the page. */+ bounds: Rect[]+}++/** Each image a shader paint on `pageId` shows, with its preset and the layers painting it. */+function shadersOnPage(editor: Editor, pageId: string) {+ const found = new Map<string, PageShader>()+ for (const node of editor.graph.getAllNodes()) {+ if (readShaderPaints(node).length === 0 || !editor.graph.isDescendant(node.id, pageId)) continue+ for (const paint of [...node.fills, ...node.strokes]) {+ const shader = shaderOfPaint(node, paint)+ if (!shader || !paint.visible) continue+ const entry = found.get(shader.image) ?? {+ preset: shader.preset,+ nodeIds: new Set<string>(),+ bounds: []+ }+ entry.nodeIds.add(node.id)+ entry.bounds.push(editor.graph.getAbsoluteBounds(node.id))+ found.set(shader.image, entry)+ }+ }+ return found+}++/**+ * Plays the shader paints on screen while the editor previews a page: each runs on a WebGPU+ * canvas at about its size on screen, and its frames replace the still frame wherever the canvas+ * paints that image, so the layers above, masks, and clipping draw as usual. A shader scrolled+ * out of view pauses; leaving preview shows the still frames again. Without WebGPU the still+ * frames stay.+ */+export function useShaderPlayback(options: {+ editor: Editor+ getRenderer: () => SkiaRenderer | null+ /** The view this canvas draws: its pan, zoom, page, and whether it previews. */+ getView: () => Pick<EditorState, 'panX' | 'panY' | 'zoom' | 'currentPageId' | 'play'>+ getViewport: () => { width: number; height: number }+ markDirty: () => void+}) {+ const { editor, getRenderer, getView, getViewport, markDirty } = options+ const players = new Map<string, Player>()+ /** Shaders that failed to start, by the preset that failed, which are not tried again. */+ const failed = new Map<string, string>()+ /** Renderers to let go of once the frame being drawn is done with them. */+ const retired: Player[] = []+ let last = 0+ let stepping = false+ /** Where the next frame starts drawing, when the last one ran out of time. */+ let turn = 0+ /** Whether this browser needs each frame copied before the canvas can show it. */+ let copyFrames = false++ function release() {+ for (const player of retired.splice(0)) {+ player.renderer?.dispose()+ player.copied?.close()+ }+ }++ function stop(hash: string) {+ const player = players.get(hash)+ if (!player) return+ player.disposed = true+ players.delete(hash)+ getRenderer()?.setLiveImage(hash, null, player.nodeIds)+ retired.push(player)+ if (!stepping) release()+ }++ /** How many pixels a shader painted over `bounds` covers on screen, at most. */+ function screenSize(bounds: Rect[]): { width: number; height: number } {+ const scale = getView().zoom * (window.devicePixelRatio || 1)+ const width = Math.max(...bounds.map((rect) => rect.width)) * scale+ const height = Math.max(...bounds.map((rect) => rect.height)) * scale+ const fit = Math.min(1, MAX_PLAY_EDGE / Math.max(width, height, 1))+ return {+ width: Math.max(1, Math.round(width * fit)),+ height: Math.max(1, Math.round(height * fit))+ }+ }++ function onScreen(bounds: Rect[]): boolean {+ const { panX, panY, zoom } = getView()+ const view = getViewport()+ return bounds.some(+ (rect) =>+ rect.x * zoom + panX < view.width &&+ (rect.x + rect.width) * zoom + panX > 0 &&+ rect.y * zoom + panY < view.height &&+ (rect.y + rect.height) * zoom + panY > 0+ )+ }++ async function start(+ hash: string,+ preset: ShaderPreset,+ size: { width: number; height: number }+ ) {+ const canvas = document.createElement('canvas')+ // The renderer sizes its buffer at the device pixel ratio itself.+ const ratio = window.devicePixelRatio || 1+ canvas.width = Math.max(1, Math.round(size.width / ratio))+ canvas.height = Math.max(1, Math.round(size.height / ratio))+ const player: Player = {+ preset: JSON.stringify(preset),+ nodeIds: new Set(),+ canvas,+ renderer: null,+ visible: true,+ disposed: false,+ copied: null,+ drawnAt: 0+ }+ players.set(hash, player)+ // Read through a call: the player may be stopped while the library loads.+ const stopped = () => player.disposed+ let renderer: Player['renderer'] = null+ try {+ const shaders = await import('shaders/core')+ if (stopped()) return+ renderer = shaders.createRendererFromJSON(structuredClone(preset))+ await renderer.initialize(canvas)+ } catch (error) {+ // A shader that cannot play keeps its still frame until its preset changes. A newer+ // player that replaced this one while it started is left alone.+ console.warn('Could not play a shader', error)+ renderer?.dispose()+ if (players.get(hash) === player) {+ stop(hash)+ failed.set(hash, player.preset)+ }+ return+ }+ if (stopped()) renderer.dispose()+ else player.renderer = renderer+ }++ /** Whether a player drawing at `canvas`'s size should be redrawn at `size` instead. */+ function resized(canvas: HTMLCanvasElement, size: { width: number; height: number }) {+ const ratio = window.devicePixelRatio || 1+ const factor = size.width / Math.max(1, canvas.width * ratio)+ return factor > RESIZE_FACTOR || factor < 1 / RESIZE_FACTOR+ }++ function sync() {+ const view = getView()+ const playing = view.play !== null && canDrawShaders()+ const wanted = playing ? shadersOnPage(editor, view.currentPageId) : new Map()+ for (const [hash, player] of players) {+ const shader = wanted.get(hash)+ if (!shader || JSON.stringify(shader.preset) !== player.preset) stop(hash)+ }+ for (const [hash, shader] of wanted) {+ if (failed.get(hash) !== JSON.stringify(shader.preset)) failed.delete(hash)+ const visible = onScreen(shader.bounds)+ const size = screenSize(shader.bounds)+ const existing = players.get(hash)+ if (existing && visible && resized(existing.canvas, size)) stop(hash)+ if (!players.has(hash) && visible && !failed.has(hash)) void start(hash, shader.preset, size)+ const player = players.get(hash)+ if (!player) continue+ player.nodeIds = shader.nodeIds+ player.visible = visible+ }+ if (players.size > 0) resume()+ else {+ pause()+ last = 0+ }+ }++ const { pause, resume } = useRafFn(+ ({ timestamp }) => {+ if (stepping || (copyFrames && last !== 0 && timestamp - last < COPY_FRAME_MS)) return+ last = timestamp+ stepping = true+ void step(timestamp).finally(() => {+ stepping = false+ release()+ })+ },+ { immediate: false }+ )++ /**+ * Draws every visible shader and shows its frame. The WebGPU canvas itself is uploaded right+ * after drawing, in the same task, since it keeps its frame only until that task ends. A+ * browser that cannot upload a WebGPU canvas gets a copy of each frame instead, which costs a+ * readback, so frames then come at most every `COPY_FRAME_MS`.+ */+ async function step(now: number) {+ const renderer = getRenderer()+ if (!renderer) return+ const copies: Promise<{ hash: string; player: Player; frame: ImageBitmap }>[] = []+ let drew = false+ const drawing = [...players].filter(([, player]) => player.visible && player.renderer)+ const started = performance.now()+ for (let index = 0; index < drawing.length; index++) {+ const [hash, player] = drawing[(turn + index) % drawing.length]+ const delta = player.drawnAt === 0 ? 0 : (now - player.drawnAt) / 1000+ player.drawnAt = now+ await player.renderer?.renderFrame({ deltaSeconds: delta, waitForGpu: false })+ if (player.disposed) continue+ if (!copyFrames && uploads(renderer, hash, player)) drew = true+ else {+ copyFrames = true+ copies.push(createImageBitmap(player.canvas).then((frame) => ({ hash, player, frame })))+ }+ // The next frame starts with the shaders this one had no time for.+ if (performance.now() - started > FRAME_BUDGET_MS) {+ turn = (turn + index + 1) % drawing.length+ break+ }+ }+ for (const { hash, player, frame } of await Promise.all(copies)) {+ if (player.disposed) {+ frame.close()+ continue+ }+ renderer.setLiveImage(hash, frame, player.nodeIds)+ player.copied?.close()+ player.copied = frame+ drew = true+ }+ if (drew) markDirty()+ }++ /** Whether the canvas shows `player`'s WebGPU canvas uploaded as it is. */+ function uploads(renderer: SkiaRenderer, hash: string, player: Player): boolean {+ try {+ return renderer.setLiveImage(hash, player.canvas, player.nodeIds)+ } catch {+ return false+ }+ }++ watch(() => [getView().play !== null, getView().currentPageId, editor.state.sceneVersion], sync, {+ immediate: true+ })+ // The view moving or the canvas resizing, as it does when preview hides the panels, changes+ // what is on screen.+ watchThrottled(+ () => [+ getView().panX,+ getView().panY,+ getView().zoom,+ getViewport().width,+ getViewport().height+ ],+ sync,+ { throttle: VIEW_CHECK_MS }+ )++ onScopeDispose(() => {+ pause()+ for (const hash of Array.from(players.keys())) stop(hash)+ release()+ })+}diff --git a/packages/vue/src/canvas/surface/shader-rasterizer.ts b/packages/vue/src/canvas/surface/shader-rasterizer.tsnew file mode 100644index 000000000..231603db0--- /dev/null+++ b/packages/vue/src/canvas/surface/shader-rasterizer.ts@@ -0,0 +1,53 @@+import type { ShaderRasterizer } from '@open-pencil/core/editor'++/** Whether this browser can draw shaders, which need WebGPU. */+export function canDrawShaders(): boolean {+ if (typeof navigator === 'undefined') return false+ // WebGPU is optional, though the DOM types declare it: shader paints keep their saved frame+ // without it.+ const gpu: unknown = Reflect.get(navigator, 'gpu')+ return gpu !== undefined+}++/**+ * Draws shader frames with the `shaders` library's preset renderer on a detached WebGPU canvas,+ * one at a time, and encodes each as PNG. The library is loaded on the first frame, so documents+ * without shaders never fetch it. Null where the browser has no WebGPU.+ */+export function createShaderRasterizer(): ShaderRasterizer | null {+ if (!canDrawShaders()) return null+ let destroyed = false+ // Read through a call: `destroy` may run while a frame is drawn.+ const isDestroyed = () => destroyed+ return {+ async render(preset, size) {+ const { createRendererFromJSON } = await import('shaders/core')+ if (isDestroyed()) return null+ const canvas = document.createElement('canvas')+ // The renderer sizes its buffer at the device pixel ratio itself.+ const ratio = window.devicePixelRatio || 1+ canvas.width = Math.max(1, Math.round(size.width / ratio))+ canvas.height = Math.max(1, Math.round(size.height / ratio))+ const renderer = createRendererFromJSON(structuredClone(preset))+ try {+ await renderer.initialize(canvas)+ // A still frame: the clocks stay at zero. The first frame only prepares the effects'+ // pipelines, so the one kept is the second; reading the canvas waits for the GPU.+ await renderer.renderFrame({ deltaSeconds: 0, waitForGpu: false })+ await renderer.renderFrame({ deltaSeconds: 0, waitForGpu: false })+ const encoded = new Promise<Blob | null>((resolve) => {+ canvas.toBlob(resolve, 'image/png')+ })+ const blob = await encoded+ if (isDestroyed() || !blob) return null+ const bytes = new Uint8Array(await blob.arrayBuffer())+ return isDestroyed() ? null : bytes+ } finally {+ renderer.dispose()+ }+ },+ destroy() {+ destroyed = true+ }+ }+}diff --git a/packages/vue/src/canvas/surface/use.ts b/packages/vue/src/canvas/surface/use.tsindex 8168158f5..18a0e4e25 100644--- a/packages/vue/src/canvas/surface/use.ts+++ b/packages/vue/src/canvas/surface/use.ts@@ -1,3 +1,4 @@+import { useElementSize } from '@vueuse/core'import type { CanvasKit } from 'canvaskit-wasm'import type { Ref } from 'vue'@@ -8,6 +9,7 @@ import {useCanvasSurfaceLifecycle} from '#vue/canvas/surface/lifecycle'import { createCanvasHitTests, createRulerVisibility } from '#vue/canvas/surface/overlays'+import { useShaderPlayback } from '#vue/canvas/surface/shader-playback'import type { UseCanvasOptions } from '#vue/canvas/surface/types'export type { UseCanvasOptions } from '#vue/canvas/surface/types'@@ -49,6 +51,19 @@ export function useCanvas(onReady: options?.onReady})+ // The scene surface plays shaders in preview; an overlay surface draws none.+ if (options?.layer !== 'overlays') {+ // The view's size in CSS pixels, which the editor's pan and zoom map the page onto.+ const viewport = useElementSize(canvasRef)+ useShaderPlayback({+ editor,+ getRenderer: surface.getRenderer,+ getView: () => options?.getRenderState?.() ?? editor.state,+ getViewport: () => ({ width: viewport.width.value, height: viewport.height.value }),+ markDirty: surface.markDirty+ })+ }+const { hitTestSectionTitle, hitTestComponentLabel, hitTestFrameTitle, hitTestIssueMarker } =createCanvasHitTests(editor, surface.getRenderer)diff --git a/packages/vue/src/controls/shader/catalog.ts b/packages/vue/src/controls/shader/catalog.tsnew file mode 100644index 000000000..1ff49a3c1--- /dev/null+++ b/packages/vue/src/controls/shader/catalog.ts@@ -0,0 +1,145 @@+import * as v from 'valibot'++/** A prop of an effect, as the `shaders` library's controls describe it. */+export type ShaderPropControl = { key: string; label: string; group: string; default: unknown } & (+ | { kind: 'range'; min: number; max: number; step: number }+ | { kind: 'color' }+ | { kind: 'select'; options: { label: string; value: string | number }[] }+ | { kind: 'checkbox' }+ | { kind: 'position' }+ | { kind: 'text' }+ /** Gradient stops, shapes, uploads, and the like, edited in the preset's JSON. */+ | { kind: 'other' }+)++/** An effect the `shaders` library draws, with the controls of its props. */+export interface ShaderEffect {+ name: string+ category: string+ description: string+ /** Whether it changes what is drawn beneath it, such as a blur, rather than drawing its own. */+ filter: boolean+ props: ShaderPropControl[]+}++const label = v.optional(v.string())+const uiTypes = (...types: string[]) =>+ v.union([+ v.picklist(types),+ v.pipe(+ v.array(v.string()),+ v.check((list) => types.includes(list[0] ?? ''))+ )+ ])++const rangeUI = v.object({+ type: uiTypes('range'),+ min: v.number(),+ max: v.number(),+ step: v.optional(v.number()),+ label,+ group: v.optional(v.string())+})+const selectUI = v.object({+ type: uiTypes('select'),+ options: v.array(v.object({ label: v.string(), value: v.union([v.string(), v.number()]) })),+ label,+ group: v.optional(v.string())+})+const plainUI = v.object({+ type: uiTypes('color', 'checkbox', 'position', 'origin', 'text'),+ label,+ group: v.optional(v.string())+})+const propMetadata = v.object({ ui: v.optional(v.unknown()), default: v.optional(v.unknown()) })++/** Words a camel-cased prop name is made of, for a prop without a label. */+const words = (key: string) =>+ key.replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/^./, (c) => c.toUpperCase())++function control(key: string, metadata: unknown): ShaderPropControl | null {+ const parsed = v.safeParse(propMetadata, metadata)+ if (!parsed.success) return null+ const { ui } = parsed.output+ const base = { key, default: parsed.output.default }+ const range = v.safeParse(rangeUI, ui)+ if (range.success) {+ const { min, max, step = (max - min) / 100 } = range.output+ return {+ ...base,+ label: range.output.label ?? words(key),+ group: range.output.group ?? '',+ kind: 'range',+ min,+ max,+ step+ }+ }+ const select = v.safeParse(selectUI, ui)+ if (select.success) {+ const { options } = select.output+ return {+ ...base,+ label: select.output.label ?? words(key),+ group: select.output.group ?? '',+ kind: 'select',+ options+ }+ }+ const plain = v.safeParse(plainUI, ui)+ if (plain.success) {+ const [type] = [plain.output.type].flat()+ const kind =+ type === 'origin' ? 'position' : (type as 'color' | 'checkbox' | 'position' | 'text')+ return {+ ...base,+ label: plain.output.label ?? words(key),+ group: plain.output.group ?? '',+ kind+ }+ }+ return { ...base, label: words(key), group: '', kind: 'other' }+}++const registryEntry = v.object({+ name: v.string(),+ category: v.string(),+ description: v.optional(v.string()),+ requiresChild: v.optional(v.boolean()),+ propsMetadata: v.optional(v.record(v.string(), v.unknown()))+})++/** An effect's name in words, `FilmGrain` as Film grain, as the properties panel names effects. */+export function shaderEffectLabel(name: string): string {+ return name+ .replace(/([a-z0-9])([A-Z])/g, '$1 $2')+ .replace(/ ([A-Z])(?=[a-z])/g, (_, letter: string) => ` ${letter.toLowerCase()}`)+}++let catalog: Promise<ShaderEffect[]> | null = null++/**+ * Every effect the `shaders` library draws, by name. The registry is large, so it is loaded+ * the first time a picker asks for it and kept for the session.+ */+export function loadShaderCatalog(): Promise<ShaderEffect[]> {+ catalog ??= import('shaders/registry').then(({ getAllShaders }) =>+ getAllShaders().flatMap((entry) => {+ const parsed = v.safeParse(registryEntry, entry)+ if (!parsed.success) return []+ const {+ name,+ category,+ description = '',+ requiresChild = false,+ propsMetadata = {}+ } = parsed.output+ const props = Object.entries(propsMetadata).flatMap(([key, metadata]) => {+ const item = control(key, metadata)+ return item ? [item] : []+ })+ return [{ name, category, description, filter: requiresChild, props }]+ })+ )+ return catalog+}diff --git a/packages/vue/src/controls/shader/index.ts b/packages/vue/src/controls/shader/index.tsnew file mode 100644index 000000000..9bf805eb2--- /dev/null+++ b/packages/vue/src/controls/shader/index.ts@@ -0,0 +1,13 @@+export { loadShaderCatalog, shaderEffectLabel } from './catalog'+export { shaderEffectPreview } from './preview'+export type { ShaderEffect, ShaderPropControl } from './catalog'+export {+ addShaderEffect,+ DEFAULT_SHADER_EFFECT,+ DEFAULT_SHADER_PRESET,+ moveShaderEffect,+ parseShaderPreset,+ removeShaderEffect,+ setShaderEffectProp,+ shaderPresetJSON+} from './preset'diff --git a/packages/vue/src/controls/shader/preset.ts b/packages/vue/src/controls/shader/preset.tsnew file mode 100644index 000000000..bc58efaac--- /dev/null+++ b/packages/vue/src/controls/shader/preset.ts@@ -0,0 +1,60 @@+import * as v from 'valibot'++import { shaderPresetSchema, type ShaderPreset } from '@open-pencil/scene-graph'++/**+ * Edits of a shader preset's stack of effects. The effects at the top level draw first to last,+ * each over the ones before it; every edit returns a new preset.+ */++/** The effect a new shader paint, or a new effect in one, starts with. */+export const DEFAULT_SHADER_EFFECT = 'Aurora'++/** The preset a new shader paint starts with. */+export const DEFAULT_SHADER_PRESET: ShaderPreset = { components: [{ type: DEFAULT_SHADER_EFFECT }] }++/** `preset` with the effect `type` added on top, drawn with its default props. */+export function addShaderEffect(preset: ShaderPreset, type: string): ShaderPreset {+ return { ...preset, components: [...preset.components, { type }] }+}++/** `preset` without its effect at `index`; the last effect stays, since a shader needs one. */+export function removeShaderEffect(preset: ShaderPreset, index: number): ShaderPreset {+ if (preset.components.length <= 1) return preset+ return { ...preset, components: preset.components.filter((_, at) => at !== index) }+}++/** `preset` with its effect at `from` moved to `to`. */+export function moveShaderEffect(preset: ShaderPreset, from: number, to: number): ShaderPreset {+ const last = preset.components.length - 1+ if (from < 0 || from > last || to < 0 || to > last) return preset+ const components = [...preset.components]+ components.splice(to, 0, ...components.splice(from, 1))+ return { ...preset, components }+}++/** `preset` with prop `key` of its effect at `index` set to `value`. */+export function setShaderEffectProp(+ preset: ShaderPreset,+ index: number,+ key: string,+ value: unknown+): ShaderPreset {+ return {+ ...preset,+ components: preset.components.map((component, at) =>+ at === index ? { ...component, props: { ...component.props, [key]: value } } : component+ )+ }+}++/** A preset written out as JSON, as shaders.com exports it. */+export function shaderPresetJSON(preset: ShaderPreset): string {+ return JSON.stringify(preset, null, 2)+}++/** The preset `text` describes, or null when it is not one: an export from shaders.com, say. */+export function parseShaderPreset(text: string): ShaderPreset | null {+ const parsed = v.safeParse(v.pipe(v.string(), v.parseJson(), shaderPresetSchema), text)+ return parsed.success ? parsed.output : null+}diff --git a/packages/vue/src/controls/shader/preview.ts b/packages/vue/src/controls/shader/preview.tsnew file mode 100644index 000000000..e0ef9b46f--- /dev/null+++ b/packages/vue/src/controls/shader/preview.ts@@ -0,0 +1,47 @@+import type { ShaderPreset } from '@open-pencil/scene-graph'++import { createShaderRasterizer } from '#vue/canvas/surface/shader-rasterizer'++import type { ShaderEffect } from './catalog'++/** A preview's size in CSS pixels, as a picker row shows it. */+const PREVIEW_SIZE = { width: 48, height: 32 }+/** What a filter is previewed over: it changes what is beneath it, which shows best on edges. */+const PREVIEW_BASE = { type: 'Voronoi' }++const previews = new Map<string, Promise<string | null>>()+let rasterizer: ReturnType<typeof createShaderRasterizer> | undefined+let queue: Promise<unknown> = Promise.resolve()++function previewPreset(effect: ShaderEffect): ShaderPreset {+ return {+ components: effect.filter ? [PREVIEW_BASE, { type: effect.name }] : [{ type: effect.name }]+ }+}++/**+ * A small still of `effect` with its default props, as an object URL for an `<img>`; null where+ * shaders cannot be drawn. Previews are drawn one at a time, the first time each is asked for,+ * and kept for the session.+ */+export function shaderEffectPreview(effect: ShaderEffect): Promise<string | null> {+ const known = previews.get(effect.name)+ if (known) return known+ rasterizer ??= createShaderRasterizer()+ const active = rasterizer+ const ratio = window.devicePixelRatio || 1+ const drawn = queue.then(async () => {+ const bytes = await active?.render(previewPreset(effect), {+ width: PREVIEW_SIZE.width * ratio,+ height: PREVIEW_SIZE.height * ratio+ })+ return bytes+ ? URL.createObjectURL(new Blob([new Uint8Array(bytes)], { type: 'image/png' }))+ : null+ })+ // A preview that fails to draw leaves the rest of the queue to go on.+ queue = drawn.catch(() => null)+ const url = drawn.catch(() => null)+ previews.set(effect.name, url)+ return url+}diff --git a/packages/vue/src/i18n/locales/de/panels.json b/packages/vue/src/i18n/locales/de/panels.jsonindex b9dd1e09f..0eb780f3d 100644--- a/packages/vue/src/i18n/locales/de/panels.json+++ b/packages/vue/src/i18n/locales/de/panels.json@@ -98,6 +98,15 @@"linearGradient": "Linear","radialGradient": "Radial","image": "Bild",+ "shader": "Shader",+ "addShaderEffect": "Effekt hinzufügen",+ "searchShaderEffects": "Effekte suchen",+ "noShaderEffects": "Keine Effekte gefunden",+ "removeShaderEffect": "Effekt entfernen",+ "shaderPreset": "Preset-JSON",+ "applyShaderPreset": "Anwenden",+ "invalidShaderPreset": "Kein Shaders-Preset",+ "shaderNeedsWebGPU": "Dieser Browser hat kein WebGPU, daher zeigen Shader ihr gespeichertes Bild.","stops": "Stopps","addStop": "Stopp hinzufügen","alignLeft": "Links ausrichten",diff --git a/packages/vue/src/i18n/locales/es/panels.json b/packages/vue/src/i18n/locales/es/panels.jsonindex 991f6a262..c3784a6a3 100644--- a/packages/vue/src/i18n/locales/es/panels.json+++ b/packages/vue/src/i18n/locales/es/panels.json@@ -113,6 +113,15 @@"linearGradient": "Lineal","radialGradient": "Radial","image": "Imagen",+ "shader": "Shader",+ "addShaderEffect": "Añadir efecto",+ "searchShaderEffects": "Buscar efectos",+ "noShaderEffects": "No se encontraron efectos",+ "removeShaderEffect": "Quitar efecto",+ "shaderPreset": "JSON del preset",+ "applyShaderPreset": "Aplicar",+ "invalidShaderPreset": "No es un preset de shaders",+ "shaderNeedsWebGPU": "Este navegador no tiene WebGPU, así que los shaders muestran su fotograma guardado.","stops": "Paradas","addStop": "Añadir parada","alignLeft": "Alinear a la izquierda",diff --git a/packages/vue/src/i18n/locales/fr/panels.json b/packages/vue/src/i18n/locales/fr/panels.jsonindex d29380244..b8fed5c5a 100644--- a/packages/vue/src/i18n/locales/fr/panels.json+++ b/packages/vue/src/i18n/locales/fr/panels.json@@ -98,6 +98,15 @@"linearGradient": "Linéaire","radialGradient": "Radial","image": "Image",+ "shader": "Shader",+ "addShaderEffect": "Ajouter un effet",+ "searchShaderEffects": "Rechercher des effets",+ "noShaderEffects": "Aucun effet trouvé",+ "removeShaderEffect": "Supprimer l’effet",+ "shaderPreset": "JSON du preset",+ "applyShaderPreset": "Appliquer",+ "invalidShaderPreset": "Ce n’est pas un preset shaders",+ "shaderNeedsWebGPU": "Ce navigateur n’a pas WebGPU : les shaders affichent leur image enregistrée.","stops": "Arrêts","addStop": "Ajouter un arrêt","alignLeft": "Aligner à gauche",diff --git a/packages/vue/src/i18n/locales/it/panels.json b/packages/vue/src/i18n/locales/it/panels.jsonindex d9269d23c..de53cd103 100644--- a/packages/vue/src/i18n/locales/it/panels.json+++ b/packages/vue/src/i18n/locales/it/panels.json@@ -98,6 +98,15 @@"linearGradient": "Lineare","radialGradient": "Radiale","image": "Immagine",+ "shader": "Shader",+ "addShaderEffect": "Aggiungi effetto",+ "searchShaderEffects": "Cerca effetti",+ "noShaderEffects": "Nessun effetto trovato",+ "removeShaderEffect": "Rimuovi effetto",+ "shaderPreset": "JSON del preset",+ "applyShaderPreset": "Applica",+ "invalidShaderPreset": "Non è un preset di shaders",+ "shaderNeedsWebGPU": "Questo browser non ha WebGPU, quindi gli shader mostrano il fotogramma salvato.","stops": "Punti","addStop": "Aggiungi punto","alignLeft": "Allinea a sinistra",diff --git a/packages/vue/src/i18n/locales/ja/panels.json b/packages/vue/src/i18n/locales/ja/panels.jsonindex a11fc6fb7..bf0ce23ee 100644--- a/packages/vue/src/i18n/locales/ja/panels.json+++ b/packages/vue/src/i18n/locales/ja/panels.json@@ -113,6 +113,15 @@"linearGradient": "線形グラデーション","radialGradient": "放射状グラデーション","image": "画像",+ "shader": "シェーダー",+ "addShaderEffect": "エフェクトを追加",+ "searchShaderEffects": "エフェクトを検索",+ "noShaderEffects": "エフェクトが見つかりません",+ "removeShaderEffect": "エフェクトを削除",+ "shaderPreset": "プリセット JSON",+ "applyShaderPreset": "適用",+ "invalidShaderPreset": "shaders のプリセットではありません",+ "shaderNeedsWebGPU": "このブラウザーは WebGPU に対応していないため、シェーダーは保存されたフレームを表示します。","stops": "カラーピッカーの分岐点","addStop": "分岐点を追加","alignLeft": "左揃え",diff --git a/packages/vue/src/i18n/locales/pl/panels.json b/packages/vue/src/i18n/locales/pl/panels.jsonindex b5f4ae017..e0f0f034d 100644--- a/packages/vue/src/i18n/locales/pl/panels.json+++ b/packages/vue/src/i18n/locales/pl/panels.json@@ -98,6 +98,15 @@"linearGradient": "Liniowy","radialGradient": "Promieniowy","image": "Obraz",+ "shader": "Shader",+ "addShaderEffect": "Dodaj efekt",+ "searchShaderEffects": "Szukaj efektów",+ "noShaderEffects": "Nie znaleziono efektów",+ "removeShaderEffect": "Usuń efekt",+ "shaderPreset": "JSON presetu",+ "applyShaderPreset": "Zastosuj",+ "invalidShaderPreset": "To nie jest preset shaders",+ "shaderNeedsWebGPU": "Ta przeglądarka nie obsługuje WebGPU, więc shadery pokazują zapisaną klatkę.","stops": "Punkty","addStop": "Dodaj punkt","alignLeft": "Wyrównaj do lewej",diff --git a/packages/vue/src/i18n/locales/ru/panels.json b/packages/vue/src/i18n/locales/ru/panels.jsonindex d924de45f..e1f19a730 100644--- a/packages/vue/src/i18n/locales/ru/panels.json+++ b/packages/vue/src/i18n/locales/ru/panels.json@@ -112,6 +112,15 @@"linearGradient": "Линейный","radialGradient": "Радиальный","image": "Изображение",+ "shader": "Шейдер",+ "addShaderEffect": "Добавить эффект",+ "searchShaderEffects": "Искать эффекты",+ "noShaderEffects": "Эффекты не найдены",+ "removeShaderEffect": "Удалить эффект",+ "shaderPreset": "JSON пресета",+ "applyShaderPreset": "Применить",+ "invalidShaderPreset": "Это не пресет shaders",+ "shaderNeedsWebGPU": "В этом браузере нет WebGPU, поэтому шейдеры показывают сохранённый кадр.","stops": "Точки","addStop": "Добавить точку","alignCenter": "По центру",diff --git a/packages/vue/src/i18n/locales/zh-cn/panels.json b/packages/vue/src/i18n/locales/zh-cn/panels.jsonindex 291b00f86..0e7278bd2 100644--- a/packages/vue/src/i18n/locales/zh-cn/panels.json+++ b/packages/vue/src/i18n/locales/zh-cn/panels.json@@ -112,6 +112,15 @@"linearGradient": "线性","radialGradient": "径向","image": "图片",+ "shader": "着色器",+ "addShaderEffect": "添加效果",+ "searchShaderEffects": "搜索效果",+ "noShaderEffects": "未找到效果",+ "removeShaderEffect": "移除效果",+ "shaderPreset": "预设 JSON",+ "applyShaderPreset": "应用",+ "invalidShaderPreset": "不是 shaders 预设",+ "shaderNeedsWebGPU": "此浏览器不支持 WebGPU,着色器显示已保存的帧。","stops": "色标","addStop": "添加色标","alignCenter": "居中对齐",diff --git a/packages/vue/src/i18n/messages/panels.ts b/packages/vue/src/i18n/messages/panels.tsindex e3744a507..60e7299a7 100644--- a/packages/vue/src/i18n/messages/panels.ts+++ b/packages/vue/src/i18n/messages/panels.ts@@ -443,6 +443,15 @@ export const panelMessageDefaults = {linearGradient: 'Linear',radialGradient: 'Radial',image: 'Image',+ shader: 'Shader',+ addShaderEffect: 'Add effect',+ searchShaderEffects: 'Search effects',+ noShaderEffects: 'No effects found',+ removeShaderEffect: 'Remove effect',+ shaderPreset: 'Preset JSON',+ applyShaderPreset: 'Apply',+ invalidShaderPreset: 'Not a shaders preset',+ shaderNeedsWebGPU: 'This browser has no WebGPU, so shaders show their saved frame.',stops: 'Stops',addStop: 'Add stop',diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.tsindex 28bd331ab..dc13e2a38 100644--- a/packages/vue/src/index.ts+++ b/packages/vue/src/index.ts@@ -126,6 +126,21 @@ export type {export { useExport } from '#vue/document/export/use'export type { ExportFormatId, ExportFormatOption, ExportSetting } from '#vue/document/export/use'export { useFillControls } from '#vue/controls/fill/use'+export { canDrawShaders, createShaderRasterizer } from '#vue/canvas/surface/shader-rasterizer'+export {+ addShaderEffect,+ DEFAULT_SHADER_EFFECT,+ DEFAULT_SHADER_PRESET,+ loadShaderCatalog,+ moveShaderEffect,+ parseShaderPreset,+ removeShaderEffect,+ setShaderEffectProp,+ shaderEffectLabel,+ shaderEffectPreview,+ shaderPresetJSON+} from '#vue/controls/shader'+export type { ShaderEffect, ShaderPropControl } from '#vue/controls/shader'export { useColorVariableBinding } from '#vue/controls/color-variable-binding/use'export { useNumberVariableBinding } from '#vue/controls/number-variable-binding/use'export type { NumberBindingPath } from '#vue/controls/number-variable-binding/use'@@ -425,7 +440,13 @@ export { ToolbarRoot, ToolbarItem, useToolbar } from '#vue/primitives/Toolbar'export type { ToolbarContext } from '#vue/primitives/Toolbar'/** DOM event helpers for cast-free template bindings. */-export { blurTarget, inputNumberValue, inputValue, selectTarget } from '#vue/shared/dom-events'+export {+ blurTarget,+ inputChecked,+ inputNumberValue,+ inputValue,+ selectTarget+} from '#vue/shared/dom-events'/** Internationalization. */export * from '#vue/i18n'diff --git a/packages/vue/src/shared/dom-events.ts b/packages/vue/src/shared/dom-events.tsindex 3ccfdd0c0..c5481d81d 100644--- a/packages/vue/src/shared/dom-events.ts+++ b/packages/vue/src/shared/dom-events.ts@@ -2,6 +2,10 @@ export function inputValue(e: Event): string {return e.target instanceof HTMLInputElement ? e.target.value : ''}+export function inputChecked(e: Event): boolean {+ return e.target instanceof HTMLInputElement ? e.target.checked : false+}+export function inputNumberValue(e: Event): number {return +inputValue(e)}diff --git a/packages/vue/tests/controls/shader.test.ts b/packages/vue/tests/controls/shader.test.tsnew file mode 100644index 000000000..3286b0568--- /dev/null+++ b/packages/vue/tests/controls/shader.test.ts@@ -0,0 +1,63 @@+import { describe, expect, test } from 'bun:test'++import {+ addShaderEffect,+ loadShaderCatalog,+ moveShaderEffect,+ parseShaderPreset,+ removeShaderEffect,+ setShaderEffectProp,+ shaderPresetJSON+} from '#vue/controls/shader'++const PRESET = { components: [{ type: 'Aurora' }, { type: 'FilmGrain' }] }++describe('shader presets', () => {+ test('stack effects: added on top, moved, removed down to the last one', () => {+ const added = addShaderEffect(PRESET, 'Swirl')+ expect(added.components.map((c) => c.type)).toEqual(['Aurora', 'FilmGrain', 'Swirl'])++ const moved = moveShaderEffect(added, 2, 0)+ expect(moved.components.map((c) => c.type)).toEqual(['Swirl', 'Aurora', 'FilmGrain'])+ expect(moveShaderEffect(added, 0, 5)).toBe(added)++ const one = removeShaderEffect(removeShaderEffect(PRESET, 0), 0)+ expect(one.components.map((c) => c.type)).toEqual(['FilmGrain'])+ })++ test('sets one prop of one effect', () => {+ const next = setShaderEffectProp(PRESET, 1, 'strength', 0.4)+ expect(next.components).toEqual([+ { type: 'Aurora' },+ { type: 'FilmGrain', props: { strength: 0.4 } }+ ])+ expect(PRESET.components[1]).toEqual({ type: 'FilmGrain' })+ })++ test('reads a preset from JSON and rejects anything else', () => {+ expect(parseShaderPreset(shaderPresetJSON(PRESET))).toEqual(PRESET)+ expect(parseShaderPreset('{"components":[]}')).toBeNull()+ expect(parseShaderPreset('not json')).toBeNull()+ })+})++describe('shader catalog', () => {+ test('describes each effect with controls for its props', async () => {+ const catalog = await loadShaderCatalog()+ const aurora = catalog.find((effect) => effect.name === 'Aurora')++ expect(catalog.length).toBeGreaterThan(100)+ expect(aurora?.props.find((prop) => prop.key === 'colorA')).toMatchObject({+ kind: 'color',+ label: 'Color A',+ default: '#a533f8'+ })+ expect(aurora?.props.find((prop) => prop.key === 'curtainCount')).toMatchObject({+ kind: 'range',+ min: 1,+ max: 4,+ step: 1+ })+ expect(aurora?.props.find((prop) => prop.key === 'center')).toMatchObject({ kind: 'position' })+ })+})diff --git a/skills/open-pencil/references/design-authoring.md b/skills/open-pencil/references/design-authoring.mdindex c9197f51f..2d4945160 100644--- a/skills/open-pencil/references/design-authoring.md+++ b/skills/open-pencil/references/design-authoring.md@@ -20,6 +20,7 @@ This reference describes scene creation, not React DOM output. Use the `render`## Paint, text, and artwork- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`. Without a `transform` a linear gradient runs left to right and the others are centred; `transform` is Figma's `gradientTransform`, mapping the layer's unit square onto gradient space.+- `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. The canvas shows the shader's first frame, preview plays it, and files and other formats keep that frame as an image.- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow` takes a CSS `box-shadow` list, such as `shadow="0 4 8 #0002, inset 0 1 0 #fff"`, and `blur` a layer blur radius; both are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning.@@ -88,6 +89,6 @@ Generated from the renderer metadata. This inventory lists accepted names, not a**Elements:** `Frame`, `Text`, `Rectangle`, `Ellipse`, `Line`, `Star`, `Polygon`, `Vector`, `Group`, `Section`, `Component`, `ComponentSet`, `Instance`, `View`, `Rect`, `Icon`, `Button.Root`, `Toggle.Root`, `Switch.Root`, `Switch.Thumb`, `Checkbox.Root`, `Checkbox.Indicator`, `RadioGroup.Root`, `RadioGroup.Item`, `RadioGroup.Indicator`, `ToggleGroup.Root`, `ToggleGroup.Item`, `Slider.Root`, `Slider.Track`, `Slider.Range`, `Slider.Thumb`, `Progress.Root`, `Progress.Track`, `Progress.Indicator`, `Tabs.Root`, `Tabs.List`, `Tabs.Trigger`, `Tabs.Content`, `Collapsible.Root`, `Collapsible.Trigger`, `Collapsible.Content`, `Accordion.Root`, `Accordion.Item`, `Accordion.Header`, `Accordion.Trigger`, `Accordion.Content`, `NumberField.Root`, `NumberField.Input`, `NumberField.Increment`, `NumberField.Decrement`, `TextField.Root`, `TextField.Input`, `Textarea.Root`, `Textarea.Input`.-**Helpers:** `solid`, `gradient`, `linearGradient`, `radialGradient`, `angularGradient`, `diamondGradient`, `dropShadow`, `innerShadow`, `layerBlur`, `backgroundBlur`, `foregroundBlur`, `designVar`, `defineVars`.+**Helpers:** `solid`, `gradient`, `linearGradient`, `radialGradient`, `angularGradient`, `diamondGradient`, `shader`, `dropShadow`, `innerShadow`, `layerBlur`, `backgroundBlur`, `foregroundBlur`, `designVar`, `defineVars`.**Properties:** `name`, `key`, `flex`, `flow`, `dir`, `gap`, `wrap`, `rowGap`, `columnGap`, `justify`, `justifyContent`, `items`, `align`, `alignItems`, `grow`, `w`, `h`, `width`, `height`, `minW`, `maxW`, `minH`, `maxH`, `x`, `y`, `top`, `left`, `position`, `constraints`, `p`, `padding`, `px`, `py`, `pt`, `pr`, `pb`, `pl`, `bg`, `fill`, `fills`, `background`, `backgroundColor`, `stroke`, `border`, `borderColor`, `strokeWidth`, `borderWidth`, `strokeAlign`, `strokeDash`, `strokeCap`, `strokeJoin`, `dashPattern`, `strokes`, `strokeWeights`, `rounded`, `borderRadius`, `roundedTL`, `roundedTR`, `roundedBL`, `roundedBR`, `cornerRadius`, `cornerSmoothing`, `opacity`, `blendMode`, `rotate`, `rotation`, `overflow`, `mask`, `visible`, `locked`, `shadow`, `blur`, `effects`, `size`, `fontSize`, `font`, `fontFamily`, `weight`, `fontWeight`, `italic`, `color`, `text`, `characters`, `content`, `value`, `title`, `textAlign`, `textAlignHorizontal`, `textHorizontalAlignment`, `textAlignVertical`, `textVerticalAlignment`, `textAutoResize`, `lineHeight`, `letterSpacing`, `textDecoration`, `textCase`, `maxLines`, `truncate`, `grid`, `columns`, `rows`, `colStart`, `rowStart`, `col`, `row`, `colSpan`, `rowSpan`, `points`, `pointCount`, `innerRadius`, `label`, `style`, `bind`, `component`, `componentId`, `properties`, `propertyRefs`, `of`, `modelValue`, `open`, `disabled`, `filled`, `states`, `min`, `max`, `step`, `defaultValue`.\ No newline at end of filediff --git a/src/app/editor/preparation/types.ts b/src/app/editor/preparation/types.tsindex bc479a2f5..7966b579b 100644--- a/src/app/editor/preparation/types.ts+++ b/src/app/editor/preparation/types.ts@@ -20,6 +20,7 @@ export const EDITOR_PREPARATION_PHASES = ['resolving-fonts','resolving-fallbacks','layout',+ 'drawing-shaders','preparing-render'] as const@@ -28,7 +29,7 @@ export type EditorPreparationPhase = (typeof EDITOR_PREPARATION_PHASES)[number]export interface EditorPreparationProgress {completed: numbertotal: number- unit: 'bytes' | 'nodes' | 'fonts' | 'pages'+ unit: 'bytes' | 'nodes' | 'fonts' | 'pages' | 'shaders'}export interface EditorPreparation {@@ -92,3 +93,11 @@ export interface EditorPreparationHandle {fail(failure: Omit<EditorPreparationFailure, 'id' | 'kind'>): voidcancel(reason?: EditorPreparationCancelReason): void}++/** The unit a page's preparation counts in, for the phases that count anything. */+export function pageProgressUnit(phase: string): EditorPreparationProgress['unit'] | undefined {+ if (phase === 'resolving-fonts') return 'fonts'+ if (phase === 'drawing-shaders') return 'shaders'+ if (phase === 'populating-page') return 'pages'+ return undefined+}diff --git a/src/app/editor/session/create.ts b/src/app/editor/session/create.tsindex 465c523e4..a86d559bd 100644--- a/src/app/editor/session/create.ts+++ b/src/app/editor/session/create.ts@@ -3,6 +3,7 @@ import { shallowReactive } from 'vue'import { createEditor } from '@open-pencil/core/editor'import { BUILTIN_IO_FORMATS, IORegistry } from '@open-pencil/core/io'import { SceneGraph } from '@open-pencil/scene-graph'+import { createShaderRasterizer } from '@open-pencil/vue'import { recordPreparationOutcome } from '@/app/diagnostics'import {@@ -21,7 +22,7 @@ import {type EditorPreparationEventName,type EditorPreparationEvents} from '@/app/editor/preparation/events'-import type { EditorPreparationHandle } from '@/app/editor/preparation/types'+import { pageProgressUnit, type EditorPreparationHandle } from '@/app/editor/preparation/types'import {createEditorComputedRefs,createEditorStoreModules,@@ -47,6 +48,7 @@ function buildEditorStore(initialGraph?: SceneGraph) {graph,state,loadFont,+ shaderRasterizer: createShaderRasterizer(),resolveFigmaClipboardImages: IS_TAURI ? resolveFigmaClipboardImages : undefined,skipInitialGraphSetup: !!initialGraph,getViewportSize: () =>@@ -135,12 +137,6 @@ function buildEditorStore(initialGraph?: SceneGraph) {const panes = createCanvasPaneRegistry(state)- function progressUnit(phase: string): 'fonts' | 'pages' | undefined {- if (phase === 'resolving-fonts') return 'fonts'- if (phase === 'populating-page') return 'pages'- return undefined- }-async function switchPage(pageId: string,options: {@@ -169,7 +165,7 @@ function buildEditorStore(initialGraph?: SceneGraph) {options.onProgress?.(progress)preparation.update({...progress,- unit: progressUnit(progress.phase)+ unit: pageProgressUnit(progress.phase)})}})diff --git a/src/components/fill-picker/FillPicker.vue b/src/components/fill-picker/FillPicker.vueindex a06d690cb..55e356803 100644--- a/src/components/fill-picker/FillPicker.vue+++ b/src/components/fill-picker/FillPicker.vue@@ -3,13 +3,20 @@ import { PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'rekaimport type { FocusOutsideEvent, PointerDownOutsideEvent } from 'reka-ui'import { tv } from 'tailwind-variants'-import type { Fill } from '@open-pencil/scene-graph'-import { applySolidFillColor, FillRoot, useI18n, useRetainedPopup } from '@open-pencil/vue'+import type { Fill, ShaderPaint, ShaderPreset } from '@open-pencil/scene-graph'+import {+ applySolidFillColor,+ DEFAULT_SHADER_PRESET,+ FillRoot,+ useI18n,+ useRetainedPopup+} from '@open-pencil/vue'import type { OkHCLControls } from '@open-pencil/vue'import ColorPickerPanel from '@/components/color-picker-panel/ColorPickerPanel.vue'import GradientEditor from '@/components/fill-picker/GradientEditor.vue'import ImageFillPicker from '@/components/fill-picker/ImageFillPicker.vue'+import ShaderFillPicker from '@/components/fill-picker/shader/ShaderFillPicker.vue'import { usePopoverUI } from '@/components/ui/overlay/popover'import Tip from '@/components/ui/overlay/Tip.vue'import FillSwatch from '@/components/ui/paint/FillSwatch.vue'@@ -25,9 +32,12 @@ const {fill,okhcl = null,swatchBackground,- keepOpen+ keepOpen,+ shader = null} = defineProps<{fill: Fill+ /** The shader the paint draws, when it is a shader's frame; the picker then edits the shader. */+ shader?: ShaderPaint | nullokhcl?: OkHCLControls | nullswatchBackground?: string/** Names the trigger for a paint that is not a fill, such as a stroke. */@@ -38,6 +48,7 @@ const {const activeStop = defineModel<number>('activeStop')const emit = defineEmits<{update: [fill: Fill]+ updateShader: [preset: ShaderPreset]openChange: [open: boolean]cancel: []}>()@@ -45,7 +56,9 @@ const { open: popupOpen, portalActive } = useRetainedPopup(undefined, () => {emit('cancel')emit('openChange', false)})-const cls = usePopoverUI({ content: 'w-60 p-2' })+const cls = usePopoverUI({+ content: 'max-h-(--reka-popover-content-available-height) w-60 overflow-y-auto p-2'+})const { panels } = useI18n()/** Dragging a gradient handle on the canvas leaves the picker open, as in Figma. */@@ -119,8 +132,8 @@ function cancelFromEscape(event: KeyboardEvent) {</Tip><Tip :label="panels.image"><button- :data-active="root.category === 'IMAGE' || undefined"- :class="tabClass(root.category === 'IMAGE')"+ :data-active="(root.category === 'IMAGE' && !shader) || undefined"+ :class="tabClass(root.category === 'IMAGE' && !shader)":aria-label="panels.image"data-test-id="fill-picker-tab-image"@click="root.actions.toImage"@@ -128,6 +141,17 @@ function cancelFromEscape(event: KeyboardEvent) {<icon-lucide-image class="size-3.5" /></button></Tip>+ <Tip :label="panels.shader">+ <button+ :data-active="shader !== null || undefined"+ :class="tabClass(shader !== null)"+ :aria-label="panels.shader"+ data-test-id="fill-picker-tab-shader"+ @click="shader || emit('updateShader', DEFAULT_SHADER_PRESET)"+ >+ <icon-lucide-sparkles class="size-3.5" />+ </button>+ </Tip></div><ColorPickerPanel@@ -144,8 +168,14 @@ function cancelFromEscape(event: KeyboardEvent) {@update="emit('update', $event)"/>+ <ShaderFillPicker+ v-if="shader"+ :preset="shader.preset"+ @update="emit('updateShader', $event)"+ />+<ImageFillPicker- v-if="root.category === 'IMAGE'"+ v-else-if="root.category === 'IMAGE'":fill="root.fill"@update="emit('update', $event)"/>diff --git a/src/components/fill-picker/shader/ShaderEffectControls.vue b/src/components/fill-picker/shader/ShaderEffectControls.vuenew file mode 100644index 000000000..4a6761c9f--- /dev/null+++ b/src/components/fill-picker/shader/ShaderEffectControls.vue@@ -0,0 +1,149 @@+<script setup lang="ts">+import { computed } from 'vue'++import type { ShaderComponent } from '@open-pencil/scene-graph'+import { colorToHexRaw, parseColor } from '@open-pencil/scene-graph/color'+import type { Color, Vector } from '@open-pencil/scene-graph/primitives'+import { inputChecked, useI18n, type ShaderEffect, type ShaderPropControl } from '@open-pencil/vue'++import ColorInput from '@/components/ColorPicker/ColorInput.vue'+import NumberField from '@/components/inputs/NumberField.vue'+import Tip from '@/components/ui/overlay/Tip.vue'+import PanelFieldGroup from '@/components/ui/panel/PanelFieldGroup.vue'+import PanelGrid from '@/components/ui/panel/PanelGrid.vue'+import AppSelect from '@/components/ui/select/AppSelect.vue'++/**+ * The settings of one effect of a shader, laid out as the properties panel lays out an effect's:+ * numbers two to a row, colors as color inputs, choices as selects, generated from the props the+ * `shaders` library describes. Props it has no control for are edited in the preset's JSON.+ */+const { effect, component } = defineProps<{+ effect: ShaderEffect+ component: ShaderComponent+}>()+const emit = defineEmits<{ set: [key: string, value: unknown] }>()+const { panels } = useI18n()++/** A run of numbers, which pair up in a grid, or one other prop. */+type Block = { key: string; numbers: ShaderPropControl[] } | ShaderPropControl++/** The props in the library's order, with runs of numbers together. */+const blocks = computed(() =>+ effect.props.reduce<Block[]>((list, prop) => {+ const last = list.at(-1)+ if (prop.kind !== 'range') list.push(prop)+ else if (last && 'numbers' in last) last.numbers.push(prop)+ else list.push({ key: prop.key, numbers: [prop] })+ return list+ }, [])+)++function value(prop: ShaderPropControl): unknown {+ return component.props?.[prop.key] ?? prop.default+}++function numberValue(prop: ShaderPropControl): number {+ const current = value(prop)+ return typeof current === 'number' ? current : 0+}++function colorValue(prop: ShaderPropControl): Color {+ const current = value(prop)+ return parseColor(typeof current === 'string' ? current : '#000000')+}++/** A position as percentages of the layer, as the library's position control shows it. */+function position(prop: ShaderPropControl): Vector {+ const current = value(prop)+ if (typeof current !== 'object' || current === null) return { x: 50, y: 50 }+ const x = 'x' in current && typeof current.x === 'number' ? current.x : 0.5+ const y = 'y' in current && typeof current.y === 'number' ? current.y : 0.5+ return { x: Math.round(x * 100), y: Math.round(y * 100) }+}++function setPosition(prop: ShaderPropControl, axis: 'x' | 'y', percent: number) {+ const { x, y } = position(prop)+ emit('set', prop.key, { x: x / 100, y: y / 100, [axis]: percent / 100 })+}++function selectValue(prop: ShaderPropControl): string | number {+ const current = value(prop)+ return typeof current === 'string' || typeof current === 'number' ? current : ''+}+</script>++<template>+ <div class="flex flex-col gap-1.5" data-slot="shader-effect-settings">+ <template v-for="block in blocks" :key="block.key">+ <PanelGrid v-if="'numbers' in block" :columns="2">+ <PanelFieldGroup v-for="prop in block.numbers" :key="prop.key" :label="prop.label">+ <NumberField+ v-if="prop.kind === 'range'"+ :model-value="numberValue(prop)"+ :min="prop.min"+ :max="prop.max"+ :step="prop.step"+ :aria-label="prop.label"+ :data-property="`shader-${prop.key}`"+ @commit="emit('set', prop.key, $event)"+ />+ </PanelFieldGroup>+ </PanelGrid>+ <PanelFieldGroup v-else-if="block.kind === 'color'" :label="block.label">+ <ColorInput+ :color="colorValue(block)"+ editable+ :data-property="`shader-${block.key}`"+ @update="emit('set', block.key, `#${colorToHexRaw($event)}`)"+ />+ </PanelFieldGroup>+ <PanelFieldGroup v-else-if="block.kind === 'select'" :label="block.label">+ <AppSelect+ :model-value="selectValue(block)"+ :options="block.options"+ :label="block.label"+ :data-property="`shader-${block.key}`"+ @update:model-value="emit('set', block.key, $event)"+ />+ </PanelFieldGroup>+ <label+ v-else-if="block.kind === 'checkbox'"+ class="flex cursor-pointer items-center gap-2 text-xs text-surface"+ >+ <input+ type="checkbox"+ class="accent-accent"+ :checked="value(block) === true"+ :data-property="`shader-${block.key}`"+ @change="emit('set', block.key, inputChecked($event))"+ />+ {{ block.label }}+ </label>+ <PanelFieldGroup v-else-if="block.kind === 'position'" :label="block.label">+ <div class="flex items-center gap-1.5">+ <Tip :label="panels.xAxis">+ <NumberField+ icon="X"+ suffix="%"+ :model-value="position(block).x"+ :min="0"+ :max="100"+ @commit="setPosition(block, 'x', $event)"+ />+ </Tip>+ <Tip :label="panels.yAxis">+ <NumberField+ icon="Y"+ suffix="%"+ :model-value="position(block).y"+ :min="0"+ :max="100"+ @commit="setPosition(block, 'y', $event)"+ />+ </Tip>+ </div>+ </PanelFieldGroup>+ </template>+ </div>+</template>diff --git a/src/components/fill-picker/shader/ShaderEffectThumb.vue b/src/components/fill-picker/shader/ShaderEffectThumb.vuenew file mode 100644index 000000000..6bfaff668--- /dev/null+++ b/src/components/fill-picker/shader/ShaderEffectThumb.vue@@ -0,0 +1,30 @@+<script setup lang="ts">+import { useIntersectionObserver } from '@vueuse/core'+import { ref, useTemplateRef } from 'vue'++import { shaderEffectPreview, type ShaderEffect } from '@open-pencil/vue'++/** A small still of an effect, drawn the first time its row scrolls into view. */+const { effect } = defineProps<{ effect?: ShaderEffect }>()++const root = useTemplateRef<HTMLElement>('root')+const src = ref<string | null>(null)++const { stop } = useIntersectionObserver(root, ([entry]) => {+ if (!entry?.isIntersecting || !effect) return+ stop()+ void shaderEffectPreview(effect).then((url) => {+ src.value = url+ return url+ })+})+</script>++<template>+ <span+ ref="root"+ class="block h-8 w-12 shrink-0 overflow-hidden rounded border border-border bg-input"+ >+ <img v-if="src" :src="src" alt="" class="size-full object-cover" />+ </span>+</template>diff --git a/src/components/fill-picker/shader/ShaderFillPicker.vue b/src/components/fill-picker/shader/ShaderFillPicker.vuenew file mode 100644index 000000000..414f03923--- /dev/null+++ b/src/components/fill-picker/shader/ShaderFillPicker.vue@@ -0,0 +1,246 @@+<script setup lang="ts">+import { computed, onMounted, ref, shallowRef, type ComponentPublicInstance } from 'vue'++import type { ShaderPreset } from '@open-pencil/scene-graph'+import {+ addShaderEffect,+ canDrawShaders,+ DEFAULT_SHADER_EFFECT,+ loadShaderCatalog,+ moveShaderEffect,+ parseShaderPreset,+ removeShaderEffect,+ setShaderEffectProp,+ shaderEffectLabel,+ shaderPresetJSON,+ useFlatReorderDrag,+ useI18n,+ type ShaderEffect+} from '@open-pencil/vue'++import ShaderEffectControls from '@/components/fill-picker/shader/ShaderEffectControls.vue'+import ShaderEffectThumb from '@/components/fill-picker/shader/ShaderEffectThumb.vue'+import AppButton from '@/components/ui/button/AppButton.vue'+import IconButton from '@/components/ui/button/IconButton.vue'+import AppAlert from '@/components/ui/feedback/AppAlert.vue'+import AppTextarea from '@/components/ui/input/AppTextarea.vue'+import Tip from '@/components/ui/overlay/Tip.vue'+import PanelItemRow from '@/components/ui/panel/PanelItemRow.vue'+import AppCombobox from '@/components/ui/select/AppCombobox.vue'++/**+ * Edits a shader paint's preset as the properties panel edits effects: a list of the shader's+ * effects, top first, each with its type and settings, reordered by dragging; and the preset as+ * JSON, which also takes a preset copied from shaders.com.+ */+const { preset } = defineProps<{ preset: ShaderPreset }>()+const emit = defineEmits<{ update: [preset: ShaderPreset] }>()+const { panels, common } = useI18n()++const catalog = shallowRef<ShaderEffect[]>([])+onMounted(async () => {+ catalog.value = await loadShaderCatalog()+})+const effectsByName = computed(() => new Map(catalog.value.map((effect) => [effect.name, effect])))++const effectOptions = computed(() =>+ catalog.value.map((effect) => ({+ value: effect.name,+ label: shaderEffectLabel(effect.name),+ group: effect.category,+ description: effect.description+ }))+)++/** The effects top first, as the panel lists layers: the last one drawn is on top. */+const rows = computed(() =>+ preset.components+ .map((component, index) => ({+ id: String(index),+ index,+ component,+ effect: catalog.value.find((effect) => effect.name === component.type)+ }))+ .reverse()+)++const expanded = ref<number | null>(null)++function add() {+ emit('update', addShaderEffect(preset, DEFAULT_SHADER_EFFECT))+ expanded.value = preset.components.length+}++function remove(index: number) {+ emit('update', removeShaderEffect(preset, index))+ expanded.value = null+}++/** Changing an effect's type starts it from the new effect's own defaults. */+function setType(index: number, type: string) {+ if (preset.components[index]?.type === type) return+ emit('update', {+ ...preset,+ components: preset.components.map((component, at) => (at === index ? { type } : component))+ })+}++const reorder = useFlatReorderDrag({+ items: () => rows.value,+ // Rows run top first, so a row's place counts from the end of the preset's list.+ onMove: (id, target) => {+ const from = Number(id)+ emit('update', moveShaderEffect(preset, from, preset.components.length - 1 - target))+ expanded.value = null+ }+})++function setupRow(element: Element | ComponentPublicInstance | null, id: string) {+ reorder.setupItem(element instanceof HTMLElement ? element : null, () => ({ id }))+}++function dropPosition(id: string) {+ if (reorder.instructionTargetId.value !== id) return undefined+ return reorder.instruction.value?.operation === 'reorder-before' ? 'before' : 'after'+}++const editingJSON = ref(false)+const json = ref('')+const invalid = ref(false)+function toggleJSON() {+ editingJSON.value = !editingJSON.value+ json.value = shaderPresetJSON(preset)+ invalid.value = false+}+function applyJSON() {+ const parsed = parseShaderPreset(json.value)+ invalid.value = parsed === null+ if (!parsed) return+ emit('update', parsed)+ expanded.value = null+ editingJSON.value = false+}+</script>++<template>+ <div class="flex flex-col gap-2" data-test-id="shader-fill-picker">+ <AppAlert+ v-if="!canDrawShaders()"+ tone="warning"+ :heading="panels.shaderNeedsWebGPU"+ data-test-id="shader-no-webgpu"+ />++ <div>+ <div class="mb-1 flex items-center justify-between">+ <span class="text-[11px] text-muted">{{ panels.effects }}</span>+ <div class="flex items-center">+ <IconButton+ :label="panels.shaderPreset"+ :data-active="editingJSON || undefined"+ data-test-id="shader-preset-toggle"+ @click="toggleJSON"+ >+ <icon-lucide-braces class="size-3" />+ </IconButton>+ <IconButton :label="panels.addShaderEffect" data-test-id="shader-add-effect" @click="add">+ <icon-lucide-plus class="size-3" />+ </IconButton>+ </div>+ </div>++ <div v-if="editingJSON" class="flex flex-col gap-1.5">+ <AppTextarea v-model="json" :rows="8" class="font-mono text-[11px]" />+ <AppAlert v-if="invalid" tone="error" :heading="panels.invalidShaderPreset" />+ <div class="flex justify-end gap-1.5">+ <AppButton variant="ghost" size="sm" @click="editingJSON = false">+ {{ common.cancel }}+ </AppButton>+ <AppButton size="sm" data-test-id="shader-preset-apply" @click="applyJSON">+ {{ panels.applyShaderPreset }}+ </AppButton>+ </div>+ </div>++ <div v-else class="flex flex-col" data-test-id="shader-effects">+ <div+ v-for="row in rows"+ :key="row.id"+ :ref="(element) => setupRow(element, row.id)"+ class="relative data-[dragging]:opacity-50"+ :data-dragging="reorder.draggingId.value === row.id || undefined"+ :data-shader-effect="row.component.type"+ >+ <div+ v-if="dropPosition(row.id)"+ class="pointer-events-none absolute inset-x-1 z-10 h-0.5 rounded-full bg-accent"+ :class="dropPosition(row.id) === 'before' ? 'top-0' : 'bottom-0'"+ />+ <PanelItemRow>+ <div class="flex min-w-0 flex-1 items-center gap-1.5">+ <Tip+ :label="+ expanded === row.index+ ? panels.collapseEffectSettings+ : panels.expandEffectSettings+ "+ >+ <button+ type="button"+ :aria-expanded="expanded === row.index"+ :aria-label="+ expanded === row.index+ ? panels.collapseEffectSettings+ : panels.expandEffectSettings+ "+ class="flex size-5 shrink-0 cursor-pointer items-center justify-center overflow-hidden rounded border border-border bg-input p-0"+ @click="expanded = expanded === row.index ? null : row.index"+ >+ <icon-lucide-sparkles class="size-3 text-muted" />+ </button>+ </Tip>+ <div class="min-w-0 flex-1">+ <AppCombobox+ :model-value="row.component.type"+ :options="effectOptions"+ :label="panels.shader"+ :search-placeholder="panels.searchShaderEffects"+ :empty-label="panels.noShaderEffects"+ :result-limit="catalog.length"+ :ui="{ content: 'w-64' }"+ @update:model-value="setType(row.index, $event)"+ >+ <template #option="{ option }">+ <ShaderEffectThumb :effect="effectsByName.get(option.value)" />+ <span class="min-w-0 flex-1 truncate text-surface">{{ option.label }}</span>+ </template>+ </AppCombobox>+ </div>+ </div>+ <template #rail="{ removeClass }">+ <Tip :label="panels.removeShaderEffect">+ <IconButton+ :label="panels.removeShaderEffect"+ :disabled="preset.components.length === 1"+ :class="removeClass"+ @click="remove(row.index)"+ >+ <icon-lucide-minus class="size-3.5" />+ </IconButton>+ </Tip>+ </template>+ </PanelItemRow>+ <ShaderEffectControls+ v-if="expanded === row.index && row.effect"+ class="py-2"+ :effect="row.effect"+ :component="row.component"+ @set="+ (key, value) => emit('update', setShaderEffectProp(preset, row.index, key, value))+ "+ />+ </div>+ </div>+ </div>+ </div>+</template>diff --git a/src/components/preparation/presentation.ts b/src/components/preparation/presentation.tsindex 0c8e4bbd5..510658750 100644--- a/src/components/preparation/presentation.ts+++ b/src/components/preparation/presentation.ts@@ -8,6 +8,7 @@ export const preparationPhaseLabels = {'resolving-fonts': 'Resolving fonts','resolving-fallbacks': 'Finalizing typography',layout: 'Computing layout',+ 'drawing-shaders': 'Drawing shaders','preparing-render': 'Preparing canvas'} satisfies Record<EditorPreparation['phase'], string>diff --git a/src/components/properties/FillSection.vue b/src/components/properties/FillSection.vueindex da7a66b9a..4821402f1 100644--- a/src/components/properties/FillSection.vue+++ b/src/components/properties/FillSection.vue@@ -1,5 +1,5 @@<script setup lang="ts">-import type { Fill } from '@open-pencil/scene-graph'+import { shaderOfPaint, type Fill, type SceneNode } from '@open-pencil/scene-graph'import { colorToHexRaw } from '@open-pencil/scene-graph/color'import type { Color } from '@open-pencil/scene-graph/primitives'import {@@ -11,6 +11,7 @@ import {} from '@open-pencil/vue'import type { BindableValueActions } from '@open-pencil/vue'+import { useEditorStore } from '@/app/editor/active-store'import FillPicker from '@/components/fill-picker/FillPicker.vue'import PaintField from '@/components/inputs/PaintField.vue'import VariableBindingPicker from '@/components/properties/binding/VariableBindingPicker.vue'@@ -37,6 +38,7 @@ import PanelSection from '@/components/ui/panel/PanelSection.vue'import AppSelect from '@/components/ui/select/AppSelect.vue'const fillCtx = useFillControls()+const store = useEditorStore()const okhcl = useOkHCL()const gradients = useGradientEditing()@@ -61,6 +63,11 @@ const {update: updateStyle} = useSharedStylePicker('fill')+/** The shader a fill of the layer the panel shows draws, if it draws one. */+function fillShader(node: SceneNode | null, fill: Fill) {+ return node ? shaderOfPaint(node, fill) : null+}+function displayFill(fill: Fill, resolvedColor: Color | undefined): Fill {return fill.type === 'SOLID' && resolvedColor ? { ...fill, color: resolvedColor } : fill}@@ -149,6 +156,7 @@ function updateSolidColor(<FillPicker:fill="displayFill(fill, binding.resolvedValue)":okhcl="createFillOkhclAdapter(okhcl, activeNode, index)"+ :shader="fillShader(activeNode, fill)":keep-open="gradients.pressesHandle":active-stop="gradients.stop(gradients.target(selectedNodeIds, 'fills', index))"@update:active-stop="@@ -159,6 +167,7 @@ function updateSolidColor(actions.update(index, next))"+ @update-shader="store.setShaderPaint(selectedNodeIds, 'fills', index, $event)"@open-change="onPickerOpenChange(selectedNodeIds, index, $event, binding.actions)"@cancel="cancelPaintMutation(binding.actions)"/>@@ -181,7 +190,7 @@ function updateSolidColor("/><span v-else class="min-w-0 flex-1 truncate font-mono text-xs text-surface">- {{ fillLabel(fill) }}+ {{ fillLabel(fill, undefined, fillShader(activeNode, fill)) }}</span></template>diff --git a/src/components/properties/fill-label.ts b/src/components/properties/fill-label.tsindex daa3803e8..07fcff134 100644--- a/src/components/properties/fill-label.ts+++ b/src/components/properties/fill-label.ts@@ -1,8 +1,19 @@-import type { Fill, Variable } from '@open-pencil/scene-graph'+import type { Fill, ShaderPaint, Variable } from '@open-pencil/scene-graph'import { colorToHexRaw } from '@open-pencil/scene-graph/color'+import { shaderEffectLabel } from '@open-pencil/vue'-export function fillLabel(fill: Fill, boundVariable?: Variable): string {+export function fillLabel(+ fill: Fill,+ boundVariable?: Variable,+ shader?: ShaderPaint | null+): string {if (boundVariable) return boundVariable.name+ // A shader is named by its effects, top first, as its picker lists them.+ if (shader)+ return shader.preset.components+ .map((component) => shaderEffectLabel(component.type))+ .reverse()+ .join(', ')if (fill.type === 'SOLID') return colorToHexRaw(fill.color)if (fill.type.startsWith('GRADIENT')) return fill.type.replace('GRADIENT_', '')return fill.typediff --git a/tests/e2e/canvas/fill-modes-visual.spec.ts b/tests/e2e/canvas/fill-modes-visual.spec.tsindex d47ffb379..4d653d7c8 100644--- a/tests/e2e/canvas/fill-modes-visual.spec.ts+++ b/tests/e2e/canvas/fill-modes-visual.spec.ts@@ -1,5 +1,7 @@import { readFile } from 'node:fs/promises'+import { shaderPresetKey } from '@open-pencil/scene-graph'+import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'import { readScenePixels } from '#tests/helpers/canvas/pixels'@@ -206,3 +208,83 @@ test('gradients and image fill modes', async () => {await editor.canvas.waitForRender()await expectCanvas('gradients-and-image-fill-modes')})++test('a shader paint draws its saved frame, and its live frame while one plays', async () => {+ const square = async (color: string) =>+ editor.page.evaluate(async (fill) => {+ const canvas = new OffscreenCanvas(32, 32)+ const context = canvas.getContext('2d')+ if (!context) throw new Error('Cannot create frame fixture canvas')+ const gradient = context.createLinearGradient(0, 0, 32, 32)+ gradient.addColorStop(0, fill)+ gradient.addColorStop(1, '#111827')+ context.fillStyle = gradient+ context.fillRect(0, 0, 32, 32)+ return [...new Uint8Array(await (await canvas.convertToBlob()).arrayBuffer())]+ }, color)+ const saved = await square('#f97316')+ const preset = { components: [{ type: 'Aurora' }] }+ const presetKey = shaderPresetKey(preset)+ await editor.page.evaluate(+ ({ frame, preset, presetKey }) => {+ const store = window.openPencil?.getStore?.()+ if (!store) throw new Error('OpenPencil store not initialized')+ const node = store.graph.createNode('RECTANGLE', store.state.currentPageId, {+ name: 'Shader',+ x: 120,+ y: 120,+ width: 160,+ height: 160,+ cornerRadius: 24,+ fills: [{ type: 'SOLID', color: { r: 0, g: 0, b: 0, a: 1 }, visible: true, opacity: 1 }]+ })+ store.setShaderPaint([node.id], 'fills', 0, preset)+ const stored = store.graph.getNode(node.id)+ const paint = stored?.fills[0]+ if (!stored || !paint?.imageHash) throw new Error('No shader paint')+ // A frame saved with the document, drawn at the layer's size, so none is drawn again.+ store.graph.images.set(paint.imageHash, new Uint8Array(frame))+ const entry = stored.pluginData.find((item) => item.key === 'shader')+ if (!entry) throw new Error('No shader entry')+ const value: unknown = JSON.parse(entry.value)+ if (typeof value !== 'object' || value === null) throw new Error('Unreadable shader entry')+ const current = { ...value, frame: { width: 160, height: 160, preset: presetKey } }+ store.graph.updateNode(node.id, {+ pluginData: stored.pluginData.map((item) =>+ item === entry ? { ...item, value: JSON.stringify(current) } : item+ )+ })+ store.clearSelection()+ store.requestRender()+ },+ { frame: saved, preset, presetKey }+ )+ await editor.canvas.waitForRender()+ await expectCanvas('shader-saved-frame')++ const live = await square('#22d3ee')+ await editor.page.evaluate(async (frame) => {+ const store = window.openPencil?.getStore?.()+ if (!store) throw new Error('OpenPencil store not initialized')+ const node = [...store.graph.getAllNodes()].find((item) => item.name === 'Shader')+ const hash = node?.fills[0]?.imageHash+ if (!node || !hash) throw new Error('No shader paint')+ const bitmap = await createImageBitmap(new Blob([new Uint8Array(frame)]))+ for (const renderer of store.canvasRenderers) renderer.setLiveImage(hash, bitmap, [node.id])+ store.requestRender()+ }, live)+ await editor.canvas.waitForRender()+ await expectCanvas('shader-live-frame')++ await editor.page.evaluate(() => {+ const store = window.openPencil?.getStore?.()+ if (!store) throw new Error('OpenPencil store not initialized')+ const node = [...store.graph.getAllNodes()].find((item) => item.name === 'Shader')+ const hash = node?.fills[0]?.imageHash+ if (!node || !hash) throw new Error('No shader paint')+ for (const renderer of store.canvasRenderers) renderer.setLiveImage(hash, null, [node.id])+ store.requestRender()+ })+ await editor.canvas.waitForRender()+ await expectCanvas('shader-saved-frame')+})diff --git a/tests/e2e/canvas/fill-modes-visual.spec.ts-snapshots/shader-live-frame-openpencil-darwin.png b/tests/e2e/canvas/fill-modes-visual.spec.ts-snapshots/shader-live-frame-openpencil-darwin.pngnew file mode 100644index 000000000..6558e47beBinary files /dev/null and b/tests/e2e/canvas/fill-modes-visual.spec.ts-snapshots/shader-live-frame-openpencil-darwin.png differdiff --git a/tests/e2e/canvas/fill-modes-visual.spec.ts-snapshots/shader-saved-frame-openpencil-darwin.png b/tests/e2e/canvas/fill-modes-visual.spec.ts-snapshots/shader-saved-frame-openpencil-darwin.pngnew file mode 100644index 000000000..712883598Binary files /dev/null and b/tests/e2e/canvas/fill-modes-visual.spec.ts-snapshots/shader-saved-frame-openpencil-darwin.png differdiff --git a/tests/engine/render/canvas/renderer-lifecycle.test.ts b/tests/engine/render/canvas/renderer-lifecycle.test.tsindex 4278526ac..68f7d3b2d 100644--- a/tests/engine/render/canvas/renderer-lifecycle.test.ts+++ b/tests/engine/render/canvas/renderer-lifecycle.test.ts@@ -22,6 +22,7 @@ function createRenderer() {textPreparationCache: new TextPreparationCache(),transientPreviews: new Map(),imageCache: createImageCache(),+ liveImages: new Map(),imagePreviews: new ImagePreviewCache(() => undefined),onImagePreviewReady: null,vectorPathCache: new Map(),diff --git a/tests/engine/render/canvas/surface-replacement.test.ts b/tests/engine/render/canvas/surface-replacement.test.tsindex a26beea10..9a5f30db4 100644--- a/tests/engine/render/canvas/surface-replacement.test.ts+++ b/tests/engine/render/canvas/surface-replacement.test.ts@@ -17,12 +17,14 @@ describe('renderer surface replacement', () => {const renderer: Pick<SkiaRenderer,| 'surface'+ | 'liveImages'| 'tiledScene'| 'sceneBackingAllocationFailed'| 'invalidateScenePicture'| 'replaceSurface'> = {surface: previous,+ liveImages: new Map(),tiledScene: asDouble<SkiaRenderer['tiledScene']>({ destroy: mock() }),sceneBackingAllocationFailed: true,invalidateScenePicture: mock(),diff --git a/tests/engine/text/edit-undo.test.ts b/tests/engine/text/edit-undo.test.tsindex 45677411b..646d7dc08 100644--- a/tests/engine/text/edit-undo.test.ts+++ b/tests/engine/text/edit-undo.test.ts@@ -45,6 +45,7 @@ function setup() {},getTextEditor: () => textEditor,getRenderer: () => null,+ getRenderers: () => [],runLayoutForNode: () => undefined,runMutationWithLayout: async (operation) => operation(),getCk: () => null,diff --git a/tools/checks/i18n/mixed-script-baseline.txt b/tools/checks/i18n/mixed-script-baseline.txtindex dc4d23dac..39972d822 100644--- a/tools/checks/i18n/mixed-script-baseline.txt+++ b/tools/checks/i18n/mixed-script-baseline.txt@@ -173,6 +173,9 @@ ja:panels.sizingFillFrja:panels.sizingFixedPxja:panels.xAxisja:panels.yAxis+ja:panels.shaderPreset+ja:panels.invalidShaderPreset+ja:panels.shaderNeedsWebGPUja:recovery.dialogDescriptionja:rendering.reloadRequiredja:rendering.settingsDescription@@ -397,3 +400,6 @@ zh-cn:variables.expressionzh-cn:variables.expressionHintzh-cn:variables.conditionHintzh-cn:variables.conditionCustom+zh-cn:panels.shaderPreset+zh-cn:panels.invalidShaderPreset+zh-cn:panels.shaderNeedsWebGPUdiff --git a/tools/checks/i18n/translation-baseline.txt b/tools/checks/i18n/translation-baseline.txtindex 8010a491b..0b14aaae7 100644--- a/tools/checks/i18n/translation-baseline.txt+++ b/tools/checks/i18n/translation-baseline.txt@@ -53,6 +53,7 @@ de:panels.behaviourMinde:panels.behaviourMaxde:panels.behaviourStateHoverde:panels.behaviourText+de:panels.shaderde:settings.mcpde:settings.accentOrangede:settings.accentPink@@ -94,6 +95,7 @@ es:panels.radialGradientes:panels.textCaseOriginales:panels.variableses:panels.slots+es:panels.shaderes:rendering.experimentales:settings.generales:settings.mcp@@ -162,6 +164,7 @@ fr:panels.slotDescriptionfr:panels.behaviourMinfr:panels.behaviourMaxfr:panels.behaviourStateFocus+fr:panels.shaderfr:settings.animationsfr:settings.diagnosticsfr:settings.mcp@@ -205,6 +208,7 @@ it:panels.behaviourMinit:panels.behaviourMaxit:panels.behaviourStateHoverit:panels.behaviourStateFocus+it:panels.shaderit:rendering.settingsTitleit:settings.mcpit:settings.media@@ -256,6 +260,7 @@ pl:panels.framePresetCategoryFigmaCommunitypl:panels.framePresetCategoryTabletpl:panels.kerningpl:panels.behaviourMin+pl:panels.shaderpl:settings.mcppl:settings.mediapl:storage.providerAmazonS3