JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas"></canvas>
CSS
/* 适应窗口大小 rem标准模式*/
@media screen and (max-width: 374px) {
*{font-size: 16px;line-height: 16px;}
}
@media (min-width:375px) and (max-width: 413px) {
*{font-size: 18px;line-height: 18px;}
}
@media (min-width:414px) and (max-width: 479px) {
*{font-size: 20px;line-height: 20px;}
}
@media (min-width:480px) and (max-width:639px){
*{font-size: 24px;line-height: 24px;}
}
@media screen and (min-width: 640px) {
*{font-size: 32px;line-height: 32px;}
}
JavaScript
//绘制简易饼状图
drawCircle = function(canvas,percentage,number){
var clientWidth = document.documentElement.clientWidth;
var canvasWidth = Math.floor(clientWidth*200/720);
var innerR = canvasWidth * 0.7*0.5;
//var canvas = mainDom.J_telphone.find('.canvas').get(0),
var ctx;
canvas.setAttribute('width',canvasWidth+'px');
canvas.setAttribute('height',canvasWidth+'px');
if (canvas.getContext) {
ctx = canvas.getContext('2d');
}
ctx.translate(canvasWidth/2,canvasWidth/2);
//var percentage = 3/5;
ctx.beginPath();
ctx.arc(0, 0, innerR, 0, Math.PI*2,false);
ctx.lineWidth = 10;
ctx.strokeStyle = "#f7f7f7";
ctx.stroke();
// 设置字体
ctx.font = "24px";
// 设置对齐方式
ctx.textAlign = "center";
// 设置填充颜色
ctx.fillStyle = "#ff744d";
// 设置字体内容,以及在画布上的位置
ctx.fillText(number, 0, 2);
var start = 0,
end = 0;
ctx.beginPath();
ctx.arc(0, 0, innerR, Math.PI*3/2,Math.PI*(2*percentage-1/4),false);
ctx.lineWidth = 10;
ctx.strokeStyle = "#ff744d";
ctx.stroke();
};
drawCircle(document.getElementById('canvas'),3/5,45);