@bddh/starling-cutout
Version:
cutout
2 lines (1 loc) • 2.36 kB
JavaScript
import e from"lodash/isEmpty";import t from"lodash/isNumber";import{waitForLoading as s}from"./packages/wait-for-loading/es/index.js";import"./libs/seriously.js";import"./libs/seriously.chroma.js";import"./libs/seriously.chroma_obs.js";import"./libs/seriously.chroma_post.js";class i{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((()=>!(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((()=>!(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)}}export{i as default};