I want to create an arrowTo
function wi开发者_C百科th CanvasRenderingContext2D.prototype
. To do that, I need to get the coordinates of the last point, e.g.
//...
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)
//...
How can I retrieve them?
You'd have to keep track of them yourself. Or do the unthinkable and override moveTo/lineTo to keep track of the last coords via CanvasRenderingContext2D.prototype
.
This is a property that canvas HAS STORED somewhere and it should be available from a property (likely) because it can be difficult tracking last point coordinates.
For example when you draw an arc
ctx.arc(xc, yc, radius, starting_angle, ending_angle);
You don't have immediate information of the last point coordinates.
Of course this can be obtained in this case with
lastX = xc + radius*Math.cos(ending_angle);
lastY = yc + radius*Math.sin(ending_angle);
But it is irritating needing to include those computations when we know that canvas does remember last point.
If after the arc instruction you add a
ctx.lineTo(x, y);
And it does work. Therefore canvas has that last point stored somewhere and I can't understand why it is hidden for the programmer.
精彩评论