som*_*ent 14 javascript html5 canvas html5-canvas
我想用CanvasRenderingContext2D.prototype创建一个arrowTo函数.要做到这一点,我需要得到最后一点的坐标.例如
//...
var ctx = someCanvas.getContext('2d');
ctx.moveTo(10,40);
//the coordinates of the last point are now (10,40)
ctx.lineTo(50,50);
//and now it's (50,50)
//...
Run Code Online (Sandbox Code Playgroud)
我怎样才能找回它们?
Lui*_*ety 10
这是一个画布已存储在某处的属性,它应该可以从属性(可能)中获得,因为跟踪最后一个点坐标可能很困难。
例如,当您绘制圆弧时
ctx.arc(xc, yc, radius, starting_angle, ending_angle);
Run Code Online (Sandbox Code Playgroud)
您没有最后一个点坐标的即时信息。
当然,这可以在这种情况下获得
lastX = xc + radius*Math.cos(ending_angle);
lastY = yc + radius*Math.sin(ending_angle);
Run Code Online (Sandbox Code Playgroud)
但是当我们知道画布确实记得最后一点时,需要包含这些计算是令人恼火的。
如果在 arc 指令之后添加一个
ctx.lineTo(x, y);
Run Code Online (Sandbox Code Playgroud)
它确实有效。因此,画布将最后一个点存储在某个地方,我不明白为什么它对程序员来说是隐藏的。