satori 0.33.5 → 0.34.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/dist/canvas-afafb49b.d.ts +16 -0
- package/dist/experimental.cjs +8 -0
- package/dist/experimental.cjs.LEGAL.txt +4 -0
- package/dist/experimental.cjs.map +1 -0
- package/dist/experimental.d.cts +29 -0
- package/dist/experimental.d.ts +29 -0
- package/dist/experimental.js +8 -0
- package/dist/experimental.js.LEGAL.txt +4 -0
- package/dist/experimental.js.map +1 -0
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -175
- package/dist/index.d.ts +2 -175
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/jsx/index.d.cts +2 -2
- package/dist/jsx/index.d.ts +2 -2
- package/dist/jsx/jsx-runtime.d.cts +1 -1
- package/dist/jsx/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime-28d3932e.d.ts +1324 -0
- package/dist/{jsx-runtime-d92ed26c.d.ts → jsx-runtime-9a51f310.d.ts} +7 -0
- package/dist/satori-dd73c891.d.ts +175 -0
- package/dist/standalone.cjs +7 -7
- package/dist/standalone.cjs.map +1 -1
- package/dist/standalone.d.cts +1 -1
- package/dist/standalone.d.ts +1 -1
- package/dist/standalone.js +7 -7
- package/dist/standalone.js.map +1 -1
- package/package.json +12 -1
package/README.md
CHANGED
|
@@ -122,6 +122,55 @@ await satori(
|
|
|
122
122
|
)
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
+
#### Canvas (WebGL, experimental)
|
|
126
|
+
|
|
127
|
+
> **Note:** This is experimental and may change or be removed in any release. It's only available from `satori/experimental`, so it doesn't add to the size of the `satori` import.
|
|
128
|
+
|
|
129
|
+
With `satori/experimental`, you can draw the content of a `<canvas>` with WebGL2 via the `webgl` prop. Satori calls it with a WebGL2 context sized to the canvas, then embeds what you drew as an image:
|
|
130
|
+
|
|
131
|
+
```jsx
|
|
132
|
+
import satori from 'satori/experimental'
|
|
133
|
+
|
|
134
|
+
await satori(
|
|
135
|
+
<canvas
|
|
136
|
+
width={1200}
|
|
137
|
+
height={630}
|
|
138
|
+
style={{ width: '100%', height: '100%' }}
|
|
139
|
+
webgl={(gl, { width, height }) => {
|
|
140
|
+
gl.clearColor(0, 0.5, 1, 1)
|
|
141
|
+
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
142
|
+
// Compile shaders, draw, etc.
|
|
143
|
+
}}
|
|
144
|
+
/>,
|
|
145
|
+
options
|
|
146
|
+
)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Each canvas gets a new WebGL2 context, which is destroyed with everything drawn into it afterwards:
|
|
150
|
+
|
|
151
|
+
- In browsers and Web Workers, Satori uses `OffscreenCanvas`.
|
|
152
|
+
- In Node.js 20.16+, Satori loads the `gl` package from your project, which must be a WebGL2 build of [headless-gl](https://github.com/stackgl/headless-gl) such as an ANGLE + SwiftShader one (e.g. `github:encharm/headless-gl`). It runs shaders on the CPU. If you bundle your server code, keep `gl` external and include its files in the deployment.
|
|
153
|
+
|
|
154
|
+
To provide contexts yourself, e.g. to reuse them across renders, pass a `createWebGLContext(width, height)` option that returns a WebGL2 context. Satori doesn't destroy the contexts it returns.
|
|
155
|
+
|
|
156
|
+
- The `width` and `height` attributes set the drawing buffer size (300×150 by default), like in browsers. The canvas is displayed at its CSS size, keeping that aspect ratio unless both CSS dimensions are set.
|
|
157
|
+
- Before calling `webgl`, Satori binds the default framebuffer, sets the viewport to the whole drawing buffer, and clears it to transparent, like a new canvas. Afterwards, it reads the default framebuffer. The `webgl` callback can be async.
|
|
158
|
+
- The drawing buffer is treated as premultiplied alpha unless the context was created with `premultipliedAlpha: false`.
|
|
159
|
+
- Children of `<canvas>` are fallback content and are not rendered.
|
|
160
|
+
- The `satori` import ignores the `webgl` prop and draws `<canvas>` like any other element.
|
|
161
|
+
|
|
162
|
+
If you use React's JSX types, add the `webgl` prop to them:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import type { WebGLCanvasRenderer } from 'satori/experimental'
|
|
166
|
+
|
|
167
|
+
declare module 'react' {
|
|
168
|
+
interface CanvasHTMLAttributes<T> {
|
|
169
|
+
webgl?: WebGLCanvasRenderer
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
125
174
|
### CSS
|
|
126
175
|
|
|
127
176
|
Satori uses the same Flexbox [layout engine](https://yogalayout.com) as React Native, and it’s **not** a complete CSS implementation. However, it supports a subset of the spec that covers most common CSS features:
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Support for `<canvas webgl={...}>`: Satori gets a WebGL2 context (from
|
|
3
|
+
* `createWebGLContext`, or a default one), runs the element's `webgl`
|
|
4
|
+
* callback, and embeds the resulting drawing buffer as an image.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
interface WebGLCanvasInfo {
|
|
8
|
+
/** Width of the canvas drawing buffer, from the `width` attribute. */
|
|
9
|
+
width: number;
|
|
10
|
+
/** Height of the canvas drawing buffer, from the `height` attribute. */
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
13
|
+
type WebGLCanvasRenderer = (gl: WebGL2RenderingContext, info: WebGLCanvasInfo) => void | Promise<void>;
|
|
14
|
+
type CreateWebGLContext = (width: number, height: number) => WebGL2RenderingContext | null | undefined | Promise<WebGL2RenderingContext | null | undefined>;
|
|
15
|
+
|
|
16
|
+
export { CreateWebGLContext as C, WebGLCanvasInfo as W, WebGLCanvasRenderer as a };
|