-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
58 lines (51 loc) · 1.08 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
let x,y; /* x and y positions */
let xspeed,yspeed; /* speed the logo moves */
let dvd; /* dvd logo image */
let r,g,b; /* rgb color values */
/* Load the image into the dvd variable */
function preload() {
dvd = loadImage("dvd_logo.png");
}
/* The image will start at a random x and y position */
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
x = random(width);
y = random(height);
xspeed = 1;
yspeed = 1;
pickColor();
}
/* Each time the image hits a wall, it will change color */
function pickColor() {
r = random(256);
g = random(256);
b = random(256);
}
/* Draws the image and handles wall collision */
function draw() {
background(0);
tint(r,g,b);
image(dvd, x, y);
x += xspeed;
y += yspeed;
if (x + dvd.width >= width) {
xspeed *= -1;
x = width - dvd.width;
pickColor();
}
else if (x <= 0) {
xspeed *= -1;
x = 0;
pickColor();
}
if (y + dvd.height >= height) {
yspeed *= -1;
y = height - dvd.height;
pickColor();
}
else if (y <= 0) {
yspeed *= -1;
y = 0;
pickColor();
}
}