with p5.js
<html>
<head>
<title>Bouncy Ball</title>
<meta charset="UTF-8">
<script src="../lib/p5.js"></script>
<script src="sketch.js"></script>
</head>
<body style="padding:10px; margin:10px">
<h1>My Bouncy Ball</h1>
<div id="myContainer"></div>
</body>
</html>function draw() {
... change (x,y) position of circle
... change size of green rectangle
... change rotation angle of blue bar
... change color of ellipse
}
Change size
Rotate
Change color
Move ball
// Global variables: ball properties
var x, y; // ball position
var radius = 20; // ball radius
var xVelocity = 2.5; // x speed & direction
var yVelocity = 1.5; // y speed & direction
function setup() { // special Processing function
// make canvas the full size of window, minus the border padding
var myCanvas = createCanvas(windowWidth-40, windowHeight-100);
myCanvas.parent('myContainer');
main(); // invoke main
}
// Main function - run once before the "draw loop"
function main() {
x = width/2; // middle of stage
y = height/2; // middle of stage
}sketch.js
... global variables: ball properties
function setup() { ... }
function main() { ... }
function drawBall(x, y) {
// set colors
strokeWeight(2);
stroke(26, 0, 255); // dark blue
fill(0, 251, 255); // light blue
// set draw mode and draw the ball
ellipseMode(RADIUS);
ellipse(x, y, radius, radius);
}
function draw() {
drawBall(x,y);
}Note: Ellipsis (...) within code listing means that there's something that was either previously shown or is hidden because it's not relevant
... global variables: ball properties
function setup() { ... }
function main() { ... }
function drawBall(x, y) { ... }
function draw() {
drawBall(x,y);
x += xVelocity;
// check left & right boundaries
if (x < 0 || x > width) {
xVelocity = -xVelocity; // reverse direction
}
}
... global variables: ball properties
function setup() { ... }
function main() { ... }
function drawBall(x, y) { ... }
function draw() {
background(255,255,0); // yellow
drawBall(x,y);
x += xVelocity;
// check left & right boundaries
if (x < 0 || x > width) {
xVelocity = -xVelocity; // reverse direction
}
}... global variables: ball properties
function setup() { ... }
function main() { ... }
function drawBall(x, y) { ... }
function draw() {
background(255,255,0); // yellow
drawBall(x,y);
x += xVelocity;
// check left & right boundaries
if (x < radius || x > width-radius) {
xVelocity = -xVelocity; // reverse direction
}
}
... global variables: ball properties
var xVelocity = 2.5; // x speed & direction
var yVelocity = 1.5; // y speed & direction
function setup() { ... }
function main() { ... }
function drawBall(x, y) { ... }
function draw() {
background(255,255,255); // white
drawBall(x,y);
x += xVelocity;
// check left & right boundaries
if (x < radius || x > width-radius) {
xVelocity = -xVelocity; // reverse direction
}
y += yVelocity;
// check top & bottom boundaries
if (y < radius || y > height-radius) {
yVelocity = -yVelocity; // reverse direction
}
}
Please enter Bouncy Ball P5js for Exercise Name