UNPKG

react-img-editor-en

Version:
172 lines (171 loc) 9.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _konva = _interopRequireDefault(require("konva")); var _Plugin2 = _interopRequireDefault(require("./Plugin")); var _utils = require("../common/utils"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var tileHeight = 5; var tileWidth = 5; var Mosaic = exports["default"] = /*#__PURE__*/function (_Plugin) { function Mosaic() { var _this; _classCallCheck(this, Mosaic); _this = _callSuper(this, Mosaic, arguments); _this.name = "mosaic"; _this.iconfont = "iconfont icon-mosaic"; _this.title = "Mosaic"; _this.params = ["strokeWidth"]; _this.defaultParamValue = { strokeWidth: 2 }; _this.isPaint = false; _this.tiles = []; _this.tileRowSize = 0; _this.tileColumnSize = 0; _this.width = 0; _this.height = 0; _this.rectGroup = null; _this.drawTile = function (tiles, drawLayer) { tiles = [].concat(tiles); tiles.forEach(function (tile) { if (tile.isFilled) { return; } if (!tile.color) { var dataLen = tile.data.length; var r = 0, g = 0, b = 0, a = 0; for (var i = 0; i < dataLen; i += 4) { r += tile.data[i]; g += tile.data[i + 1]; b += tile.data[i + 2]; a += tile.data[i + 3]; } // Set tile color. var pixelLen = dataLen / 4; tile.color = { r: Math.round(r / pixelLen), g: Math.round(g / pixelLen), b: Math.round(b / pixelLen), a: Math.round(a / pixelLen) }; } var color = tile.color; var x = tile.column * tileWidth; var y = tile.row * tileHeight; var w = tile.pixelWidth; var h = tile.pixelHeight; var rect = new _konva["default"].Rect({ globalCompositeOperation: "source-over", x: x, y: y, width: w, height: h, fill: "rgba(".concat(color.r, ", ").concat(color.g, ", ").concat(color.b, ", ").concat(color.a / 255, ")") }); _this.rectGroup.add(rect); tile.isFilled = true; }); drawLayer.add(_this.rectGroup); drawLayer.draw(); }; _this.getTilesByPoint = function (x, y, strokeWidth) { var ts = []; var startRow = Math.max(0, Math.floor(y / tileHeight) - Math.floor(strokeWidth / 2)); var startColumn = Math.max(0, Math.floor(x / tileWidth) - Math.floor(strokeWidth / 2)); var endRow = Math.min(_this.tileRowSize, startRow + strokeWidth); var endColumn = Math.min(_this.tileColumnSize, startColumn + strokeWidth); while (startRow < endRow) { var column = startColumn; while (column < endColumn) { ts.push(_this.tiles[startRow * _this.tileColumnSize + column]); column += 1; } startRow += 1; } return ts; }; _this.onDrawStart = function (drawEventParams) { var stage = drawEventParams.stage, imageData = drawEventParams.imageData; _this.tiles = []; _this.width = stage.width(); _this.height = stage.height(); _this.tileRowSize = Math.ceil(_this.height / tileHeight); _this.tileColumnSize = Math.ceil(_this.width / tileWidth); _this.rectGroup = new _konva["default"].Group({ id: (0, _utils.uuid)() }); // 将图片切分成一个个大一点的贴片 for (var i = 0; i < _this.tileRowSize; i++) { for (var j = 0; j < _this.tileColumnSize; j++) { var tile = { row: i, column: j, pixelWidth: tileWidth, pixelHeight: tileHeight, data: [] }; var data = []; // 转换为像素图形下,起始像素位置 var pixelPosition = (_this.width * tileHeight * tile.row + tile.column * tileWidth) * 4; // 转换为像素图形下,包含多少行 var pixelRowAmount = tile.pixelHeight; // 计算,转换为像素图形使,一个贴片所包含的所有像素数据。先遍历贴片范围内的每一列,每一列中再单独统计行的像素数量 for (var _i = 0; _i < pixelRowAmount; _i++) { // 当前列的起始像素位置 var position = pixelPosition + _this.width * 4 * _i; // 贴片范围内一行的像素数据,等于贴片宽度 * 4 data = [].concat(_toConsumableArray(data), _toConsumableArray(imageData.data.slice(position, position + tile.pixelWidth * 4))); } tile.data = data; _this.tiles.push(tile); } } _this.isPaint = true; }; _this.onDraw = function (drawEventParams) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer, paramValue = drawEventParams.paramValue; var pos = stage.getPointerPosition(); if (!_this.isPaint || !pos) return; var strokeWidth = paramValue && paramValue.strokeWidth ? paramValue.strokeWidth : _this.defaultParamValue.strokeWidth; _this.drawTile(_this.getTilesByPoint(pos.x, pos.y, strokeWidth), drawLayer); }; _this.onDrawEnd = function (drawEventParams) { var pubSub = drawEventParams.pubSub; _this.isPaint = false; pubSub.pub("PUSH_HISTORY", _this.rectGroup); }; _this.onLeave = function () { _this.isPaint = false; }; return _this; } _inherits(Mosaic, _Plugin); return _createClass(Mosaic); }(_Plugin2["default"]);