-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
85 lines (65 loc) · 1.45 KB
/
sketch.js
File metadata and controls
85 lines (65 loc) · 1.45 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
let dvd_size = 160;
let dvd_w = dvd_size;
let dvd_h = dvd_size*(650/1100);
let buf = 0;
let pos_x = dvd_w + buf;
let pos_y = dvd_h + buf;
let vel_x,vel_y;
let r,g,b;
let speed = 3;
let img;
// Load the image.
function preload() {
img = loadImage('assets/DVD_video_logo.png');
}
function setup() {
createCanvas(windowWidth+1, windowHeight);
vel_x = speed/sqrt(2);
vel_y = speed/sqrt(2);
let brightness = 90
r = random(brightness,255);
g = random(brightness,255);
b = random(brightness,255);
recolor(img);
}
function recolor(img) {
img.loadPixels();
let new_r = random(0,255);
let new_g = random(0,255);
let new_b = random(0,255);
for (let i = 0; i < img.pixels.length; i += 4) {
img.pixels[i] = new_r; // New R
img.pixels[i + 1] = new_g; // New G
img.pixels[i + 2] = new_b; // New B
}
img.updatePixels();
}
function draw() {
let w = windowWidth+1;
let h = windowHeight;
background(0);
fill(r, g, b);
image(img,pos_x, pos_y, dvd_w, dvd_h);
pos_x += vel_x;
pos_y += vel_y;
collision = false;
if (pos_x + dvd_w >= w && vel_x > 0) {
vel_x *= -1;
collision = true;
}
if (pos_y + dvd_h>= h && vel_y > 0) {
vel_y *= -1;
collision = true;
}
if (pos_x <= 0 && vel_x < 0) {
vel_x *= -1;
collision = true;
}
if (pos_y <= 0 && vel_y < 0) {
vel_y *= -1;
collision = true;
}
if (collision) {
recolor(img);
}
}