feat: canvas 示例

master
LCJ-MinYa 7 days ago
parent c3aac354e1
commit c01dae8426

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

@ -0,0 +1,128 @@
<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.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 线 ===
if (custom) {
// 线10px 线 + 5px
ctx.setLineDash([5, 8]);
ctx.lineDashOffset = 0;
ctx.stroke();
ctx.setLineDash([]);
} else {
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