如何在HTML5画布中的两个文本对象之间绘制二次曲线?

时间:2010-07-21 13:19:05

标签: javascript html5 canvas bezier

每个科目的目标。

代码片段:

var canvas= document.getElementById('myCanvas');
var ctx= canvas.getContext('2d');   
canvas.width= 520;
canvas.height= 405;
ctx.font = "15pt Verdana";
ctx.lineWidth = 1;

// text 1
ctx.fillText("me and my dog puddie", 210, 90);  
// text 2
ctx.fillText("you and many many crazy nuts", 210, 130); 
// draw a quadratic bezier curved line between the these 2 text blocks
ctx.strokeStyle = "rgb(65,60,50)";
ctx.beginPath();
ctx.moveTo(210,100);
ctx.bezierCurve(230,250,130,160,160,100);
ctx.stroke();

/* outcome:
no line were drawn between these two text objects
*/

我对二次曲线的理解非常有限

2 个答案:

答案 0 :(得分:3)

更改行

ctx.bezierCurve(230,250,130,160,160,100);

ctx.bezierCurveTo(230,250,130,160,160,100);

你应该好好去。

答案 1 :(得分:3)

您应该将quadraticCurveTo用于二次曲线,bezierCurveTo用于三次曲线。

ctx.beginPath();
ctx.moveTo(210,100); // move to the start
ctx.quadraticCurveTo(230, 130, 160, 100); // draw quadractic curve
ctx.stroke();

请参阅“画布教程”中的Bezier and quadratic curves