画布在边缘上斑点

时间:2017-11-24 14:50:52

标签: javascript html5 html5-canvas blob microsoft-edge

尝试将Html画布元素转换为blob时在Microsoft Edge浏览器上获取异常。一切都适用于普通浏览器。 例外:

  

SCRIPT438:对象不支持属性或方法'toBlob'

Html片段:

<canvas id="cnv" width="640px" height="520px" style="display: none"></canvas>

使用Javascript:

var files[];
var canvas = document.getElementById('cnv');
canvas.toBlob(function (blob) {            
        files.push(blob);
         }
    }, 'image/jpeg', 1);

当我调用toBlob方法时,我得到了这个例外。有没有办法教Edge gb转换? 我正在使用:

  

Microsoft Edge 41.16299.15.0

2 个答案:

答案 0 :(得分:9)

这种小型填料from here必须帮助see jsfiddle

if (!HTMLCanvasElement.prototype.toBlob) {
   Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
     value: function (callback, type, quality) {
       var canvas = this;
       setTimeout(function() {
         var binStr = atob( canvas.toDataURL(type, quality).split(',')[1] ),
         len = binStr.length,
         arr = new Uint8Array(len);

         for (var i = 0; i < len; i++ ) {
            arr[i] = binStr.charCodeAt(i);
         }

         callback( new Blob( [arr], {type: type || 'image/png'} ) );
       });
     }
  });
}

这也有助github.com/blueimp/JavaScript-Canvas-to-Blob

答案 1 :(得分:0)

如果您使用 webpack ES6 ,则通过npm install the package

npm install blueimp-canvas-to-blob

然后像这样导入包

import "blueimp-canvas-to-blob/js/canvas-to-blob";