import type { CanvasRecorderOpts } from "./api.js" ; import { downloadWithMime } from "./raw.js" ; /**
 * Triggers canvas-to-blob conversion for given file type (and opt. `quality`),
 * then triggers download via  downloadWithMime . Default file type is
 * `png`. Default quality is 0.95 (only used for JPEG/WebP).
 *
 *  param canvas param baseName param type param quality export const downloadCanvas = ( canvas , baseName , type = "png" , quality = 0.95 ) => { const mime = `image/${ type }` ; canvas . toBlob ( ( blob ) => blob ? downloadWithMime ( `${ baseName }.${ type }` , blob , { mime } ) : console . warn ( "can't download canvas" ) , mime , quality ) ; } ; remarks param canvas param fileName param opts export const canvasRecorder = ( canvas , fileName , opts ? ) => { opts = { fps : 60 , mimeType : "video/webm; codecs=vp9" , ... opts } ; const stream = canvas . captureStream ( opts . fps ) ; const recorder = new MediaRecorder ( stream , { mimeType : opts . mimeType , } ) ; let blobs = [ ] ; recorder . ondataavailable = ( e ) => { if ( e . data . size > 0 ) { blobs . push ( e . data ) ; downloadWithMime ( fileName , new Blob ( blobs , { type : opts ! . mimeType } ) , { mime : opts ! . mimeType ! , } ) ; blobs = [ ] ; } } ; return recorder ; } ;