"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _locale = _interopRequireDefault(require("./../locale"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

// CanvasUtil canvas操作
// 国际化数据
var CanvasUtil = {
  cropImg: function cropImg(opts) {
    if (opts === void 0) {
      opts = {};
    }

    // 参数校验
    var errMsg = '';

    if (isNaN(opts.x)) {
      errMsg = 'SeedsUI Error:CanvasUtil.cropImg裁切方法x参数不正确';
      console.warn(errMsg);
      return errMsg;
    }

    if (isNaN(opts.y)) {
      errMsg = "SeedsUI Error:CanvasUtil.cropImg," + (0, _locale["default"])('方法', 'method') + "y" + (0, _locale["default"])('参数不正确', 'wrong_parameter');
      console.warn(errMsg);
      return errMsg;
    }

    if (isNaN(opts.width)) {
      errMsg = "SeedsUI Error:CanvasUtil.cropImg," + (0, _locale["default"])('方法', 'method') + "width" + (0, _locale["default"])('参数不正确', 'wrong_parameter');
      console.warn(errMsg);
      return errMsg;
    }

    if (isNaN(opts.height)) {
      errMsg = "SeedsUI Error:CanvasUtil.cropImg," + (0, _locale["default"])('方法', 'method') + "width" + (0, _locale["default"])('参数不正确', 'wrong_parameter');
      console.warn(errMsg);
      return errMsg;
    }

    var suffix = opts.suffix || 'image/png';
    var quality = opts.quality || 0.92; // 开始裁切

    var canvas = document.getElementById('imgcrop_canvas');

    if (!canvas) {
      canvas = document.createElement('canvas');
      canvas.setAttribute('id', 'imgcrop_canvas');
      document.body.appendChild(canvas);
    }

    var ctx = canvas.getContext('2d');
    canvas.width = opts.width;
    canvas.height = opts.height;
    var img = new Image();
    img.src = opts.src;
    img.crossOrigin = 'Anonymous';
    img.addEventListener('load', function () {
      ctx.drawImage(img, opts.x, opts.y, opts.width, opts.height, 0, 0, opts.width, opts.height); // 成功回调

      try {
        if (opts.success) opts.success(canvas.toDataURL(suffix, quality));
      } catch (error) {
        if (opts.fail) opts.fail({
          errMsg: error
        });
      }
    }, false);
  }
};
var _default = CanvasUtil;
exports["default"] = _default;
module.exports = exports.default;