We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys,
I am working in P5.js to create an interactive website where, when the mouse is moved along the x-axis, a box responsively scales with it. This works on my screen as I have created it to fit my browser on fullscreen. However, depending on different screen sizes, the boxes don't scale correctly. When the browser window is small, I want the box to retain the same scale relative to the window. I am thinking I need to work in percentages instead of pixels but I'm wondering is there a better way to go about this?
Thanks for any help, and I can attach the code if need be (I don't have access to it right at this moment).
Thanks!
Answers
That's exactly right. Use the
width
andheight
variables and use percentages for all your positions and sizes.For example,
width * .5, height * .5
would give you the center of the screen.Hi Kevin Workman,
Thanks for your response. I've played around with the variables and percentages but it's not working out how I want it too.
Basically I want the boxes to scale the exact same way on every single screen size, relative to the screen size itself. Is this as easy as I"m imagining it to be?
Attaching the original Processing code (not P5) so you can have a look. I'm using video files. The variables and numbers seem a bit crazy for the scaling but that's just me experimenting — one goes small when one goes big, etc.
I'm not totally sure what you're asking because I don't know what this code does. I can't run it because I don't have any movie files. It would be easier if you posted an example that just used colored rectangles.
But anyway, the only thing that pops out to me is that you still have numbers like
930
and100
in there. If you want your sizes to be based on the width and height of the window, you'll need those numbers to be relative towidth
andheight
as well.Hey Kevin,
Yeah sorry about that, here's the code below with just boxes. On my screen it looks perfect, that's why I have such weird numbers because it was just trial and error to get it to work on just my screen size:
I have played around with width and height but it isn't working how I am expecting it too. Do you think there's some code I need to include that factors in the shortest/furthest distance the edge of the box is to the edge of the window, etc., and makes sure that is a constant no matter the window size? I hope this is making sense.
What is it doing that's different from what you expected?
Kf
KevinWorkman — whenever I used width and height in place of numbers I couldn't wrap my head around how extreme it was scaling the boxes.
kfrajer — this is exactly what I was looking for! I never knew about map(), thank you so much.
Hey kfrajer, I'm actually putting this code into P5.js, and errors come up on the lines with float — I replaced it with var but I'm not sure what I'm doing wrong? Any insight would be fantastic.
Thanks.
What does the error say?
Watch for integer divisions as it might do something that you don't want: rounding your operations. Instead perform casting or make it clear you are working with floats:
rect(width/3.0, height/2.0, w1,w1);
Notice the float nature of 3 and 2 above compared to my prev example.
Kf
remember to avoid using image with 5 parameters; better stick with 3 if you can. For images use resize() instead.
When you know the video beforehand, change its size to your need and store it (eg files vid640, vid1024...). Have multiple versions for typical screen sizes. I know when you want to be totally flexible towards screen sizes, that's not possible. But it slows things down.
Remark
instead of this
I'd define floats
width1000th=width/1000.0;
andheight1000th=height/1000.0;
in setup() and then just sayKfrajer, in p5 where I have
It is giving the error: "missing ';' before statement" — even though there is no semicolon missing from the previous line or anything.
Maybe I'm wrong but I thought that when using p5 you use var instead of float?
EDIT: Nevermind guys, there was another part of the code that was wrong and I was overlooking it. Sorry! Thanks again for all your help.