import { ImTool } from './ImTool' ; import { ImageType , loadImage , fileToDataURL } from './Utils' ; param video export function fromCanvas ( canvas ) { return Promise . resolve ( new ImTool ( canvas ) ) ; } param video export function fromVideo ( video ) { return Promise . resolve ( new ImTool ( video ) ) ; } param stream export function fromMediaStream ( stream ) { return new Promise < ImTool > ( ( resolve , reject ) => { const video = document . createElement ( 'video' ) ; video . srcObject = stream ; video . play ( ) ; video . addEventListener ( 'playing' , async ( ) => { const tool = await fromVideo ( video ) ; video . srcObject = null ; stream . getTracks ( ) . forEach ( track => track . stop ( ) ) ; resolve ( tool ) ; } ) ; video . addEventListener ( 'error' , e => { reject ( e ) ; } ) ; } ) ; } param image export async function fromImage ( image ) { let url ; if ( typeof image === 'string' ) { url = image ; } else if ( image instanceof Blob ) { url = await fileToDataURL ( image ) ; } else if ( image instanceof HTMLImageElement ) { if ( image . complete && image . naturalWidth === 0 ) { return Promise . resolve ( new ImTool ( image ) ) ; } else { url = image . src ; } } if ( url ) { const img = await loadImage ( url ) ; return new ImTool ( img ) ; } else { throw new Error ( 'Unable to load the image.' ) ; } } /**
 * Creates a new instance of ImTool from screen capture.
 */ export async function fromScreen ( ) { if ( ! navigator . mediaDevices ?. getDisplayMedia ) { throw new Error ( 'Screen capture is not supported in this browser.' ) ; } const stream = await navigator . mediaDevices . getDisplayMedia ( { video : true , } ) ; if ( ! stream ) { throw new Error ( 'Unable to start screen capture.' ) ; } return await fromMediaStream ( stream ) ; } /**
 * Creates a new instance of ImTool from webcam capture.
 */ export async function fromWebcam ( ) { if ( ! navigator . mediaDevices ?. getUserMedia ) { throw new Error ( 'Webcam capture is not supported in this browser.' ) ; } const stream = await navigator . mediaDevices . getUserMedia ( { video : true , } ) ; if ( ! stream ) { throw new Error ( 'Unable to start webcam capture.' ) ; } return await fromMediaStream ( stream ) ; }