Tried to make a small program drawing to an image with setContext. But when I use translate in draw() the first sprite I draw is misplaced. Maybe someone can explain why?
~~~
function setup()
viewer.mode = FULLSCREEN
buf = image(WIDTH,HEIGHT)
end
function draw()
spriteMode(CENTER)
translate(WIDTH/2,HEIGHT/2)
tint(233, 226, 80)
sprite(buf,0,0) — this is misplaced
translate(0,60) — but translate is after??
tint(131, 80, 233)
sprite(buf,0,0)
end
function touched(touch)
setContext(buf)
ellipse(touch.x-WIDTH/2,touch.y-HEIGHT/2,50)
setContext()
end
~~~
Comments
@tnlogy Translate is additive. The second translate is added to the first translate. What are you trying to do anyways.
@tnlogy Are both circles supposed to be directly under your finger as you move it. Or just one of them. I guess I’m just not understanding what you’re trying to do and what you’re say isn’t working.
PS. Playing with you code again. If I move my finger horizontally across the screen, one circle is above where my finger is and the second one is above that circle. Are you asking why the first circle isn’t under your finger and the second circle above your finger.
@tnology - not sure what you are trying to achieve, tried to add something to resolve this. Try running with and without the second translate.
Note: with second transpose present both circles are above the touch.
Thanks for debugging!
@tnlogy If you want the circle to be under your finger, try this. This works just fine on my iPhone 8.
For example this in your code
~~~
function draw()
sprite(buf,WIDTH/2,HEIGHT/2)
translate(0,60)
rect(0,0,50,50)
end
~~~
@tnlogy OK, the translate after the sprite is shifting the sprite but shouldn’t. Is that what you’re saying.
If so, you need to undo all the translates that have been done. So in this case, you do translate(0,-60) when you’re done. You do that for every translate to nullify each translate before doing another translate unless you want them to add..
@tnlogy Did you read my update to the above answer in the above post, or did I post it too late.
Here’s an example with multiple translates.
Thanks for the help and chat. I will report this as a bug in the runtime.
@tnlogy I don’t think it’s a bug. Sometimes if you have a lot of translates, you don’t want them totally zeroed out. You might want some translates offset from previous translates so you can subtract from any previous translates.
All,
This isn’t because you are making a sprite of screen size is it? That was my thought behind showing the screen rectangle?
@Bri_G No, it was the translate. It took me awhile to figure out what the issue was.
that last script works fine on my iPad and my iPhone.
A normal day in debugging code.