javascript – 使用HTML5画布创建一个条形圆,但条形之间的空间不均匀

前端之家收集整理的这篇文章主要介绍了javascript – 使用HTML5画布创建一个条形圆,但条形之间的空间不均匀前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个循环的进度条(虽然它不再是一个吧,不是吗?).在这个cricle周围有垂直于圆圈的细条.现在的问题是,我的代码不会产生均匀间距的条形.这是代码和结果的图像:
  1. function MH5PB(canvasId,//the id of the canvas to draw the pb on
  2. value,//a float value,representing the progress(ex: 0.3444)
  3. background,//the background color of the pb(ex: "#ffffff")
  4. circleBackground,//the background color of the bars in the circles
  5. integerColor,//the color of the outer circle(or the int circle)
  6. floatColor //the color of the inner circle(or the float circle)
  7. )
  8. {
  9. var canvas = document.getElementById(canvasId);
  10. var context = canvas.getContext("2d");
  11. var canvasWidth = canvas.width;
  12. var canvasHeight = canvas.height;
  13. var radius = Math.min(canvasWidth,canvasHeight) / 2;
  14. var numberOfBars = 72;
  15. var barThickness = 2;
  16.  
  17. //margin from the borders,and also the space between the two circles
  18. var margin = parseInt(radius / 12.5) >= 2 ? parseInt(radius / 12.5) : 2;
  19.  
  20. //the thickness of the int circle and the float circle
  21. var circleThickness = parseInt((radius / 5) * 2);
  22.  
  23. //the outer radius of the int circle
  24. var intOuterRadius = radius - margin;
  25. //the inner radius of the int circle
  26. var intInnerRadius = radius - margin - circleThickness;
  27.  
  28. //the outer radius of the float circle
  29. var floatOuterRadius = intOuterRadius - margin - circleThickness;
  30. //the inner radius of the float circle
  31. var floatInnerRadius = floatOuterRadius - circleThickness;
  32.  
  33. //draw a bar,each degreeStep degrees
  34. var intCircleDegreeStep = 5;
  35. // ((2 * Math.PI * intOuterRadius) / (barThickness + 10)) //
  36. // this area is the total number of required bars //
  37. // to fill the intCircle.1px space between each bar//
  38. var floatCircleDegreeStep = 360 / ((2 * Math.PI * floatOuterRadius) / (barThickness + 10));
  39.  
  40. context.lineWidth = barThickness;
  41. context.strokeStyle = circleBackground;
  42. //draw the bg of the outer circle
  43. for(i = 90; i < 450; i+=intCircleDegreeStep)
  44. {
  45. //since we want to start from top,and move cw,we have to map the degree
  46. //in the loop
  47. cxOuter = Math.floor(intOuterRadius * Math.cos(i) + radius);
  48. cyOuter = Math.floor(intOuterRadius * Math.sin(i) + radius);
  49. cxInner = Math.floor(intInnerRadius * Math.cos(i) + radius);
  50. cyInner = Math.floor(intInnerRadius * Math.sin(i) + radius);
  51. context.moveTo(cxOuter,cyOuter);
  52. context.lineTo(cxInner,cyInner);
  53. context.stroke();
  54. }
  55. }

编辑:哦,而且行也没有消除锯齿.你知道为什么吗?
我还应该解释一下,这个进度条由两部分组成.外圈(在提供的图像中可见)和内圈.外圆是百分比的整数部分的量(即45在45.98%中的45),内圆是百分比的非整数部分的量(即,在45.98%中为98).因此,您现在知道intCircle和floatCircle是什么:)

解决方法

看来你正在将学位传递给Math.sin和Math.cos.这些函数需要弧度.例如,
  1. // i degrees to radians.
  2. Math.sin(i * (Math.PI / 180));

猜你在找的JavaScript相关文章