@snovakow/greenscreen 0.0.0-stage → 0.6.0

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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +239 -2
  3. package/dist/greenscreen.d.ts +32 -0
  4. package/dist/greenscreen.d.ts.map +1 -0
  5. package/dist/greenscreen.global.js +350 -0
  6. package/dist/greenscreen.global.js.map +1 -0
  7. package/dist/greenscreen.js +250 -0
  8. package/dist/greenscreen.js.map +1 -0
  9. package/dist/greenscreen.min.js +350 -0
  10. package/dist/greenscreen.min.js.map +1 -0
  11. package/dist/index.d.ts +6 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +3 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/pack.d.ts +7 -0
  16. package/dist/pack.d.ts.map +1 -0
  17. package/dist/pack.js +63 -0
  18. package/dist/pack.js.map +1 -0
  19. package/dist/react.d.ts +26 -0
  20. package/dist/react.d.ts.map +1 -0
  21. package/dist/react.js +78 -0
  22. package/dist/react.js.map +1 -0
  23. package/dist/renderer.d.ts +19 -0
  24. package/dist/renderer.d.ts.map +1 -0
  25. package/dist/renderer.js +25 -0
  26. package/dist/renderer.js.map +1 -0
  27. package/dist/settings.d.ts +36 -0
  28. package/dist/settings.d.ts.map +1 -0
  29. package/dist/settings.js +112 -0
  30. package/dist/settings.js.map +1 -0
  31. package/dist/shader.glsl.d.ts +3 -0
  32. package/dist/shader.glsl.d.ts.map +1 -0
  33. package/dist/shader.glsl.js +199 -0
  34. package/dist/shader.glsl.js.map +1 -0
  35. package/dist/shader.wgsl.d.ts +2 -0
  36. package/dist/shader.wgsl.d.ts.map +1 -0
  37. package/dist/shader.wgsl.js +177 -0
  38. package/dist/shader.wgsl.js.map +1 -0
  39. package/dist/source.d.ts +16 -0
  40. package/dist/source.d.ts.map +1 -0
  41. package/dist/source.js +103 -0
  42. package/dist/source.js.map +1 -0
  43. package/dist/three-webgpu.d.ts +20 -0
  44. package/dist/three-webgpu.d.ts.map +1 -0
  45. package/dist/three-webgpu.js +193 -0
  46. package/dist/three-webgpu.js.map +1 -0
  47. package/dist/three.d.ts +27 -0
  48. package/dist/three.d.ts.map +1 -0
  49. package/dist/three.js +81 -0
  50. package/dist/three.js.map +1 -0
  51. package/dist/types.d.ts +4 -0
  52. package/dist/types.d.ts.map +1 -0
  53. package/dist/types.js +4 -0
  54. package/dist/types.js.map +1 -0
  55. package/dist/webgl.d.ts +13 -0
  56. package/dist/webgl.d.ts.map +1 -0
  57. package/dist/webgl.js +183 -0
  58. package/dist/webgl.js.map +1 -0
  59. package/dist/webgpu.d.ts +19 -0
  60. package/dist/webgpu.d.ts.map +1 -0
  61. package/dist/webgpu.js +303 -0
  62. package/dist/webgpu.js.map +1 -0
  63. package/package.json +103 -4
  64. package/src/global.ts +11 -0
  65. package/src/greenscreen.ts +286 -0
  66. package/src/index.ts +5 -0
  67. package/src/pack.ts +73 -0
  68. package/src/react.tsx +126 -0
  69. package/src/renderer.ts +53 -0
  70. package/src/settings.ts +179 -0
  71. package/src/shader.glsl.ts +199 -0
  72. package/src/shader.wgsl.ts +176 -0
  73. package/src/source.ts +105 -0
  74. package/src/three-webgpu.ts +279 -0
  75. package/src/three.ts +114 -0
  76. package/src/types.ts +10 -0
  77. package/src/webgl.ts +191 -0
  78. package/src/webgpu.ts +312 -0
