@turbox3d/graphic-component-pixi
Version:
Graphic component library based on pixi
303 lines (302 loc) • 14.5 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var PIXI = _interopRequireWildcard(require("pixi.js"));
var _rendererPixi = require("@turbox3d/renderer-pixi");
var _shared = require("@turbox3d/shared");
var _drawUtils = _interopRequireDefault(require("../draw-utils"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
/** UI组件-图片 */
var Image2d = exports["default"] = /*#__PURE__*/function (_Mesh2D) {
function Image2d() {
var _this;
(0, _classCallCheck2["default"])(this, Image2d);
_this = _callSuper(this, Image2d, arguments);
_this.reactivePipeLine = [_this.updateGeometry, _this.updateMaterial, _this.updatePosition, _this.updateRotation, _this.updateScale];
_this.g = new PIXI.Graphics();
_this.s = new PIXI.Sprite();
return _this;
}
(0, _inherits2["default"])(Image2d, _Mesh2D);
return (0, _createClass2["default"])(Image2d, [{
key: "componentDidMount",
value: function componentDidMount() {
this.g.zIndex = -1;
this.s.zIndex = -1;
this.view.addChild(this.g);
this.view.addChild(this.s);
}
}, {
key: "loadTextureResource",
value: function () {
var _loadTextureResource = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
var backgroundImage, t;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
backgroundImage = this.props.backgroundImage;
if (backgroundImage) {
_context.next = 3;
break;
}
return _context.abrupt("return");
case 3:
if (!(backgroundImage instanceof HTMLImageElement)) {
_context.next = 9;
break;
}
_context.next = 6;
return PIXI.Texture.fromLoader(backgroundImage, backgroundImage.src)["catch"](function () {
(0, _shared.fail)('Load Image2d backgroundImage texture failed.');
});
case 6:
t = _context.sent;
_context.next = 12;
break;
case 9:
_context.next = 11;
return PIXI.Texture.fromURL(backgroundImage)["catch"](function () {
(0, _shared.fail)('Load Image2d backgroundImage texture failed.');
});
case 11:
t = _context.sent;
case 12:
return _context.abrupt("return", t);
case 13:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function loadTextureResource() {
return _loadTextureResource.apply(this, arguments);
}
return loadTextureResource;
}()
}, {
key: "updateGeometry",
value: function () {
var _updateGeometry = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
var _this$props, width, height, _this$props$radius, radius, _this$props$lineWidth, lineWidth, _this$props$lineColor, lineColor, _this$props$lineAlpha, lineAlpha, _this$props$alignment, alignment, _native, _this$props$fillColor, fillColor, _this$props$fillAlpha, fillAlpha, _this$props$alpha, alpha, _this$props$backgroun, backgroundImage, _this$props$fit, fit, alignParam, fillPos, rw, rh, t, _this$s$texture, tw, th, imgRatio, rectRatio, center, ratio, _yield$cropImage, element, _ratio, _yield$cropImage2, _element;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
this.g.clear();
this.s.visible = false;
_this$props = this.props, width = _this$props.width, height = _this$props.height, _this$props$radius = _this$props.radius, radius = _this$props$radius === void 0 ? 0 : _this$props$radius, _this$props$lineWidth = _this$props.lineWidth, lineWidth = _this$props$lineWidth === void 0 ? 0 : _this$props$lineWidth, _this$props$lineColor = _this$props.lineColor, lineColor = _this$props$lineColor === void 0 ? 0x0 : _this$props$lineColor, _this$props$lineAlpha = _this$props.lineAlpha, lineAlpha = _this$props$lineAlpha === void 0 ? 1 : _this$props$lineAlpha, _this$props$alignment = _this$props.alignment, alignment = _this$props$alignment === void 0 ? 0 : _this$props$alignment, _native = _this$props["native"], _this$props$fillColor = _this$props.fillColor, fillColor = _this$props$fillColor === void 0 ? 0x0 : _this$props$fillColor, _this$props$fillAlpha = _this$props.fillAlpha, fillAlpha = _this$props$fillAlpha === void 0 ? 0 : _this$props$fillAlpha, _this$props$alpha = _this$props.alpha, alpha = _this$props$alpha === void 0 ? 1 : _this$props$alpha, _this$props$backgroun = _this$props.backgroundImage, backgroundImage = _this$props$backgroun === void 0 ? '' : _this$props$backgroun, _this$props$fit = _this$props.fit, fit = _this$props$fit === void 0 ? 'none' : _this$props$fit;
alignParam = alignment;
if (alignment === 0) {
alignParam = 1;
} else if (alignment === 1) {
alignParam = 0;
}
fillPos = {
x: lineWidth * alignParam,
y: lineWidth * alignParam
};
rw = width - lineWidth * 2 * alignParam;
rh = height - lineWidth * 2 * alignParam;
_drawUtils["default"].drawRect(this.g, {
x: 0,
y: 0,
width: width,
height: height,
central: false,
radius: radius,
lineWidth: lineWidth,
lineColor: lineColor,
lineAlpha: lineAlpha,
alignment: alignment,
"native": _native,
fillColor: fillColor,
fillAlpha: fillAlpha,
alpha: alpha
});
_context2.next = 11;
return this.loadTextureResource();
case 11:
t = _context2.sent;
if (t) {
_context2.next = 14;
break;
}
return _context2.abrupt("return");
case 14:
this.s.texture = t;
_this$s$texture = this.s.texture, tw = _this$s$texture.width, th = _this$s$texture.height;
imgRatio = tw / th;
rectRatio = width / height;
center = {
x: width / 2,
y: height / 2
};
if (!(fit === 'none')) {
_context2.next = 24;
break;
}
if (rectRatio > 1) {
this.s.width = rw;
this.s.height = rw / imgRatio;
} else {
this.s.width = rh * imgRatio;
this.s.height = rh;
}
this.s.position.set(center.x - this.s.width / 2, center.y - this.s.height / 2);
_context2.next = 54;
break;
case 24:
if (!(fit === 'fill')) {
_context2.next = 30;
break;
}
this.s.width = rw;
this.s.height = rh;
this.s.position.set(fillPos.x, fillPos.y);
_context2.next = 54;
break;
case 30:
if (!(fit === 'cover')) {
_context2.next = 53;
break;
}
if (!(imgRatio <= rectRatio)) {
_context2.next = 42;
break;
}
ratio = rw / tw;
_context2.next = 35;
return (0, _shared.cropImage)(backgroundImage, {
start: {
x: 0,
y: th / 2 - rh / ratio / 2
},
end: {
x: tw,
y: th / 2 + rh / ratio / 2
}
});
case 35:
_yield$cropImage = _context2.sent;
element = _yield$cropImage.element;
if (element) {
this.s.texture = PIXI.Texture.from(element);
}
this.s.width = rw;
this.s.height = rh;
_context2.next = 50;
break;
case 42:
_ratio = rh / th;
_context2.next = 45;
return (0, _shared.cropImage)(backgroundImage, {
start: {
x: tw / 2 - rw / _ratio / 2,
y: 0
},
end: {
x: tw / 2 + rw / _ratio / 2,
y: th
}
});
case 45:
_yield$cropImage2 = _context2.sent;
_element = _yield$cropImage2.element;
if (_element) {
this.s.texture = PIXI.Texture.from(_element);
}
this.s.width = rw;
this.s.height = rh;
case 50:
this.s.position.set(fillPos.x, fillPos.y);
_context2.next = 54;
break;
case 53:
if (fit === 'contain') {
if (imgRatio <= rectRatio) {
this.s.width = rh * imgRatio;
this.s.height = rh;
this.s.position.set(fillPos.x + rw / 2 - rh * imgRatio / 2, fillPos.y);
} else {
this.s.width = rw;
this.s.height = rw / imgRatio;
this.s.position.set(fillPos.x, fillPos.y + rh / 2 - rw / imgRatio / 2);
}
}
case 54:
// 方便做贴图的镜像翻转
this.s.anchor.set(0.5, 0.5);
// anchor 设置为 0.5 后,此处需要将贴图调整回去
this.s.position.set(this.s.position.x + this.s.width / 2, this.s.position.y + this.s.height / 2);
this.s.visible = true;
case 57:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function updateGeometry() {
return _updateGeometry.apply(this, arguments);
}
return updateGeometry;
}()
}, {
key: "updateMaterial",
value: function updateMaterial() {
var _this$props2 = this.props,
_this$props2$zIndex = _this$props2.zIndex,
zIndex = _this$props2$zIndex === void 0 ? 0 : _this$props2$zIndex,
_this$props2$material = _this$props2.materialDirection,
materialDirection = _this$props2$material === void 0 ? {
x: 1,
y: 1
} : _this$props2$material;
this.view.zIndex = zIndex;
this.s.scale.set(materialDirection.x > 0 ? Math.abs(this.s.scale.x) : -Math.abs(this.s.scale.x), materialDirection.y > 0 ? Math.abs(this.s.scale.y) : -Math.abs(this.s.scale.y));
}
}, {
key: "updatePosition",
value: function updatePosition() {
var _this$props3 = this.props,
_this$props3$x = _this$props3.x,
x = _this$props3$x === void 0 ? 0 : _this$props3$x,
_this$props3$y = _this$props3.y,
y = _this$props3$y === void 0 ? 0 : _this$props3$y,
_this$props3$central = _this$props3.central,
central = _this$props3$central === void 0 ? false : _this$props3$central,
width = _this$props3.width,
height = _this$props3.height;
var _ref = central ? [x - width / 2, y - height / 2] : [x, y],
_ref2 = (0, _slicedToArray2["default"])(_ref, 2),
posX = _ref2[0],
posY = _ref2[1];
this.view.position.x = posX;
this.view.position.y = posY;
}
}, {
key: "updateRotation",
value: function updateRotation() {
var _a;
this.view.rotation = (_a = this.props.rotation) !== null && _a !== void 0 ? _a : 0;
}
}, {
key: "updateScale",
value: function updateScale() {
var _a, _b, _c, _d;
this.view.scale.set((_b = (_a = this.props.scale) === null || _a === void 0 ? void 0 : _a.x) !== null && _b !== void 0 ? _b : 1, (_d = (_c = this.props.scale) === null || _c === void 0 ? void 0 : _c.y) !== null && _d !== void 0 ? _d : 1);
}
}]);
}(_rendererPixi.Mesh2D);