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

Y-axis up

Open kamildul opened this issue 3 years ago • 8 comments
trafficstars

Topic

Hello everyone, I nedd help, please tell me how i can shwitch mode to y-axis up? I tryied create solution to draw line in cartesian coordynation. I dont now how i can do it. I search in documentation and forum information for schwitch mode to y-axis up but i can find :-( Please help me. Thanks!

kamildul avatar Aug 08 '22 09:08 kamildul

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

welcome[bot] avatar Aug 08 '22 09:08 welcome[bot]

Hi! You could use scale(1, -1) before drawing to flip the y axis. Would that solve your problem?

davepagurek avatar Aug 08 '22 15:08 davepagurek

@davepagurek image I tried this with the shapes example and it seems like scale can not take negative numbers this comes from the canvas scale

test here to verify : https://www.w3schools.com/TAgs/tryit.asp?filename=tryhtml5_canvas_scale

so I looked for solutions with the HTML 5 canvas using @kamildul's question https://stackoverflow.com/questions/4335400/in-html5-canvas-can-i-make-the-y-axis-go-up-rather-than-down context.transform(1, 0, 0, -1, 0, canvas.height)

This fixed the issue on the w3 school demo, but you need to get the canvas reference on p5js. You can do this with the DOM API

good luck

DavidKozdra avatar Aug 09 '22 00:08 DavidKozdra

@DavidKozdra I think your issue is that just using scale keeps the origin in the same spot. Do you expect the origin to be in the bottom left? if so, in p5, this can be accomplished by translating before scaling:

translate(0, height)
scale(1, -1)

davepagurek avatar Aug 09 '22 01:08 davepagurek

@davepagurek that did work, I recommend mentioning that in the first reply, also my solution also works either way will fix the issue

DavidKozdra avatar Aug 09 '22 02:08 DavidKozdra

Hello gentlemen's! Thanks for response.

translate(0, height) scale(1, -1)

This solve my problem with y-axis, but still i have problem with image - my image is upside down in mirror

obraz

kamildul avatar Aug 09 '22 05:08 kamildul

When drawing your image, you can temporarily mirror the canvas back again:

push()
scale(1, -1)
image(yourImage, 0, 0)
pop()

davepagurek avatar Aug 09 '22 11:08 davepagurek

Also for completeness, @DavidKozdra one other option available to you is to use p5's applyMatrix function (documentation here) with the same parameters you provided to avoid having to go through drawingContext to get the underlying canvas context:

applyMatrix(1, 0, 0, -1, 0, height)

davepagurek avatar Aug 09 '22 12:08 davepagurek

Hi, if you need help with your own code, please direct your questions to the forum. We use GitHub issues mainly for bug reports and feature requests only. Thanks!

limzykenneth avatar Aug 11 '22 15:08 limzykenneth