1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
var len = this.list.length; var canvas = document.getElementById("wheelCanvas"); var ctx = canvas.getContext("2d"); var canvasW = canvas.width; var canvasH = canvas.height;
var baseAngle = Math.PI * 2 / len; ctx.clearRect(0, 0, canvasW, canvasH); ctx.strokeStyle = "#ffb725";
for (var i = 0; i < len; i++) {
var angle = i * baseAngle; ctx.font = '16px Microsoft YaHei'; ctx.fillStyle = i % 2 === 0 ? '#ffdb37' : '#ffb725'; ctx.beginPath(); ctx.arc(canvasW * 0.5, canvasH * 0.5, 200, angle, angle + baseAngle, false);
ctx.arc(canvasW * 0.5, canvasH * 0.5, 10, angle + baseAngle, angle, true); ctx.stroke(); ctx.fill(); ctx.save(); ctx.fillStyle = "#e9311f"; var item = this.list[i]; var line_height = 24;
var translateX = canvasW * 0.5 + Math.cos(angle + baseAngle / 2) * 260; var translateY = canvasH * 0.5 + Math.sin(angle + baseAngle / 2) * 260;
if (!item.img) { ctx.font = '20px Microsoft YaHei'; translateX = canvasW * 0.5 + Math.cos(angle + baseAngle / 2) * 220; translateY = canvasH * 0.5 + Math.sin(angle + baseAngle / 2) * 220; } ctx.translate(translateX, translateY);
ctx.rotate(angle + baseAngle / 2 + Math.PI / 2);
ctx.fillText(item.name, -ctx.measureText(item.name).width / 2, 100);
var triangleEdge = canvasH * 0.27;
var imgMaxWidth = Math.sqrt(2 * triangleEdge * triangleEdge * (1 - Math.cos(baseAngle))); if (item.imgEl) { ctx.drawImage(item.imgEl, -imgMaxWidth * 0.5, canvasH * 0.27, imgMaxWidth, item.img .vertical_resolution * imgMaxWidth / item.img.horizontal_resolution); } ctx.restore();
|