var CanvasUtil = {
  toBase64: function toBase64(canvas, _ref) {
    var _ref$suffix = _ref.suffix,
      suffix = _ref$suffix === void 0 ? 'image/png' : _ref$suffix,
      _ref$quality = _ref.quality,
      quality = _ref$quality === void 0 ? 0.92 : _ref$quality;
    return canvas.toDataURL(suffix, quality);
  },
  isBlank: function isBlank(canvas) {
    var blank = document.createElement('canvas');
    blank.width = canvas.width;
    blank.height = canvas.height;
    if (canvas.toDataURL() === blank.toDataURL()) return true;
    return false;
  },
  clear: function clear(canvas) {
    var width = canvas.width;
    var height = canvas.height;
    var ctx = canvas !== null && canvas !== void 0 && canvas.ctx ? canvas.ctx : canvas.getContext('2d');
    ctx.clearRect(0, 0, width, height);
  },
  // 旋转base64图片-90度
  rotateBase64: function rotateBase64(base64, _ref2) {
    var backgroundColor = _ref2.backgroundColor;
    return new Promise(function (resolve) {
      var img = new Image();
      img.onload = function () {
        // 创建画布并调整尺寸
        var canvas = document.createElement('canvas');
        canvas.width = img.height; // 交换宽高
        canvas.height = img.width;

        // 获取绘图上下文
        var ctx = canvas.getContext('2d');

        // 应用变换：移动到画布底部 + 逆时针旋转90度
        ctx.translate(0, canvas.height);
        ctx.rotate(-Math.PI / 2);

        // 绘制背景
        if (backgroundColor && typeof backgroundColor === 'string' && backgroundColor !== 'transparent') {
          ctx.fillStyle = backgroundColor;
          ctx.fillRect(0, 0, ctx.canvas.height, ctx.canvas.width);
        }

        // 绘制原始图像
        ctx.drawImage(img, 0, 0);

        // 导出为Base64
        resolve(canvas.toDataURL());
      };
      img.onerror = function () {
        console.error('CanvasUtil.rotateBase64 failed');
        resolve('');
      };
      img.src = base64;
    });
  }
};
export default CanvasUtil;