|
| 1 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | +import type Smelter from '@swmansion/smelter-web-wasm'; |
| 3 | + |
| 4 | +type CanvasProps = React.DetailedHTMLProps< |
| 5 | + React.CanvasHTMLAttributes<HTMLCanvasElement>, |
| 6 | + HTMLCanvasElement |
| 7 | +>; |
| 8 | + |
| 9 | +type SmelterCanvasProps = { |
| 10 | + smelter: Smelter; |
| 11 | + audio?: boolean; |
| 12 | + children: React.ReactElement; |
| 13 | +} & CanvasProps; |
| 14 | + |
| 15 | +type UpdatedProps = { |
| 16 | + smelter: Smelter; |
| 17 | + width?: number | string; |
| 18 | + height?: number | string; |
| 19 | + audio?: boolean; |
| 20 | +}; |
| 21 | + |
| 22 | +type RegisterOptions = UpdatedProps & { |
| 23 | + outputId: string; |
| 24 | + canvas: HTMLCanvasElement; |
| 25 | +}; |
| 26 | + |
| 27 | +const getNewOutputId = (() => { |
| 28 | + let counter = 1; |
| 29 | + return () => { |
| 30 | + const outputId = `output-${counter}`; |
| 31 | + counter += 1; |
| 32 | + return outputId; |
| 33 | + }; |
| 34 | +})(); |
| 35 | + |
| 36 | +export default function SmelterCanvasOutput(props: SmelterCanvasProps) { |
| 37 | + const { children, smelter, audio, ...canvasProps } = props; |
| 38 | + const [updatedProps, setUpdatedProps] = useState<UpdatedProps | undefined>(); |
| 39 | + const [registerOptions, setRegisterOptions] = useState<RegisterOptions | undefined>(undefined); |
| 40 | + const key = useRef<number>(1); |
| 41 | + |
| 42 | + const canvasRef = useCallback( |
| 43 | + async (updatedCanvas: HTMLCanvasElement | null) => { |
| 44 | + if (!updatedCanvas || !updatedProps || updatedCanvas === registerOptions?.canvas) { |
| 45 | + return; |
| 46 | + } |
| 47 | + const outputId = getNewOutputId(); |
| 48 | + setRegisterOptions({ |
| 49 | + ...updatedProps, |
| 50 | + outputId, |
| 51 | + canvas: updatedCanvas, |
| 52 | + }); |
| 53 | + }, |
| 54 | + [updatedProps, registerOptions] |
| 55 | + ); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + // force new canvas |
| 59 | + key.current += 1; |
| 60 | + |
| 61 | + setUpdatedProps({ |
| 62 | + smelter, |
| 63 | + width: canvasProps.width, |
| 64 | + height: canvasProps.height, |
| 65 | + audio, |
| 66 | + }); |
| 67 | + }, [canvasProps.width, canvasProps.height, smelter, audio]); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + if (!registerOptions) { |
| 71 | + return; |
| 72 | + } |
| 73 | + const { audio, outputId, width, height, canvas } = registerOptions; |
| 74 | + const promise = smelter.registerOutput(outputId, children, { |
| 75 | + type: 'canvas', |
| 76 | + video: { |
| 77 | + canvas, |
| 78 | + resolution: { |
| 79 | + width: Number(width ?? canvas?.width), |
| 80 | + height: Number(height ?? canvas?.height), |
| 81 | + }, |
| 82 | + }, |
| 83 | + audio, |
| 84 | + }); |
| 85 | + |
| 86 | + return () => { |
| 87 | + void promise |
| 88 | + .catch(console.error) |
| 89 | + .then(() => smelter.unregisterOutput(outputId)) |
| 90 | + .catch(console.error); |
| 91 | + }; |
| 92 | + }, [registerOptions]); |
| 93 | + |
| 94 | + return <canvas key={key.current} ref={canvasRef} {...canvasProps} />; |
| 95 | +} |
0 commit comments