Constructs a new instance of the green screen stream handler.
The method to use for green screen processing.
The desired video resolution or a vector specifying width and height.
OptionalcanvasEl: HTMLCanvasElement(Optional) An existing HTMLCanvasElement to use for rendering. If not provided, a new canvas will be created.
Initializes the media stream and canvas, sets the canvas resolution, and determines whether to use machine learning-based background removal based on the selected green screen method.
Optionalcanvas(Optional) An existing HTMLCanvasElement to use for rendering. If not provided, a new canvas will be created.
Initializes the media stream and canvas, sets the canvas resolution, and determines whether to use machine learning-based background removal based on the selected green screen method.
The method to use for green screen processing.
Captures the current state of the canvas as a MediaStream. Optionally, you can specify the frames per second (fps) for the captured stream.
Optionalfps: numberThe frames per second for the captured stream (default is 25).
The captured MediaStream from the canvas.
Retrieves the color palette and dominant color from the current video stream. This method captures the current frame from the video source, processes it, and returns an object containing the color palette and dominant color.
An object containing the color palette and dominant color.
Initializes the green screen stream with a background source and optional configuration.
The URL of the background image or video to use.
Optionalconfig: IGreenScreenConfigOptional configuration settings for the green screen.
A promise that resolves when the initialization is complete.
Scales the provided image to fit the canvas dimensions. Returns a promise that resolves with the scaled HTMLImageElement.
The image to scale.
OptionalimageOptions: ImageBitmapOptionsOptional options for creating the image bitmap.
A promise that resolves with the scaled image.
Sets the background image or video for the green screen. Returns a promise that resolves with the loaded background element (HTMLImageElement or HTMLVideoElement).
The source URL of the background image or video.
A promise that resolves with the loaded background element.
Sets the BodyPix model for machine learning-based background removal. Loads the model based on the provided configuration or uses a default configuration.
The configuration for the BodyPix model.
A promise that resolves when the model is loaded and ready.
Sets the chroma key color used for green screen processing. The chroma key color is specified as RGB values.
The red component of the chroma key color (0-255).
The green component of the chroma key color (0-255).
The blue component of the chroma key color (0-255).
The
GreenScreenStreamclass provides a virtual background solution for video streams, supporting both chroma key (green screen) and machine learning-based background segmentation. It manages video sources, background images or videos, and rendering via WebGL, and can output the processed stream as a MediaStream suitable for use in web applications.Features:
Usage:
initialize()with a background source and optional configuration.addVideoTrack().start().captureStream().stop().Example