We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everybody, I am new here and quite intimidated by all the wonderful work that is done here. I'm a a student and just discovering Processing. For a project I downloaded p5 because I'd like to run my files online. I've managed to do the transfer with my sketches from the normal processing code to p5.js, except for one, and I just can't figure out why it won't work. It's with the 'constrain' function, where I'd like to limit a dot (mouse coordinates) to a rectangle in the sketch, if that makes any sense. I copy both the codes here, hoping that someone can help me! Thank you for taking the time to read this.
Original code Processing:
float mx; float my; float easing = 0.05; int radius = 10; int edge = 235; int inner = edge + radius; void setup() { size(500, 700); noStroke(); ellipseMode(RADIUS); rectMode(CORNERS); }
void draw() {
if (abs(mouseX - mx) > 0.1) { mx = mx + (mouseX - mx) * easing; } if (abs(mouseY - my) > 0.1) { my = my + (mouseY- my) * easing; }
mx = constrain(mx, inner, width - inner);
my = constrain(my, inner, height - inner);
fill(196, 239, 234);
rect(edge, edge, width-edge, height-edge);
fill(146, 140, 211);
ellipse(mx, my, radius, radius);
}
Code that I typed in p5:
var mx; var my; var easing = 0.05; var radius = 10; var edge = 120; var inner = edge + radius;
function setup() { createCanvas(windowWidth, windowHeight); noStroke(); background(255,255,255) ellipseMode(RADIUS); rectMode(CORNERS); }
function draw() {
if (abs(mouseX - mx) > 0.1) { mx = mx + (mouseX - mx) * easing; } if (abs(mouseY - my) > 0.1) { my = my + (mouseY- my) * easing; }
x= constrain(mx, inner, width - inner);
y= constrain(my, inner, height - inner);
fill(196, 239, 234);
rect(edge, edge, width-edge, height-edge);
fill(146, 140, 211);
ellipse(mx, my, radius, radius);
}
Answers
Can you please edit your post to format your code? Read this: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Also, can you be more specific about exactly what's not working? What do you expect this code to do? What does it do instead?
The only error I get is due to a missing semicolon on line 11.
Thank you for your reply. I would like to transfer the first code, which runs how it should in Processing, to p5. By this I mean having a rectangle in the middle, in which a small circle, defined by the mouse coordinates, is contained. Even if you move the mouse 'beyond' the rectangle, it has to stay inside.
So this is the code working correctly in Processing:
And this is the code for p5, where the rectangle appears but not the circle, I can't see what I didn't rewrite correctly:
Time to debug your program. I'd start by simply logging your variables right before calling the
ellipse()
function:Doing this will show you that
mx
andmy
areNaN
.You aren't giving
mx
andmy
default values. In Java they'll be given default values of0
, but JavaScript will give them default values ofundefined
. Then when you pass that into theconstrain()
function, you get backNaN
.To fix your problem, you have to give your
mx
andmy
values. Probably either0
or the middle of the screen, depending on how you want your sketch to behave.Thank you for your quick response! As a newbie I'm not familiar with the
console.log
, what do I write between the" "
? I have givenmx
andmy
values and there is an improvement because the circle now appears, only it stays in the upper left corner, and doesn't follow the mouse..There's a very good article about Processing to p5.js transition below: O:-)
https://GitHub.com/processing/p5.js/wiki/Processing-transition
Before translating your sketch to p5.js, I've made some tweaks.
You can watch it online running under Pjs library at the link below too: :D
http://studio.SketchPad.cc/sp/pad/view/ro.9XkJm3bjTAxB2/latest
And finally the converted p5.js version. Check it online at the link below too: :-bd
http://Bl.ocks.org/GoSubRoutine/dc44acdaa0f05c1402ea33bd60a69da4
http://p5js.SketchPad.cc/sp/pad/view/ro.CQ494WRivD81VV/latest
Thank you so much! This looks good :)