對於那些有興趣的人,我提出了兩個解決我的問題。
第一個想法是將每個點實際繪製爲一個角,使用畫布繪製一個整齊的角度。的演示可以看出:
http://jsfiddle.net/7BkyK/2/
var ctx = document.getElementById('canvas1').getContext('2d');
var points = [null, null, null];
for(var i=0; i<24; i++)
{
var width = 0.5 + i/2;
var m = 200;
var x = Math.cos(i/4) * 180;
var y = Math.sin(i/4) * 140;
points[0] = points[1];
points[1] = points[2];
points[2] = { X:x, Y:y};
if(points[0] == null)
continue;
var px0 = (points[0].X + points[1].X)/2;
var py0 = (points[0].Y + points[1].Y)/2;
var px1 = (points[1].X + points[2].X)/2;
var py1 = (points[1].Y + points[2].Y)/2;
ctx.beginPath();
ctx.lineWidth = width;
ctx.strokeStyle = "rgba(0,0,0,0.5)";
ctx.moveTo(m+px0,m+py0);
ctx.lineTo(m+points[1].X,m+points[1].Y);
ctx.lineTo(m+px1,m+py1);
ctx.stroke();
}
第二和更漂亮的解決方案,通過Shmiddty的建議,是貝塞爾曲線使用。事實證明,這是一個很好的解決方案:
http://jsfiddle.net/Ssrv9/1/
// 1.
// Varying line width, stroking each piece of line separately
var ctx = document.getElementById('canvas1').getContext('2d');
var points = [null, null, null, null];
for(var i=-1; i<25; i = i +1)
{
var width = 0.5 + i/2;
var m = 200;
var x = Math.cos(i/4) * 180;
var y = Math.sin(i/4) * 140;
points[0] = points[1];
points[1] = points[2];
points[2] = { X:x, Y:y};
if(points[0] == null)
continue;
var p0 = points[0];
var p1 = points[1];
var p2 = points[2];
var x0 = (p0.X + p1.X)/2;
var y0 = (p0.Y + p1.Y)/2;
var x1 = (p1.X + p2.X)/2;
var y1 = (p1.Y + p2.Y)/2;
ctx.beginPath();
ctx.lineWidth = width;
ctx.strokeStyle = "black";
ctx.moveTo(m+x0, m+y0);
ctx.quadraticCurveTo(m+p1.X, m+p1.Y, m+x1, m+y1);
ctx.stroke();
}
你最好的選擇可能是將要使用'bezierCurveTo'或'quadraticCurveTo'和'fill',而不是'stroke',它會使數學複雜化,但它可能是獲得理想結果的唯一方法。我已經能夠通過繪製多個省略號和每一步抵消/縮小它們來實現類似但不同的效果:http://jsfiddle.net/Shmiddty/ZvuQG/3/ – Shmiddty