旋转圆形画布javascript

我在使用 HTML5画布旋转圆圈时遇到了麻烦.我正在创建一个圆圈,作为装载工具.现在,装载部分(柠檬绿色)从45度标记处开始.我似乎无法弄清楚如何让这部分开始于0度标记.任何帮助将不胜感激.

<!DOCTYPE html>
<html>
  <head>
    <Meta charset="utf-8">
    <title>guage</title>
  </head>
  <body>
    <canvas id="canvas" width="300" height="300"></canvas>
    <p>Your browser does not support canvas.</p>
    <script src="http://127.0.0.1:3000/guage.js" charset="utf-8"></script>
  </body>
</html>

对于Javascript:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

//background color
ctx.fillStyle = "rgb(52,70,105)";
ctx.fillRect(0,canvas.height,canvas.width);

//drawing circle
ctx.beginPath();
ctx.linewidth = "20";
ctx.strokeStyle = "rgb(66,67)";
ctx.arc(150,125,75,2 * Math.PI);
ctx.stroke();

//generate random number between 0.01 and 1.99 since
//fill of circle is created with respect to radians.
var precision = 3;
var rando = parseFloat(Math.min(0.01 + (Math.random() * (1.99 - 0.01)),1.99).toFixed(2));
//calculate percent for display in center.
var percent = Math.round((rando * 500) / 10);

//Create fill for circle
var angle = 90 * (Math.PI / 180);
ctx.beginPath();
ctx.linewidth = "20";
ctx.strokeStyle = "rgb(24,242,92)";
ctx.arc(150,rando * Math.PI);
ctx.stroke();


//Create percent in middle of circle
ctx.font = "20px Verdana";
ctx.fillStyle = "rgb(24,92)";
ctx.fillText(percent +"%",135,135);

以下是浏览器中输出的屏幕截图:
有没有办法将圆圈旋转45度?或者你们中的任何一个人有另外一个建议,我完全忽略了在这个例子中尝试?

提前致谢!

解决方法

创建圆时,需要将起始弧设置为顶部. .arc()的第四个参数是起始位置.只需将其设置为-Math.PI / 2即可从顶部开始.

//Create fill for circle
var angle = 90 * (Math.PI / 180);
ctx.beginPath();
ctx.linewidth = "20";
ctx.strokeStyle = "rgb(24,-Math.PI/2,rando * Math.PI); // The fourth argument here
ctx.stroke();

https://jsfiddle.net/3oknq4km/1/供参考. (但固定在75%)

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...