Compare commits

..

No commits in common. '9385c44cc9c0303ffd50c5cc06b7cbb00a579144' and 'c7baf96b6f512a162256da222d7cd46b0b5d3973' have entirely different histories.

@ -51,18 +51,9 @@ const startDraw = function () {
if (custom) {
// === custom true () ===
// 线
const gap = 40; //
const gap = 40; //
for (let i = 0; i < 6; i++) {
// gap""
let startGap = gap;
let endGap = gap;
// i=1: 寿() -> () = gap
if (i === 1) startGap -= 15;
// i=4: () -> () = gap
if (i === 4) endGap -= 15;
// ()
const angle1 = (Math.PI / 3) * i + Math.PI / 2;
const x1 = centerX + radius * Math.cos(angle1);
@ -78,16 +69,16 @@ const startDraw = function () {
const dy = y2 - y1;
const dist = Math.sqrt(dx * dx + dy * dy);
// gap
if (dist > startGap + endGap) {
// 2*gap
if (dist > 2 * gap) {
const ux = dx / dist;
const uy = dy / dist;
//
const sx = x1 + ux * startGap;
const sy = y1 + uy * startGap;
const ex = x2 - ux * endGap;
const ey = y2 - uy * endGap;
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);
@ -131,98 +122,28 @@ const startDraw = function () {
}
//
function drawTextAtCorners(radius, dataArray) {
//
const data = dataArray;
const titleSize = 20; //
const statusSize = 14; //
const rowGap = 10; //
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 item = data[i];
const angle = (Math.PI / 3) * i + Math.PI / 2; //
const angle = (Math.PI / 3) * i + Math.PI / 2; //
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
// === 1. ===
ctx.font = `bold ${titleSize}px Arial`;
ctx.fillStyle = '#333';
//
ctx.font = `bold ${textSizes[i]}px Arial`;
ctx.fillStyle = textColors[i];
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();
// B.
ctx.fillStyle = color;
ctx.textAlign = 'left'; //
ctx.textBaseline = 'middle';
const textX = iconCenterX + iconRadius + iconGap;
ctx.fillText(statusText, textX, statusY);
//
ctx.fillText(text[i], x, y);
}
}
//
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();
drawHexagon(200, true);
drawHexagon(150);
@ -230,19 +151,7 @@ const startDraw = function () {
drawHexagon(50);
//
const cornerData = [
{ title: '医疗险', status: true, value: 0 },
{ title: '定期寿险', status: true, value: 100 }, // -> 100
{ title: '储蓄养老', status: false, value: 25 }, // -> 25
{ title: '家财宠险', status: true, value: 100 }, // -> 100
{ title: '重疾险', status: true, value: 0 },
{ title: '意外险', status: false, value: 0 },
];
// (150)
drawRadar(150, cornerData);
drawTextAtCorners(200, cornerData);
drawTextAtCorners(200, ['医疗险', '定期寿险', '储蓄养老', '家财宠险', '重疾险', '意外险']);
};
</script>

Loading…
Cancel
Save