@turbox3d/graphic-component-pixi
Version:
Graphic component library based on pixi
75 lines (74 loc) • 3.46 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.drawRect = drawRect;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _option = require("./option");
/**
* 绘制一个矩形
*/
function drawRect(_x, _x2) {
return _drawRect.apply(this, arguments);
}
function _drawRect() {
_drawRect = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(graphic, param) {
var x, y, width, height, _param$central, central, _param$radius, radius, _param$backgroundImag, backgroundImage, fit, fillColor, fillAlpha, _ref, _ref2, posX, posY, r;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
// 样式配置
(0, _option.setCommonOption)(graphic, param);
// 坐标配置
x = param.x, y = param.y, width = param.width, height = param.height, _param$central = param.central, central = _param$central === void 0 ? false : _param$central, _param$radius = param.radius, radius = _param$radius === void 0 ? 0 : _param$radius, _param$backgroundImag = param.backgroundImage, backgroundImage = _param$backgroundImag === void 0 ? '' : _param$backgroundImag, fit = param.fit, fillColor = param.fillColor, fillAlpha = param.fillAlpha; // 计算实际位置坐标
_ref = central ? [x - width / 2, y - height / 2] : [x, y], _ref2 = (0, _slicedToArray2["default"])(_ref, 2), posX = _ref2[0], posY = _ref2[1]; // 计算填充贴图样式
if (!backgroundImage) {
_context.next = 6;
break;
}
_context.next = 6;
return (0, _option.setFillTexture)(graphic, backgroundImage, {
fillColor: fillColor,
fillAlpha: fillAlpha,
fit: fit,
posX: posX,
posY: posY,
width: width,
height: height
});
case 6:
if (radius > 0) {
r = Math.min(radius, Math.min(width, height) / 2);
graphic.moveTo(posX + r, posY);
if (width !== 2 * r) {
graphic.lineTo(posX + width - r, posY);
}
graphic.arcTo(posX + width, posY, posX + width, posY + r, r);
if (height !== 2 * r) {
graphic.lineTo(posX + width, posY + height - r);
}
graphic.arcTo(posX + width, posY + height, posX + width - r, posY + height, r);
if (width !== 2 * r) {
graphic.lineTo(posX + r, posY + height);
}
graphic.arcTo(posX, posY + height, posX, posY + height - r, r);
if (height !== 2 * r) {
graphic.lineTo(posX, posY + r);
}
graphic.arcTo(posX, posY, posX + r, posY, r);
// graphic.drawRoundedRect(posX, posY, width, height, radius);
} else {
graphic.drawRect(posX, posY, width, height);
}
graphic.endFill();
case 8:
case "end":
return _context.stop();
}
}, _callee);
}));
return _drawRect.apply(this, arguments);
}