UNPKG

@bddh/starling-cutout

Version:

cutout

2 lines (1 loc) 2.44 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("lodash/isEmpty"),t=require("lodash/isNumber"),s=require("./packages/wait-for-loading/es/index.js");require("./libs/seriously.js"),require("./libs/seriously.chroma.js"),require("./libs/seriously.chroma_obs.js"),require("./libs/seriously.chroma_post.js");exports.default=class{seriously;version;defaultOptions={screen:[0,255,0,0]};targetElement;sourceElement;source=null;target=null;effect=null;backEffect=null;constructor(e,t,s,i=0){this.seriously=new window.Seriously,this.version=i,this.targetElement="string"==typeof e?document.getElementById(e):e,this.sourceElement="string"==typeof t?document.getElementById(t):t,this.init(s)}async loadEffect(e){return 1===e?"chroma":"chroma_obs"}async init(e){console.info("serious version:",this.version);const t=await this.loadEffect(this.version);await s.waitForLoading((()=>!(this.sourceElement?.videoWidth>0&&this.sourceElement?.videoHeight>0))),this.targetElement.width=this.sourceElement.videoWidth,this.targetElement.height=this.sourceElement.videoHeight,this.source=this.seriously.source(this.sourceElement),this.target=this.seriously.target(this.targetElement),this.effect=this.seriously.effect(t),this.backEffect=this.seriously.effect("chroma_post"),this.effect.source=this.source,this.backEffect.source=this.effect,this.target.source=this.backEffect,this.seriously.go(),this.setOptions(this.defaultOptions),e&&this.setOptions(e)}destroy(){this.seriously.destroy()}async updateSource(e){e&&this.effect&&this.backEffect?(this.sourceElement="string"==typeof e?document.getElementById(e):e,await s.waitForLoading((()=>!(this.sourceElement?.videoWidth>0&&this.sourceElement?.videoHeight>0))),this.source?.destroy(),console.info("update source: ",this.source?.isDestroyed()),this.source=this.seriously.source(this.sourceElement),this.effect.source=this.source,this.backEffect.source=this.effect,this.target.source=this.backEffect):console.warn("update source error: source or effect is null",e,this.effect)}async setOptions(s){if(this.defaultOptions={...this.defaultOptions,...s},!this.effect||!this.backEffect||e(s))return;console.info("set options: ",s);const{screen:i,edgeShrink:o}=s;if(i&&i.length>=3){const[e,s,o,r]=i;this.effect.screen=[e/255,s/255,o/255,t(r)?r:1]}o&&(this.backEffect.edgeShrink=o);for(const[e,t]of Object.entries(s))"number"==typeof t&&"screen"!==e&&"edgeShrink"!==e&&(this.effect[e]=t)}};