We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey everyone, I've made this code.
int A;
int speed;
PFont a;
String letters = "";
int back = 102;
void setup() {
size(500,500);
A = 150;
speed = 5;
a = createFont("OCRA",50,true);
frameRate(random(60));
}
void draw() {
background(100,100,100);
fill(random(0,255),random(0,255),random(0,255));
textFont(a);
A = A + speed;
if(A > 125) {
speed = -speed;
}
if(A < 0) {
speed = -speed;
}
text(letters,A,350);
}
void keyPressed() {
if ((key == ENTER) || (key == RETURN)) {
letters = letters.toLowerCase();
println(letters); // Print to console to see input
if (letters.equals("black")) {
back = 0;
} else if (letters.equals("gray")) {
back = 204;
}
letters = ""; // Clear the variable
} else if ((key > 31) && (key != CODED)) {
// If the key is alphanumeric, add it to the String
letters = letters + key;
}
}
I'm trying to get it to work on a website, I've start changing it but it's not work, any help?
var A;
var speed;
String letters = "";
var back = 102;
function setup() {
var myCanvas=createCanvas(650, 450);
myCanvas.parent('myCanvas');
A = 150;
speed = 5;
textSize(100);
frameRate(random(60));
}
function draw() {
background(100,100,100);
fill(random(0,255),random(0,255),random(0,255));
A = A + speed;
if(A > 125) {
speed = -speed;
}
if(A < 0) {
speed = -speed;
}
textFont("OCRAStd");
text(letters,100,225);
}
function keyPressed() {
if ((key == ENTER) || (key == RETURN)) {
letters = letters.toLowerCase();
println(letters); // Print to console to see input
if (letters.equals("black")) {
back = 0;
} else if (letters.equals("gray")) {
back = 204;
}
letters = ""; // Clear the variable
} else if ((key > 31) && (key != CODED)) {
// If the key is alphanumeric, add it to the String
letters = letters + key;
}
}
Thanks for your time :)
Answers
What's it meant to do?
When you type a keyPressed function should happen so it shows the letter you pressed and then like 'glitches' it
and what does it do currently? what is wrong with it?
Nothing loads up on the webpage..
Check if your html code is proper.
Why have you used a random frameRate?
String letters = "";
should bevar letters = "";
Then you have a problem within your keyPressed()-function. Not that familiar with p5js, so not really sure how they handle everything, but 'key" cannot be compared to a number, should be string or char.
You could either use
keyCode
or somthing likekey.charCodeAt(0)
instead of "key".Even in Processing, key is not compared with ENTER, keyCode is.
How do you type cast in JavaScript? In Java, you just say
(int)key
and copare with numbers.Java's
(int) fraction
corresponds to JS'~~fraction
orfraction | 0
. :PHowever, key is a String in p5.js. Therefore, we need method charCodeAt() before comparing it to a UTF-16/ASCII value:
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
Java automatically coerces key's
char
to keyCode'sint
datatype. ~O)@GoToLoop I don't get it, ENTER is not coded in Processing? I thought it was.
Processing has many key constants: ENTER, TAB, LEFT, etc.
What I meant is we can mix
char
&int
datatypes.Thus we can compare key & keyCode. :ar!
I meant doesn't ENTER key set the value of key to CODED?
I've used a random Frame rate, as the animation is suppose to be different each time the code is ran :) I'll try making some changes now :)
But it could possibly lead to very slow frameRates like 1.
Nope! But Regardless, why would you worry about it? Just use keyCode in place of key. As long as you don't need to distinguish upper & lower case letters. :-j
And you keep on confusing me. So, keyCode is set to the value of key, except that even if key is uppercase, keyCode is still lowercase? In any case, I don't understand why it has to be so confusing?
It is always uppercase. At least in Processing. L-)
While key can be both cases. B-)
If you guys are lost. I definitely am, but yeah that's part of the experiment (different frame rates )
Still, as I said, random frameRate can lead to very slow frameRate like 1 or 2.