gpt4 book ai didi

javascript - Canvas 在边缘上 Blob

转载 作者:太空狗 更新时间:2023-10-29 13:40:45 24 4
gpt4 key购买 nike

尝试将 Html canvas 元素转换为 blob 时,Microsoft Edge 浏览器出现异常。一切都在普通浏览器上运行良好。异常:

SCRIPT438: Object doesn't support property or method '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 blob 转换吗?我正在使用:

Microsoft Edge 41.16299.15.0

最佳答案

这个小 polyfill 取自 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

关于javascript - Canvas 在边缘上 Blob ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47475647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com