p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

feature request: would like to see modelX(), modelY() functions that exist in Processing

Open dnassler opened this issue 12 years ago • 4 comments

I like using the matrix functions: translate, rotate, scale. But sometimes I need to know the coordinates of points drawn from the perspective of the main canvas co-ordinate space. So for example if I have a point A that was drawn using a set of matrix transforms and another point B that was drawn using a different set of matrix transforms, I might want to draw a line from point A to point B. To do that I need to have a common co-ordinate space. The functions modelX(), modelY() in the Processing reference do this, it seems, and it would be nice if p5js had these functions as well. See Processing reference: http://processing.org/reference/modelX_.html

dnassler avatar Sep 20 '14 14:09 dnassler

I thought about this more since your post on the forum... this is sort of difficult because p5.js does not keep track of the current matrix as it is transformed. this tradeoff was made to improve performance, so we could take advantage of built-in transform, scale, etc methods in html5 canvas without having to do a lot of matrix multiplication. you can read more of that discussion here: https://github.com/lmccart/p5.js/issues/243

my suggestion for now would be to manually keep track of the matrix as you transform it. http://stackoverflow.com/questions/21717001/html5-canvas-get-coordinates-after-zoom-and-translate

you could also try wrapping some library like this in: https://github.com/ardcore/CanvasMemory

but it looks like this one hasn't been updated since 2011 so I don't know. there could be something more recent out there.

lmccart avatar Sep 20 '14 14:09 lmccart

thanks for the suggestion and thoughts. I'm going to temporarily close this as it's not likely doable in the near future with canvas renderer, it would just be too slow. we can reopen and revisit this when we begin work on webgl/3d.

lmccart avatar Nov 21 '14 22:11 lmccart

Is this issue still not doable ? Would love to see these features on p5js

MAKIO135 avatar May 17 '20 22:05 MAKIO135

I keep missing modelX/Y/Z() (and screenX/Y/Z), too. I think this closed request is specific to P2D. (In P2D, modelX/Y() and screenX/Y() would give the same, x,y values, right?)

If so, are these limitations specific to canvas/P2D? And so, in WEBGL mode, modelX(), modelY(), modelZ() might be possible? (Or maybe trivial?) Are screenX, screenY, and screenZ (z-buffer depth?) similarly possible or already available in WEBGL mode? (Is some "view" or "projection" matrix readily available?)

If "getMatrix()" (or "getGlMatrix()") were a thing, it might be used with applyMatrix() to reproduce (WebGL) transformations outside of a push()/pop(), right? I read on one of the links below that "getMatrix()" would be problematic...but, again, is that a P2D/canvas-specific problem? Otherwise, is this basically doable? Maybe there is already an easy way to grab the current (3D) transformation matrix, (p5.Matrix.mat4 or something)?

Even with a function like "getMatrix()", I'm not sure how one would get transformed coordinates: If only, in addition to "getMatrix()", applyMatrix() could perhaps be applied to a 3-vector, e.g., pv.set(0,0,0); pv.applyMatrix(); A vector whose x,y,z values could then be read out?

FWIW! Kinda out of my depth, but happy for any hints or advice!

https://github.com/processing/p5.js/issues/243 (push()/pop() discussion) https://github.com/processing/p5.js/issues/377 (this issue, modelX(), modelY() feature request) https://github.com/processing/p5.js/issues/1553 (screenX() screenY(), screenZ() feature request) https://github.com/processing/p5.js/issues/1873 (modelX(), modelY() feature request) https://github.com/processing/p5.js/issues/4337 (matrix functions...completed!)

mkmori avatar Jul 12 '20 21:07 mkmori