2

I think an older version of canvas had a canvas.PenPos property that let you see where the current pen location is. I'm mostly going to use this for debugging purposes and to draw relatively (e.g. a line 50px long to the right of the current position). Is there any way to do this currently? If not, why did they remove it...?

The pen position I'm referring to is the virtual "pen" that a drawing context (that you would get for example by calling var ctx = canvas.getContext('2d')) uses, rather than a physical pen or mouse.

I am not trying to draw with a mouse. Again, this is not a physical pen I'm looking for, it's the virtual pen that is moved with ctx.moveTo(x,y)

LemonPi
  • 1,026
  • 9
  • 22
  • Possible duplicate of [Canvas HTML5 real mouse position](http://stackoverflow.com/questions/17130395/canvas-html5-real-mouse-position) – Dan Feb 14 '16 at 16:50
  • The pen position is not the mouse position; let me clarify that in the question. – LemonPi Feb 14 '16 at 16:53

1 Answers1

0

According to w3.org, there isn't a method that returns the pen position.

Because the .getContext("2d") method returns an object, you can store the pen position as properties and call them as needed:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

ctx.beginPath();

//Start position
ctx.x = 100;
ctx.y = 100;
ctx.moveTo(ctx.x, ctx.y);
ctx.x = ctx.x + 100;
ctx.y = ctx.y + 300;
ctx.lineTo(ctx.x, ctx.y);
ctx.stroke();

When creating an arc though, you would need to calculate the pen's new position afterward.

franklylately
  • 1,112
  • 10
  • 12