UNPKG

tethr

Version:

Controlls USB-connected cameras, webcam, and smartphone camera from browser

33 lines 1.12 kB
export class CanvasMediaStream { #ctx = null; #stream = null; async begin(initialDraw, frameRequestRate = 60, size) { if (!this.#ctx) { const canvas = document.createElement('canvas'); if (size) { canvas.width = size[0]; canvas.height = size[1]; } this.#ctx = canvas.getContext('2d'); } await initialDraw(); this.#stream = this.#ctx.canvas.captureStream(frameRequestRate); return this.#stream; } updateWithImage(image) { if (!this.#ctx) { throw new Error('CanvasMediaStream.begin() must be called first'); } const sizeChanged = this.#ctx.canvas.width !== image.width || this.#ctx.canvas.height !== image.height; if (sizeChanged) { this.#ctx.canvas.width = image.width; this.#ctx.canvas.height = image.height; } this.#ctx.drawImage(image, 0, 0); } end() { this.#stream?.getTracks().forEach(track => track.stop()); } } //# sourceMappingURL=CanvasMediaStream.js.map