feat: canvas 示例

master
LCJ-MinYa 7 days ago
parent c01dae8426
commit c7baf96b6f

@ -41,45 +41,81 @@ const startDraw = function () {
function drawHexagon(radius = 50, custom) {
const cornerRadius = 20; //
ctx.beginPath();
//
for (let i = 0; i < 6; i++) {
const startAngle = (Math.PI / 3) * i + Math.PI / 2; //
const x1 = centerX + radius * Math.cos(startAngle);
const y1 = centerY + radius * Math.sin(startAngle);
const prevAngle = (Math.PI / 3) * (i - 1) + Math.PI / 2;
const prevX = centerX + radius * Math.cos(prevAngle);
const prevY = centerY + radius * Math.sin(prevAngle);
// prevX,prevY x1,y1 cornerX,cornerY
ctx.arcTo(prevX, prevY, x1, y1, cornerRadius);
}
//
const lastAngle = (Math.PI / 3) * 5 + Math.PI / 2;
const firstAngle = (Math.PI / 3) * 0 + Math.PI / 2;
const lastX = centerX + radius * Math.cos(lastAngle);
const lastY = centerY + radius * Math.sin(lastAngle);
const firstX = centerX + radius * Math.cos(firstAngle);
const firstY = centerY + radius * Math.sin(firstAngle);
ctx.arcTo(lastX, lastY, firstX, firstY, cornerRadius - cornerRadius / 2);
//
ctx.fillStyle = 'rgba(255, 248, 244, 1)';
ctx.strokeStyle = 'rgba(253, 201, 171, 1)';
ctx.lineWidth = 3;
// === custom true 线 ===
ctx.beginPath();
if (custom) {
// 线10px 线 + 5px
// === custom true () ===
// 线
const gap = 40; //
for (let i = 0; i < 6; i++) {
// ()
const angle1 = (Math.PI / 3) * i + Math.PI / 2;
const x1 = centerX + radius * Math.cos(angle1);
const y1 = centerY + radius * Math.sin(angle1);
// ()
const angle2 = (Math.PI / 3) * (i + 1) + Math.PI / 2;
const x2 = centerX + radius * Math.cos(angle2);
const y2 = centerY + radius * Math.sin(angle2);
//
const dx = x2 - x1;
const dy = y2 - y1;
const dist = Math.sqrt(dx * dx + dy * dy);
// 2*gap
if (dist > 2 * gap) {
const ux = dx / dist;
const uy = dy / dist;
//
const sx = x1 + ux * gap;
const sy = y1 + uy * gap;
const ex = x2 - ux * gap;
const ey = y2 - uy * gap;
ctx.moveTo(sx, sy);
ctx.lineTo(ex, ey);
}
}
// 线
ctx.setLineDash([5, 8]);
ctx.lineDashOffset = 0;
ctx.stroke();
ctx.setLineDash([]);
ctx.setLineDash([]); //
} else {
// === () ===
//
for (let i = 0; i < 6; i++) {
const startAngle = (Math.PI / 3) * i + Math.PI / 2; //
const x1 = centerX + radius * Math.cos(startAngle);
const y1 = centerY + radius * Math.sin(startAngle);
const prevAngle = (Math.PI / 3) * (i - 1) + Math.PI / 2;
const prevX = centerX + radius * Math.cos(prevAngle);
const prevY = centerY + radius * Math.sin(prevAngle);
// prevX,prevY x1,y1 cornerX,cornerY
ctx.arcTo(prevX, prevY, x1, y1, cornerRadius);
}
//
const lastAngle = (Math.PI / 3) * 5 + Math.PI / 2;
const firstAngle = (Math.PI / 3) * 0 + Math.PI / 2;
const lastX = centerX + radius * Math.cos(lastAngle);
const lastY = centerY + radius * Math.sin(lastAngle);
const firstX = centerX + radius * Math.cos(firstAngle);
const firstY = centerY + radius * Math.sin(firstAngle);
ctx.arcTo(lastX, lastY, firstX, firstY, cornerRadius - cornerRadius / 2);
ctx.fill();
ctx.stroke();
}

Loading…
Cancel
Save