画布弧形绘制奇怪的形状 - coffeescript

时间:2014-01-25 18:40:49

标签: javascript html5 canvas coffeescript drawing

@.ctx.lineWidth = 20
@.ctx.moveTo(i.x, i.y)
@.ctx.arc(i.x, i.y, 3, 0, Math.PI * 2)

enter image description here

为什么代码会使上面的图像成为什么原因?

2 个答案:

答案 0 :(得分:1)

在创建路径之前使用beginPath,并在创建路径后使用closePath 由于closePath ...将路径关闭回第一个点,因此根据您所寻求的内容,您可能需要在关闭路径之前或之后进行描边或填充。

答案 1 :(得分:1)

我尝试了你的弧形版本,我发现很难理解你的实际问题。因此,我制作了两个版本,以便直观地向您展示正在发生的事情。

你可以在这看看他们! 更新的JSFIDDLE http://jsfiddle.net/hqB6b/2/

<强> HTML

First with the line inside.

<canvas id="ex" width="300" height="300">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>

Second with NO line inside!

<canvas id="example" width="300" height="300">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>

<强> JS

var example = document.getElementById('example');
var ctx = example.getContext('2d');
var i = {x:100,
    y:100}
ctx.strokeStyle = '#ff0000';
ctx.lineWidth = 1;

ctx.moveTo(i.x, i.y)
//HERE BEGINPATH IS USED AFTER MOVETO
ctx.beginPath();
ctx.arc(i.x, i.y, 50, 0, Math.PI * 2)
ctx.stroke();


var ex = document.getElementById('ex');
var ct = ex.getContext('2d');
var i = {x:100,
    y:100}
ct.strokeStyle = '#ff0000';
ct.lineWidth = 1;

//HERE BEGINPATH IS USED BEFORE MOVETO
ct.beginPath();
ct.moveTo(i.x, i.y)
ct.arc(i.x, i.y, 50, 0, Math.PI * 2)
ct.stroke();
相关问题