@turbox3d/graphic-component-pixi
Version:
Graphic component library based on pixi
68 lines • 3.16 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { setCommonOption, setFillTexture } from './option';
/**
* 绘制一个矩形
*/
export function drawRect(_x, _x2) {
return _drawRect.apply(this, arguments);
}
function _drawRect() {
_drawRect = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.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 _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
// 样式配置
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 = _slicedToArray(_ref, 2), posX = _ref2[0], posY = _ref2[1]; // 计算填充贴图样式
if (!backgroundImage) {
_context.next = 6;
break;
}
_context.next = 6;
return 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);
}