access blob value outside of canvas.ToBlob() async function

后端 未结 3 1693
悲&欢浪女
悲&欢浪女 2021-01-05 04:59

I\'m working with HTMLCanvas element that return the blob object outside of the async toBlob() function. This function doesn\'t return an output va

3条回答
  •  Happy的楠姐
    2021-01-05 05:33

    You can use Promise constructor, pass Blob instance to resolve(), access Promise value at .then()

    function getCanvasBlob(canvas) {
      return new Promise(function(resolve, reject) {
        canvas.toBlob(function(blob) {
          resolve(blob)
        })
      })
    }
    
    var canvasBlob = getCanvasBlob(canvas);
    
    canvasBlob.then(function(blob) {
      // do stuff with blob
    }, function(err) {
      console.log(err)
    });
    

提交回复
热议问题