wuyuedefeng / blogs

博客文章在issue中
5 stars 0 forks source link

个人收集的一些Polyfill方案 #127

Open wuyuedefeng opened 2 years ago

wuyuedefeng commented 2 years ago

toBlob

https://www.canvasapi.cn/HTMLCanvasElement/toBlob

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

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

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