pip-canvas/PipCanvas.mjs
Victor Westerlund 97e9a02175 Initial commit
2022-08-03 15:11:42 +02:00

29 lines
No EOL
715 B
JavaScript

export class PipCanvas {
constructor(source) {
this.source = source;
const canvas = document.createElement("canvas");
this.ctx = canvas.getContext("2d");
// Call method with same constructor name if exists
if (this.source.constructor.name in this) {
this[this.source.constructor.name]();
}
this.video = document.createElement("video");
this.video.srcObject = canvas.captureStream(0);
}
// Create from image
HTMLImageElement() {
this.ctx.width = this.ctx.canvas.width = this.source.width;
this.ctx.height = this.ctx.canvas.height = this.source.height;
this.ctx.drawImage(this.source, 0, 0);
}
// Open PIP
open() {
this.video.play();
this.video.requestPictureInPicture();
}
}