0
点赞
收藏
分享

微信扫一扫

html5 canvas 原生 javascript 实现 饼状图 扇型方向 顺时针 语法和逆时针语法

香小蕉 2022-08-02 阅读 113


逆时针饼状图话扇子 60%

图片

html5 canvas  原生 javascript 实现  饼状图 扇型方向 顺时针 语法和逆时针语法_html

code:

var c = document.getElementById("canvasmain");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.arc(100, 100, 30, Math.PI * 1.5, Math.PI * 1.5 - Math.PI * 2 * 60 / 100, true);
ctx.closePath();
ctx.fillStyle = "#bbb";
ctx.fill();

  • ctx.arc(100, 100, 30, Math.PI * 1.5, Math.PI * 1.5 - Math.PI * 2 * 60 / 100, true);
  • true逆时针
  • 开始1.5,结束Math.PI * 1.5 - Math.PI * 2 * 60 / 100

顺时针百分比扇子

html5 canvas  原生 javascript 实现  饼状图 扇型方向 顺时针 语法和逆时针语法_html_02

code

             ctx.beginPath();
ctx.moveTo(100, 100);
ctx.arc(100, 100, 30, Math.PI * 1.5, Math.PI * 1.5 * 60 / 100, false);
ctx.closePath();
ctx.fillStyle = "#ddd";
ctx.fill();

  • ctx.arc(100, 100, 30, Math.PI * 1.5, Math.PI * 1.5  * 60 / 100, false);
  • false顺时针
  • 开始1.5,结束Math.PI * 1.5 * 60 / 100

 示例:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test1</title>
</head>
<style>
body {
background: #000;
margin: 0px;
padding: 0px;
}

#main {
position: absolute;
width: 200px;
height: 200px;
}
</style>

<body>
<div id="main"></div>

</body>

<script>
var por = {
init: function () {
var main = document.getElementById("main");
main.innerHTML = '<canvas id="canvasmain" width="' + main.offsetWidth + '" height="' + main.offsetHeight + '"></canvas>';
var vmain = document.getElementById("canvasmain");
var c = document.getElementById("canvasmain");
var ctx = c.getContext("2d");

//逆时针 扇形图
// ctx.beginPath();
// ctx.moveTo(100, 100);
// ctx.arc(100, 100, 30, Math.PI * 1.5, Math.PI * 1.5 - Math.PI * 2 * 60 / 100, true);
// ctx.closePath();
// ctx.fillStyle = "#bbb";
// ctx.fill();

//顺时针
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.arc(100, 100, 30, Math.PI * 1.5, Math.PI * 1.5 * 60 / 100, false);
ctx.closePath();
ctx.fillStyle = "#ddd";
ctx.fill();

};
por.init();



</script>

</html>

 

 

 

 

持续更新

 

 

 

 

 

举报

相关推荐

0 条评论