简体   繁体   English

在html5画布中创建非填充弧?

[英]Create a non filled arc in html5 canvas?

(simplified) : I've have created this simple code to create an arc (简化):我已经创建了这个简单的代码来创建arc

jsbin jsbin

The result is : 结果是:

在此处输入图片说明

but I don't want it to be full filled with red. 但我希望它充满红色。

I need something like this : (edited with photoshop) 我需要这样的东西:(用photoshop编辑)

在此处输入图片说明

(simple words : , if it was a div , then style="border:solid 1px red;background-color:white" ) (简单的单词:,如果是div,则style="border:solid 1px red;background-color:white"

question : 题 :

How can I enhance my code to do that by not filling the whole shape ? 如何通过不填充整个形状来增强代码以实现此目的? Is there any property which allows me to do this ? 有什么财产可以让我这样做吗?

A simple workaround for this is to draw 2 arcs: One red with a lineWidth of 10 and then another one on top in white and a lineWidth of 6 or 8. 一个简单的解决方法是绘制2条弧:一个红色,其lineWidth为10,然后另一个红色在顶部,白色为lineWidth ,为6或8。

Note: Odd numbers might yield better results (11 and 9/7): 注意:奇数可能会产生更好的结果(11和9/7):

  var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');

  var x = canvas.width / 2;
  var radius = 55;

  var y = canvas.height / 2;

  context.beginPath();

  var startAngle = 1.1 * Math.PI;
  var endAngle = 1.9 * Math.PI;
  var delta = 0.005 * Math.PI;
  context.arc(x, y, radius, startAngle, endAngle  , false);
  context.lineWidth = 11;

  context.strokeStyle = 'red';
  context.stroke();

  context.beginPath();
  context.arc(x, y, radius, startAngle+delta, endAngle-delta, false);
  context.lineWidth = 9;

  context.strokeStyle = 'white';
  context.stroke();

jsbin jsbin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM