使用Canvas画布结合JavaScript绘制以下图形,写出源代码和运行截图_百 ...
发布网友
发布时间:2022-04-26 00:20
我来回答
共2个回答
热心网友
时间:2022-04-07 12:21
<!DOCTYPE html>
<html>
<head>
</head>
<body align="center">
<canvas id="drawing" width="400" height="400">A drawing of someing!</canvas>
<script type="text/javascript">
//绘制饼图
var drawCircle = function(canvasId, data_arr, color_arr){
var drawing = document.getElementById(canvasId);
if(drawing.getContext) {
var context = drawing.getContext('2d');
var radius = drawing.height/2 -20,//半径
ox = radius +20, oy = radius +20;//圆心
var width = 30, height = 10, //图例宽高
posX = ox * 2 +20, posY = 30;//图例位置
var textX = posX + width + 5, textY = posY + 10;//文本位置
var startAngle = 0, endAngle = 0;//起始、结束弧度
context.strokeStyle = 'Purple';
context.lineWidth = 3;
context.strokeRect(0, 0, drawing.width, drawing.height);
for(var i=0, len=data_arr.length; i<len; i++) {
//绘制饼图
endAngle += data_arr[i] * 2*Math.PI;
context.fillStyle = color_arr[i];
context.beginPath();
context.moveTo(ox, oy);
context.arc(ox, oy, radius, startAngle, endAngle, false);
context.closePath();
context.fill();
startAngle = endAngle;
}
}
};
var init = function(){
var data_arr = [0.05, 0.25, 0.6, 0.1],
color_arr = ['#00FF21', '#FFAA00', '#00AABB', '#FF4400'];
drawCircle('drawing', data_arr, color_arr);
};
init();
</script>
</body>
</html>
热心网友
时间:2022-04-07 13:39
jmGraph 支持小程序的canvas库
https://github.com/jiamao/jmgraph
可以试下这个库,每个方块都可以做成一个对象,像操作dom一样。