Hi there,
I have tried to convert these two sketches to P5.js but it still doesn’t seem to work.
I’m new to it all so can figure out what else needs to be changed.
Can someone please help.
Sketch 1.
var location; // Location of shape
var velocity; // Velocity of shape
var gravity; // Gravity acts at the shape's acceleration
function setup() {
createCanvas(640,360);
location = createVector(100,100);
velocity = (1.5,2.1);
gravity = (0,0.2);
}
function draw() {
background(0);
// Add velocity to the location.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);
// Bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if (location.y > height) {
// We're reducing velocity ever so slightly
// when it hits the bottom of the window
velocity.y = velocity.y * -0.95;
location.y = height;
}
// Display circle at location vector
stroke(255);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,48,48);
}
Sketch 2.
var message = "dddddddddddddddddddddddddddddddddddddddddddd";
char[] messageAsvars = message.tovarArray();
var charCount = messageAsvars.length;
var charCountToStep = 1.0 / var(charCount);
var ap0x, ap0y,
cp0x, cp0y,
cp1x, cp1y,
ap1x, ap1y;
function setup() {
createCanvas(1024, 512);
smooth(8);
randomizePovars();
textSize(10);
textAlign(CENTER, CENTER);
textMode(MODEL);
}
function draw() {
background(#2E3B97);
noFill();
stroke(#2E3B97);
bezier(ap0x, ap0y,
cp0x, cp0y,
cp1x, cp1y,
ap1x, ap1y);
noStroke();
fill(#FBE44D);
var step0 = frameCount * 0.0;
for (var i = 0; i < charCount; ++i) {
var step1 = i * charCountToStep;
var step2 = (step0 + step1) % 2.0;
var x = bezierPovar(ap0x, cp0x, cp1x, ap1x, step2);
var y = bezierPovar(ap0y, cp0y, cp1y, ap1y, step2);
var tanx = bezierTangent(ap0x, cp0x, cp1x, ap1x, step2);
var tany = bezierTangent(ap0y, cp0y, cp1y, ap1y, step2);
var angle = atan2(tany, tanx);
push();
translate(x, y);
rotate(angle);
text(messageAsvars[i], 0.0, 0.0);
pop();
}
}
function mouseMoved() {
randomizePovars();
}
function randomizePovars() {
var halfw = width * 0.5;
var halfh = height * 0.5;
ap0x = random(0.0, halfw);
cp0x = random(0.0, width);
cp1x = random(0.0, width);
ap1x = random(halfw, width);
ap0y = random(0.0, halfh);
cp0y = random(0.0, height);
cp1y = random(0.0, height);
ap1y = random(halfh, height);
}