feat: canvas 示例

master
LCJ-MinYa 6 days ago
parent a950a333a0
commit 9385c44cc9

@ -190,6 +190,39 @@ const startDraw = function () {
} }
} }
//
function drawRadar(maxRadius, data) {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
const value = data[i].value || 0;
// ( / 100) *
const currentRadius = (value / 100) * maxRadius;
const angle = (Math.PI / 3) * i + Math.PI / 2;
const x = centerX + currentRadius * Math.cos(angle);
const y = centerY + currentRadius * Math.sin(angle);
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.closePath();
// ->
const gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, maxRadius);
gradient.addColorStop(1, 'rgba(255, 240, 231, 0)');
gradient.addColorStop(0, 'rgba(248, 159, 107, 1)');
ctx.fillStyle = gradient;
ctx.fill();
//
// ctx.strokeStyle = 'rgba(252, 201, 169, 1)';
// ctx.lineWidth = 1;
// ctx.stroke();
}
drawBackground(); drawBackground();
drawHexagon(200, true); drawHexagon(200, true);
drawHexagon(150); drawHexagon(150);
@ -198,13 +231,17 @@ const startDraw = function () {
// //
const cornerData = [ const cornerData = [
{ title: '医疗险', status: false }, { title: '医疗险', status: true, value: 0 },
{ title: '定期寿险', status: true }, { title: '定期寿险', status: true, value: 100 }, // -> 100
{ title: '储蓄养老', status: false }, { title: '储蓄养老', status: false, value: 25 }, // -> 25
{ title: '家财宠险', status: true }, { title: '家财宠险', status: true, value: 100 }, // -> 100
{ title: '重疾险', status: false }, { title: '重疾险', status: true, value: 0 },
{ title: '意外险', status: false }, { title: '意外险', status: false, value: 0 },
]; ];
// (150)
drawRadar(150, cornerData);
drawTextAtCorners(200, cornerData); drawTextAtCorners(200, cornerData);
}; };
</script> </script>

Loading…
Cancel
Save