@@ -0,0 +1,286 @@
1
+ import { pack, PACKED_LENGTH } from './pack.ts'
2
+ import type { Renderer, RendererEvents } from './renderer.ts'
3
+ import { DEFAULT_SETTINGS, mergeSettings } from './settings.ts'
4
+ import type { Settings } from './settings.ts'
5
+ import { Feed, warnUnkeyable } from './source.ts'
6
+ import type { BackendName, Canvas, Source } from './types.ts'
7
+ import { WebGLRenderer } from './webgl.ts'
8
+ import {
9
+ currentDevice,
10
+ knownPreference,
11
+ pipelineFor,
12
+ preferredFormat,
13
+ prefersWebGPU,
14
+ requestDevice,
15
+ WebGPURenderer,
16
+ } from './webgpu.ts'
17
+
18
+ // The backend to draw with: 'auto' lets the keyer choose
19
+ export type BackendChoice = 'auto' | BackendName
20
+
21
+ export interface GreenscreenOptions<C extends Canvas = HTMLCanvasElement> {
22
+ // The canvas to draw in. It must be one no context has been taken from. Without one, the keyer makes its own: a
23
+ // canvas element, or an OffscreenCanvas in a worker
24
+ canvas?: C
25
+ // 'auto' by default: WebGPU where the browser has it on the GPU, and WebGL2 elsewhere
26
+ backend?: BackendChoice
27
+ // The settings to start with, over the defaults
28
+ settings?: Readonly<Partial<Settings>>
29
+ // The graphics context or device has been lost, and the canvas no longer has a picture
30
+ onLost?: () => void
31
+ // It's back. The next render draws again
32
+ onRestored?: () => void
33
+ // The source's picture can't be keyed: it's larger than the GPU's textures, or from another site without CORS. Called
34
+ // once for each picture that fails, with what was thrown, and the canvas is left see-through until a picture can be
35
+ // keyed. Without it, the keyer warns in the console
36
+ onError?: (error: unknown) => void
37
+ }
38
+
39
+ // The time between a keyer's own frames, where there are no animation frames to draw on
40
+ const FALLBACK_INTERVAL = 1000 / 60
41
+
42
+ // A chroma keyer: it takes the picture from its source and draws it, keyed, into its canvas, at the picture's own size.
43
+ // The canvas is see-through where the key and the cut take the picture out, with its colour premultiplied by its
44
+ // alpha, and it keeps its picture between renders, so it can be drawn from with drawImage, or put on the page, at any
45
+ // time. Call render() on every frame, or start() for the keyer to call it itself: it draws only when there's something
46
+ // new to draw
47
+ export class Greenscreen<C extends Canvas = HTMLCanvasElement> {
48
+ readonly canvas: C
49
+ // Which backend it draws with, once it has one. It rejects where the backend can't be had
50
+ readonly initialized: Promise<BackendName>
51
+ #renderer: Renderer | null = null
52
+ #settings: Readonly<Settings> = DEFAULT_SETTINGS
53
+ #packed = new Float32Array(PACKED_LENGTH)
54
+ #scratch = new Float32Array(PACKED_LENGTH)
55
+ #dirty = true
56
+ #feed: Feed | null = null
57
+ // Whether the renderer has a picture of the current source to draw
58
+ #uploaded = false
59
+ #ready = false
60
+ #frame = 0
61
+ #loop: { cancel: () => void } | null = null
62
+ #disposed = false
63
+ readonly #onLost?: () => void
64
+ readonly #onRestored?: () => void
65
+ readonly #onError: (error: unknown) => void
66
+
67
+ constructor(source: Source | null = null, options: GreenscreenOptions<C> = {}) {
68
+ // A canvas it makes is the default type's, or an OffscreenCanvas in a worker, which a worker's code types as C
69
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
70
+ this.canvas = options.canvas ?? (createCanvas() as C)
71
+ this.#onLost = options.onLost
72
+ this.#onRestored = options.onRestored
73
+ this.#onError = options.onError ?? warnUnkeyable
74
+ this.set(options.settings ?? {})
75
+ this.source = source
76
+ this.initialized = this.#initialize(options.backend ?? 'auto')
77
+ // Handled here, so a keyer whose backend can't be had doesn't report it to a page that never asked
78
+ this.initialized.catch(() => {})
79
+ }
80
+
81
+ // A WebGL2 keyer has its backend at once. A WebGPU one has it once the device is ready, and an 'auto' one once it's
82
+ // known which to take, both of which warmUp() can see to before the keyer is made. The canvas isn't touched until the
83
+ // backend is known, as a canvas can only ever have one kind of context
84
+ #initialize(choice: BackendChoice): Promise<BackendName> {
85
+ if (choice === 'webgl') return this.#useWebGL()
86
+ if (choice === 'webgpu') return this.#useWebGPU()
87
+ if (choice === 'auto') {
88
+ const known = knownPreference()
89
+ if (known !== null) return known ? this.#useWebGPU() : this.#useWebGL()
90
+ return prefersWebGPU().then((webgpu) => (webgpu ? this.#useWebGPU() : this.#useWebGL()))
91
+ }
92
+ // Only from JavaScript, which the types don't stop
93
+ const name: string = choice
94
+ return Promise.reject(new TypeError(`Greenscreen: no backend called ${name}`))
95
+ }
96
+
97
+ #useWebGL(): Promise<BackendName> {
98
+ if (this.#disposed) return Promise.reject(new Error('Greenscreen: disposed before its backend was ready'))
99
+ try {
100
+ this.#renderer = new WebGLRenderer(this.canvas, this.#events)
101
+ return Promise.resolve('webgl')
102
+ } catch (error) {
103
+ return Promise.reject(error)
104
+ }
105
+ }
106
+
107
+ #useWebGPU(): Promise<BackendName> {
108
+ const start = (device: GPUDevice): BackendName => {
109
+ if (this.#disposed) throw new Error('Greenscreen: disposed before WebGPU was ready')
110
+ this.#renderer = new WebGPURenderer(this.canvas, device, this.#events)
111
+ return 'webgpu'
112
+ }
113
+ // With the device there already, as it is once warmUp() has resolved, the keyer starts at once, and its first
114
+ // render draws, even in the task that made it
115
+ const device = currentDevice()
116
+ if (!device) return requestDevice().then(start)
117
+ try {
118
+ return Promise.resolve(start(device))
119
+ } catch (error) {
120
+ return Promise.reject(error)
121
+ }
122
+ }
123
+
124
+ #events: RendererEvents = {
125
+ lost: () => {
126
+ this.#ready = false
127
+ this.#uploaded = false
128
+ this.#onLost?.()
129
+ },
130
+ restored: () => {
131
+ this.#feed?.invalidate()
132
+ this.#dirty = true
133
+ this.#onRestored?.()
134
+ },
135
+ }
136
+
137
+ get backend(): BackendName | null {
138
+ return this.#renderer?.backend ?? null
139
+ }
140
+
141
+ // Whether the canvas has a picture of the current source, keyed: false until the first is drawn, while the source's
142
+ // picture can't be keyed, while the context is lost, and after dispose()
143
+ get ready(): boolean {
144
+ return this.#ready
145
+ }
146
+
147
+ // How many pictures it has drawn, for whoever draws from the canvas to tell when there's a new one
148
+ get frame(): number {
149
+ return this.#frame
150
+ }
151
+
152
+ // The settings it keys with. Each change makes a new frozen object, so one held stays as it was
153
+ get settings(): Readonly<Settings> {
154
+ return this.#settings
155
+ }
156
+
157
+ get source(): Source | null {
158
+ return this.#feed?.source ?? null
159
+ }
160
+
161
+ // A new source isn't ready until its first picture is drawn
162
+ set source(source: Source | null) {
163
+ if (source === this.source) return
164
+ this.#feed?.dispose()
165
+ this.#feed = source ? new Feed(source) : null
166
+ this.#uploaded = false
167
+ this.#ready = false
168
+ }
169
+
170
+ // Changes the settings named, leaving the rest. The values are copied, corners and all, so changing the object
171
+ // afterwards changes nothing until it's given again
172
+ set(patch: Readonly<Partial<Settings>>): void {
173
+ this.#settings = mergeSettings(this.#settings, patch)
174
+ const packed = pack(this.#settings, this.#scratch)
175
+ if (packed.some((value, i) => value !== this.#packed[i])) {
176
+ this.#scratch = this.#packed
177
+ this.#packed = packed
178
+ this.#dirty = true
179
+ }
180
+ }
181
+
182
+ // Back to the defaults
183
+ reset(): void {
184
+ this.set(DEFAULT_SETTINGS)
185
+ }
186
+
187
+ // Takes the source's picture again on the next render. Needed only for a still source whose pixels change: an
188
+ // ImageBitmap, ImageData or VideoFrame. Videos, images and canvases are watched
189
+ invalidate(): void {
190
+ this.#feed?.invalidate()
191
+ }
192
+
193
+ // Draws the source keyed, if there's something new to draw: a new picture from it, or new settings. Cheap when
194
+ // there's nothing new. Returns whether the canvas has a picture of the source
195
+ render(): boolean {
196
+ const renderer = this.#renderer
197
+ const feed = this.#feed
198
+ if (this.#disposed || !renderer || renderer.lost || !feed) return false
199
+ let draw = this.#dirty
200
+ if (feed.hasPicture() && feed.changed()) {
201
+ const [width, height] = feed.size()
202
+ try {
203
+ renderer.upload(feed.source, width, height)
204
+ feed.taken()
205
+ this.#uploaded = true
206
+ draw = true
207
+ } catch (error) {
208
+ // Nothing is drawn until a picture can be taken, so the canvas doesn't show another source's picture, or an
209
+ // older one of this source's, as if it were this one. The handler is called last, so one that disposes the
210
+ // keyer or gives it another source finds it settled
211
+ this.#uploaded = false
212
+ this.#ready = false
213
+ if (feed.failed()) {
214
+ renderer.clear()
215
+ this.#onError(error)
216
+ }
217
+ return false
218
+ }
219
+ }
220
+ if (!this.#uploaded || !draw) return this.#ready
221
+ renderer.draw(this.#packed)
222
+ this.#dirty = false
223
+ this.#ready = true
224
+ this.#frame += 1
225
+ return true
226
+ }
227
+
228
+ // Renders on every animation frame until stop()
229
+ start(): void {
230
+ if (this.#loop || this.#disposed) return
231
+ let handle = 0
232
+ const step = () => {
233
+ handle = schedule(step)
234
+ this.render()
235
+ }
236
+ handle = schedule(step)
237
+ this.#loop = { cancel: () => unschedule(handle) }
238
+ }
239
+
240
+ stop(): void {
241
+ this.#loop?.cancel()
242
+ this.#loop = null
243
+ }
244
+
245
+ // Frees the context or device's resources at once. The keyer draws no more, and its canvas can't be used again
246
+ dispose(): void {
247
+ if (this.#disposed) return
248
+ this.stop()
249
+ this.#disposed = true
250
+ this.#feed?.dispose()
251
+ this.#feed = null
252
+ this.#renderer?.dispose()
253
+ this.#renderer = null
254
+ this.#ready = false
255
+ }
256
+ }
257
+
258
+ // Gets the backend ready before the first keyer needs it: for 'auto', which backend it is, and for WebGPU, the device
259
+ // and the pipeline, so a keyer made afterwards draws from its first render. Resolves with the backend that 'auto' or the
260
+ // choice comes to. 'auto' always comes to one; WebGPU asked for by name rejects where it can't be had
261
+ export async function warmUp(backend: BackendChoice = 'auto'): Promise<BackendName> {
262
+ const choice = backend === 'auto' ? ((await prefersWebGPU()) ? 'webgpu' : 'webgl') : backend
263
+ if (choice === 'webgpu') pipelineFor(await requestDevice(), preferredFormat())
264
+ return choice
265
+ }
266
+
267
+ function createCanvas(): Canvas {
268
+ if (typeof document !== 'undefined') {
269
+ const canvas = document.createElement('canvas')
270
+ canvas.width = 1
271
+ canvas.height = 1
272
+ return canvas
273
+ }
274
+ if (typeof OffscreenCanvas !== 'undefined') return new OffscreenCanvas(1, 1)
275
+ throw new Error('Greenscreen: there is no canvas to draw in here. Give it one with options.canvas')
276
+ }
277
+
278
+ function schedule(step: () => void): number {
279
+ if (typeof requestAnimationFrame === 'function') return requestAnimationFrame(step)
280
+ return setTimeout(step, FALLBACK_INTERVAL)
281
+ }
282
+
283
+ function unschedule(handle: number) {
284
+ if (typeof cancelAnimationFrame === 'function') cancelAnimationFrame(handle)
285
+ else clearTimeout(handle)
286
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { Greenscreen, warmUp } from './greenscreen.ts'
2
+ export type { BackendChoice, GreenscreenOptions } from './greenscreen.ts'
3
+ export { DEFAULT_SETTINGS, KEY_COLORS, RANGES } from './settings.ts'
4
+ export type { NumericSetting, Point, Range, Settings } from './settings.ts'
5
+ export type { BackendName, Source } from './types.ts'
package/src/pack.ts ADDED
@@ -0,0 +1,73 @@
1
+ import type { Settings } from './settings.ts'
2
+
3
+ // The settings as the shaders read them: eight sets of four numbers, a vec4 array in GLSL and a struct of vec4s in
4
+ // WGSL. Every shader works from these, so none of them can come to mean a setting differently. The first three hold
5
+ // what OBS's filter works from, worked out from the settings as OBS works it out; the rest are the add-ons' and the
6
+ // renderers'
7
+ // 0 key: its Cb and Cr, 1 / its chroma's distance from grey for hueSpill, the filter on
8
+ // 1 matte: similarity, smoothness and spill, each a thousandth of its setting, and opacity
9
+ // 2 correction: gamma's exponent, contrast's factor, brightness, unused
10
+ // 3 add-ons: tint's r, g and b in linear light, saturation
11
+ // 4 flags: flip, texture's rows bottom up, output linear, texture linear. The last three are the renderer's own
12
+ // 5 corners 0 and 1
13
+ // 6 corners 2 and 3
14
+ // 7 switches: crop on, hueSpill on, unused
15
+ export const PACKED_LENGTH = 32
16
+
17
+ // A colour's Cb and Cr from its sRGB values, as OBS's filter has them: BT.709's, in studio range
18
+ export const CB = [-0.100644, -0.338572, 0.439216, 0.501961] as const
19
+ export const CR = [0.439216, -0.398942, -0.040274, 0.501961] as const
20
+
21
+ // Into out, which is PACKED_LENGTH long, and returned
22
+ export function pack<T extends Float32Array>(settings: Readonly<Settings>, out: T): T {
23
+ // The key's chroma comes from its own sRGB values, as OBS takes them from the colour chosen
24
+ const [r, g, b] = channels(settings.keyColor)
25
+ out[0] = CB[0] * r + CB[1] * g + CB[2] * b + CB[3]
26
+ out[1] = CR[0] * r + CR[1] * g + CR[2] * b + CR[3]
27
+ // For hueSpill, the key's direction from grey, scaled to 1. A key on grey has none, and takes nothing out
28
+ out[2] = 1 / Math.max(Math.hypot(out[0] - CB[3], out[1] - CR[3]), 1e-3)
29
+ out[3] = settings.key ? 1 : 0
30
+
31
+ out[4] = settings.similarity / 1000
32
+ out[5] = settings.smoothness / 1000
33
+ out[6] = settings.spill / 1000
34
+ out[7] = settings.opacity
35
+
36
+ // An exponent from 2 down to 1/2, and a factor from 1/5 up to 5, each 1 for no change, as OBS makes them
37
+ const { gamma, contrast } = settings
38
+ out[8] = gamma < 0 ? 1 - gamma : 1 / (1 + gamma)
39
+ out[9] = contrast < 0 ? 1 / (1 - contrast) : 1 + contrast
40
+ out[10] = settings.brightness
41
+ out[11] = 0
42
+
43
+ // The tint multiplies linear colour, so it's made linear too
44
+ const tint = channels(settings.tint)
45
+ out[12] = toLinear(tint[0])
46
+ out[13] = toLinear(tint[1])
47
+ out[14] = toLinear(tint[2])
48
+ out[15] = settings.saturation
49
+
50
+ out[16] = settings.flip ? 1 : 0
51
+ out[17] = 0
52
+ out[18] = 0
53
+ out[19] = 0
54
+
55
+ settings.corners.forEach((corner, i) => {
56
+ out[20 + i * 2] = corner.x
57
+ out[21 + i * 2] = corner.y
58
+ })
59
+ out[28] = settings.crop ? 1 : 0
60
+ out[29] = settings.hueSpill ? 1 : 0
61
+ out.fill(0, 30)
62
+ return out
63
+ }
64
+
65
+ // A 0xrrggbb colour's channels, 0 to 1
66
+ export function channels(hex: number): [number, number, number] {
67
+ return [((hex >> 16) & 0xff) / 255, ((hex >> 8) & 0xff) / 255, (hex & 0xff) / 255]
68
+ }
69
+
70
+ // sRGB's value to linear light
71
+ function toLinear(c: number): number {
72
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
73
+ }
package/src/react.tsx ADDED
@@ -0,0 +1,126 @@
1
+ 'use client'
2
+
3
+ // The keyer is an object outside React that these effects drive, as a canvas or a media element would be, not state
4
+ // to be copied, so setting its source and its canvas's style is what they're for. And this is a library's entry, which
5
+ // exports its hook and constants beside the component, as fast refresh, which wants components alone, isn't for it
6
+ /* oxlint-disable react/immutability, react/only-export-components */
7
+
8
+ import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'
9
+ import type { CSSProperties } from 'react'
10
+ import { Greenscreen as Keyer } from './greenscreen.ts'
11
+ import type { BackendChoice } from './greenscreen.ts'
12
+ import { DEFAULT_SETTINGS } from './settings.ts'
13
+ import type { Settings } from './settings.ts'
14
+ import { warnUnkeyable } from './source.ts'
15
+ import type { Source } from './types.ts'
16
+
17
+ // The keyer for React: a hook that keeps one, and a component that shows its canvas. Each keyer makes its own canvas
18
+ // rather than drawing in one React made, as a canvas can only ever have one kind of context, and StrictMode mounts
19
+ // everything twice
20
+
21
+ export type { Greenscreen as Keyer } from './greenscreen.ts'
22
+ export type { BackendChoice, GreenscreenOptions } from './greenscreen.ts'
23
+ export { warmUp } from './greenscreen.ts'
24
+ export { DEFAULT_SETTINGS, KEY_COLORS, RANGES } from './settings.ts'
25
+ export type { NumericSetting, Point, Range, Settings } from './settings.ts'
26
+ export type { BackendName, Source } from './types.ts'
27
+
28
+ export interface UseGreenscreenOptions {
29
+ // 'auto' by default. A new backend makes a new keyer
30
+ backend?: BackendChoice
31
+ // While true, the keyer doesn't render by itself, and render() is yours to call
32
+ paused?: boolean
33
+ // The source's picture can't be keyed, as the keyer's own onError says. A new function on each render is fine
34
+ onError?: (error: unknown) => void
35
+ }
36
+
37
+ type Given<T> = { -readonly [K in keyof T]?: T[K] }
38
+
39
+ // The settings that are given, without the ones set to undefined, which go back to their defaults
40
+ function given<T extends object>(settings: T | undefined): Given<T> {
41
+ const found: Given<T> = {}
42
+ if (!settings) return found
43
+ for (const key in settings) if (settings[key] !== undefined) found[key] = settings[key]
44
+ return found
45
+ }
46
+
47
+ // A keyer keying source with settings, rendering on every animation frame unless paused. It's made after the first
48
+ // render, so the hook returns null until then, and disposed when the component unmounts
49
+ export function useGreenscreen(
50
+ source: Source | null | undefined,
51
+ settings?: Readonly<Partial<Settings>>,
52
+ options: UseGreenscreenOptions = {},
53
+ ): Keyer | null {
54
+ const { backend = 'auto', paused = false, onError } = options
55
+ const [keyer, setKeyer] = useState<Keyer | null>(null)
56
+
57
+ // The latest handler, which the keyer calls through, so a new one doesn't make a new keyer. It's brought up to date
58
+ // after each render, before that render's source is given to the keyer
59
+ const handler = useRef(onError)
60
+ useEffect(() => {
61
+ handler.current = onError
62
+ })
63
+
64
+ // Made in an effect, which StrictMode runs twice, so each mount has its own, disposed as it goes
65
+ useEffect(() => {
66
+ const made = new Keyer(null, { backend, onError: (error) => (handler.current ?? warnUnkeyable)(error) })
67
+ // oxlint-disable-next-line react/set-state-in-effect -- the keyer is made here, where it can be disposed
68
+ setKeyer(made)
69
+ return () => made.dispose()
70
+ }, [backend])
71
+
72
+ useEffect(() => {
73
+ if (keyer) keyer.source = source ?? null
74
+ }, [keyer, source])
75
+
76
+ // On every render, the settings over the defaults, so one no longer given goes back. set() compares, and draws
77
+ // again only when something has changed
78
+ useEffect(() => {
79
+ keyer?.set({ ...DEFAULT_SETTINGS, ...given(settings) })
80
+ })
81
+
82
+ useEffect(() => {
83
+ if (!keyer || paused) return undefined
84
+ keyer.start()
85
+ return () => keyer.stop()
86
+ }, [keyer, paused])
87
+
88
+ return keyer
89
+ }
90
+
91
+ export interface GreenscreenProps extends UseGreenscreenOptions {
92
+ // A video, image, canvas or any other picture the browser can upload
93
+ source: Source | null | undefined
94
+ // Over the defaults
95
+ settings?: Readonly<Partial<Settings>>
96
+ // How the picture fits the element, as CSS's object-fit: 'contain' by default
97
+ fit?: CSSProperties['objectFit']
98
+ className?: string
99
+ style?: CSSProperties
100
+ }
101
+
102
+ // The keyed picture, in a div the canvas fills: size the div with className or style. See-through where the key and
103
+ // the cut take the picture out. The ref is the keyer, null until it's made after the first render
104
+ export const Greenscreen = forwardRef<Keyer | null, GreenscreenProps>(function Greenscreen(
105
+ { source, settings, backend, paused, onError, fit = 'contain', className, style },
106
+ ref,
107
+ ) {
108
+ const keyer = useGreenscreen(source, settings, { backend, paused, onError })
109
+ const host = useRef<HTMLDivElement>(null)
110
+
111
+ useImperativeHandle<Keyer | null, Keyer | null>(ref, () => keyer, [keyer])
112
+
113
+ useEffect(() => {
114
+ const element = host.current
115
+ if (!element || !keyer) return undefined
116
+ const canvas = keyer.canvas
117
+ canvas.style.display = 'block'
118
+ canvas.style.width = '100%'
119
+ canvas.style.height = '100%'
120
+ canvas.style.objectFit = fit ?? 'contain'
121
+ element.append(canvas)
122
+ return () => canvas.remove()
123
+ }, [keyer, fit])
124
+
125
+ return <div ref={host} className={className} style={style} />
126
+ })
@@ -0,0 +1,53 @@
1
+ import type { BackendName, Canvas, Source } from './types.ts'
2
+
3
+ // One keyer's drawing: a texture holding the source's picture, and a pass that keys it into the canvas
4
+ export interface Renderer {
5
+ readonly backend: BackendName
6
+ // True from the moment the graphics context or device is lost until it's been made again
7
+ readonly lost: boolean
8
+ // Takes the source's current picture into the texture, at its size. Throws where the picture can't be taken
9
+ upload(source: Source, width: number, height: number): void
10
+ // Keys the texture into the canvas, sized to the texture, with the packed settings
11
+ draw(settings: Float32Array<ArrayBuffer>): void
12
+ // Leaves the canvas see-through, at the size it has, for a picture that can't be keyed
13
+ clear(): void
14
+ dispose(): void
15
+ }
16
+
17
+ // What upload() throws for a picture larger than the backend's textures
18
+ export function tooLarge(width: number, height: number, backend: string, limit: number): RangeError {
19
+ return new RangeError(`Greenscreen: the picture is ${width}×${height}, and ${backend} here takes up to ${limit} a side`)
20
+ }
21
+
22
+ // What a renderer tells its keyer
23
+ export interface RendererEvents {
24
+ // The context or device is gone, and the texture with it
25
+ lost(): void
26
+ // It's been made again, with nothing in the texture
27
+ restored(): void
28
+ }
29
+
30
+ // The canvas's context of that kind, whichever kind of canvas it is, or null where it can't have one: it has another
31
+ // kind already, or the browser has none of that kind
32
+ export function contextOf(canvas: Canvas, id: 'webgl2', options: WebGLContextAttributes): WebGL2RenderingContext | null
33
+ export function contextOf(canvas: Canvas, id: 'webgpu'): GPUCanvasContext | null
34
+ export function contextOf(canvas: Canvas, id: '2d'): Context2D | null
35
+ export function contextOf(canvas: Canvas, id: 'webgl2' | 'webgpu' | '2d', options?: WebGLContextAttributes) {
36
+ let context: unknown = null
37
+ if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) {
38
+ context = canvas.getContext(id, options)
39
+ } else if (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas) {
40
+ context = canvas.getContext(id, options)
41
+ }
42
+ if (id === 'webgl2') return context instanceof WebGL2RenderingContext ? context : null
43
+ if (id === '2d') {
44
+ if (typeof CanvasRenderingContext2D !== 'undefined' && context instanceof CanvasRenderingContext2D) return context
45
+ if (typeof OffscreenCanvasRenderingContext2D !== 'undefined' && context instanceof OffscreenCanvasRenderingContext2D) {
46
+ return context
47
+ }
48
+ return null
49
+ }
50
+ return typeof GPUCanvasContext !== 'undefined' && context instanceof GPUCanvasContext ? context : null
51
+ }
52
+
53
+ export type Context2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D