11

If I have an HTML Canvas context and do:

ctx.beginPath();
ctx.moveTo(10,10);
ctx.lineTo(20,30);
ctx.closePath();
ctx.stroke();

...a line is drawn between 10,10 and 20,30. Suppose I have this:

ctx.beginPath();
ctx.moveTo(10,10);
myFunction(ctx);

Is there any way for myFunction() to find out that the path 'cursor' is currently at 10,10?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Why would you need myFunction() to know the current location of 'cursor'? Note that the context works like a state machine. If you call "ctx.lineTo(20,30)" etc. at myFunction it should work. – Juho Vepsäläinen Jan 02 '11 at 19:22
  • @bebraw Because I want to write [`ctx.dashTo(x,y,...)`](http://stackoverflow.com/questions/4576724/dotted-stroke-in-canvas/4577326#4577326); for that, I need to know where I am in addition to where I am going in order to calculate various points along the way. – Phrogz Jan 02 '11 at 19:49
  • Righto. I think in that case you may be better off by implementing a wrapper for Context. Retain the data you need there and extend the API as you like. If you want I can provide you a simple example to get started better. :) – Juho Vepsäläinen Jan 02 '11 at 20:08
  • Just found this, https://github.com/millermedeiros/CanvasContext2DWrapper . Another one, http://code.google.com/p/canto-js/ . – Juho Vepsäläinen Jan 02 '11 at 20:09
  • @bebraw Thank you for the offer; I can wrap or intercept methods on the context effectively by myself if need be. However, if you know for certain that the data I seek can not be accessed on the context API, post that as an answer and I'll accept it. – Phrogz Jan 02 '11 at 20:10

1 Answers1

4

As far as I know there's no direct way to access arguments passed to various ctx methods (ie. moveTo in this case). You can wrap the Context API into a class of its own to do this, however. See [1] and [2] for reference.

Juho Vepsäläinen
  • 26,573
  • 12
  • 79
  • 105