Compare commits

..

2 Commits

@ -190,6 +190,10 @@ const titleArr = [
key: 'lodashExamples',
title: 'Lodash 经典用法',
},
{
key: 'canvasExample',
title: 'canvas示例',
},
];
// @/views/demo/**/*.vue

@ -0,0 +1,164 @@
<template>
<base-container>
<canvas
id="canvas-container"
width="600"
height="600"
></canvas>
</base-container>
</template>
<script setup>
import { onMounted } from 'vue';
onMounted(() => {
startDraw();
});
const startDraw = function () {
const canvas = {
width: 600,
height: 600,
el: document.getElementById('canvas-container'),
};
const ctx = canvas.el.getContext('2d');
console.log(ctx);
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
//
function drawBackground() {
const radialGradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, 600);
radialGradient.addColorStop(0, 'rgba(248, 239, 229, 1)');
radialGradient.addColorStop(1, 'rgba(252, 201, 169, 1)');
ctx.fillStyle = radialGradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
//
function drawHexagon(radius = 50, custom) {
const cornerRadius = 20; //
//
ctx.fillStyle = 'rgba(255, 248, 244, 1)';
ctx.strokeStyle = 'rgba(253, 201, 171, 1)';
ctx.lineWidth = 3;
ctx.beginPath();
if (custom) {
// === 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([]); //
} 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();
}
}
//
function drawTextAtCorners(radius, textArray) {
// 6
const text = textArray || ['医疗险', '定期寿险', '储蓄养老', '家财宠险', '重疾险', '意外险'];
const textColors = ['#333', '#333', '#333', '#333', '#333', '#333']; //
const textSizes = [20, 20, 20, 20, 20, 20]; //
for (let i = 0; i < 6; i++) {
const angle = (Math.PI / 3) * i + Math.PI / 2; //
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
//
ctx.font = `bold ${textSizes[i]}px Arial`;
ctx.fillStyle = textColors[i];
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
//
ctx.fillText(text[i], x, y);
}
}
drawBackground();
drawHexagon(200, true);
drawHexagon(150);
drawHexagon(100);
drawHexagon(50);
//
drawTextAtCorners(200, ['医疗险', '定期寿险', '储蓄养老', '家财宠险', '重疾险', '意外险']);
};
</script>
<style lang="scss" scoped>
#canvas-container {
width: 600px;
height: 600px;
margin: 0 auto;
}
</style>
Loading…
Cancel
Save