feat: canvas 示例

master
LCJ-MinYa 6 days ago
parent c7baf96b6f
commit ab12deba4c

@ -51,7 +51,7 @@ const startDraw = function () {
if (custom) {
// === custom true () ===
// 线
const gap = 40; //
const gap = 55; //
for (let i = 0; i < 6; i++) {
// ()
@ -122,25 +122,62 @@ const startDraw = function () {
}
//
function drawTextAtCorners(radius, textArray) {
// 6
const text = textArray || ['医疗险', '定期寿险', '储蓄养老', '家财宠险', '重疾险', '意外险'];
const textColors = ['#333', '#333', '#333', '#333', '#333', '#333']; //
const textSizes = [20, 20, 20, 20, 20, 20]; //
function drawTextAtCorners(radius, dataArray) {
//
const data = dataArray;
const titleSize = 20; //
const statusSize = 14; //
const rowGap = 10; //
for (let i = 0; i < 6; i++) {
const angle = (Math.PI / 3) * i + Math.PI / 2; //
const item = data[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];
// === 1. ===
ctx.font = `bold ${titleSize}px Arial`;
ctx.fillStyle = '#333';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(item.title, x, y);
// === 2. ( + ) ===
const isGuaranteed = item.status;
const statusText = isGuaranteed ? '已保障' : '未保障';
const activeColor = '#67C23A'; // 绿
const inactiveColor = '#909399'; //
const color = isGuaranteed ? activeColor : inactiveColor;
//
// y = + +
// y + 10 (沿) + 10 () + 7 () y + 27
const statusY = y + 27;
//
ctx.font = `normal ${statusSize}px Arial`;
const textWidth = ctx.measureText(statusText).width;
const iconRadius = 4;
const iconGap = 4; //
const totalWidth = iconRadius * 2 + iconGap + textWidth;
// X (使 [++] )
// = x -
// x = +
const iconCenterX = x - totalWidth / 2 + iconRadius;
// A. ()
ctx.beginPath();
ctx.arc(iconCenterX, statusY, iconRadius, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
//
ctx.fillText(text[i], x, y);
// B.
ctx.fillStyle = color;
ctx.textAlign = 'left'; //
ctx.textBaseline = 'middle';
const textX = iconCenterX + iconRadius + iconGap;
ctx.fillText(statusText, textX, statusY);
}
}
@ -151,7 +188,15 @@ const startDraw = function () {
drawHexagon(50);
//
drawTextAtCorners(200, ['医疗险', '定期寿险', '储蓄养老', '家财宠险', '重疾险', '意外险']);
const cornerData = [
{ title: '医疗险', status: false },
{ title: '定期寿险', status: true },
{ title: '储蓄养老', status: false },
{ title: '家财宠险', status: true },
{ title: '重疾险', status: false },
{ title: '意外险', status: false },
];
drawTextAtCorners(200, cornerData);
};
</script>

Loading…
Cancel
Save