微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

旋转圆形画布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%)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