Files
shoot-miniprograms/src/util.js

134 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-06-21 12:59:59 +08:00
export function renderScores(ctx, arrows = []) {
let rowIndex = 0;
arrows.forEach((item, i) => {
rowIndex = i;
if (arrows.length === 12) {
if (i > 5) rowIndex = i - 6;
ctx.drawImage(
"../static/score-bg.png",
16 + rowIndex * 46,
i > 5 ? 366 : 320,
40,
40
);
renderText(
ctx,
item.ring,
25,
"#fed847",
36 + rowIndex * 46,
i > 5 ? 395 : 349,
"center"
);
}
if (arrows.length === 36) {
ctx.drawImage(
"../static/score-bg.png",
18 + (i % 9) * 30,
290 + Math.ceil((i + 1) / 9) * 30,
27,
27,
"center"
);
renderText(
ctx,
item.ring,
18,
"#fed847",
31 + (i % 9) * 30,
310 + Math.ceil((i + 1) / 9) * 30,
"center"
);
}
});
}
2025-06-21 03:34:10 +08:00
export function renderLine(ctx, from) {
ctx.beginPath();
ctx.lineWidth = 1;
ctx.strokeStyle = "rgba(255, 255, 255, 0.3)";
const length = 35;
ctx.moveTo(from, 293);
ctx.lineTo(from + length, 293);
ctx.stroke();
}
2025-06-21 12:59:59 +08:00
export function renderText(ctx, text, size, color, x, y, textAlign = "left") {
2025-06-21 03:34:10 +08:00
ctx.setFontSize(size);
ctx.setFillStyle(color);
2025-06-21 12:59:59 +08:00
ctx.setTextAlign(textAlign);
2025-06-21 03:34:10 +08:00
ctx.fillText(text, x, y);
}
export function renderRankTitle(ctx, text) {
const fontSize = 10;
const textWidth = ctx.measureText(text).width;
const padding = 8; // 文字与背景边缘的间距
const radius = 10; // 圆角半径
2025-06-21 12:59:59 +08:00
const textX = 85;
2025-06-21 03:34:10 +08:00
const textY = 55;
2025-06-21 12:59:59 +08:00
const x = textX - padding - 15; // 文字 x 坐标减去内边距
2025-06-21 03:34:10 +08:00
const y = textY - fontSize - padding / 2 + 1; // 文字 y 坐标减去字体大小和内边距
2025-06-21 12:59:59 +08:00
const width = textWidth + padding * 2 - 19; // 背景宽度
2025-06-21 03:34:10 +08:00
const height = fontSize + padding + 1; // 背景高度
// 开始绘制圆角矩形
ctx.beginPath();
ctx.moveTo(x + radius, y);
// 上边框
ctx.lineTo(x + width - radius, y);
ctx.arcTo(x + width, y, x + width, y + radius, radius);
// 右边框
ctx.lineTo(x + width, y + height - radius);
ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius);
// 下边框
ctx.lineTo(x + radius, y + height);
ctx.arcTo(x, y + height, x, y + height - radius, radius);
// 左边框
ctx.lineTo(x, y + radius);
ctx.arcTo(x, y, x + radius, y, radius);
// 设置背景颜色并填充
ctx.fillStyle = "#5F51FF";
ctx.fill();
ctx.setFontSize(fontSize);
ctx.setFillStyle("rgba(255, 255, 255, 0.9)");
ctx.fillText(text, textX, textY); // 绘制文字
}
2025-06-21 12:59:59 +08:00
export const drawRoundImage = async (
ctx,
imgPath,
x,
y,
width,
height,
radius
) => {
ctx.save();
// 创建圆角路径
ctx.beginPath();
ctx.moveTo(x + radius, y);
// 右上角
ctx.lineTo(x + width - radius, y);
ctx.arcTo(x + width, y, x + width, y + radius, radius);
// 右下角
ctx.lineTo(x + width, y + height - radius);
ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius);
// 左下角
ctx.lineTo(x + radius, y + height);
ctx.arcTo(x, y + height, x, y + height - radius, radius);
// 左上角
ctx.lineTo(x, y + radius);
ctx.arcTo(x, y, x + radius, y, radius);
ctx.clip();
// 绘制图片
ctx.drawImage(imgPath, x, y, width, height);
ctx.restore();
};