-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
executable file
·120 lines (96 loc) · 2.69 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Logo bouncing around screen like the DVD bouncing logo "screensaver"
* Based on the Processing example "Circle Collision with Swapping Velocities"
* by Ira Greenberg, which was based on Keith Peter's ActionScript animation
* "Making Things Move!"
*/
var BACKGROUND_COLOR = 0;
var logoImage;
var logo;
var waitress;
function setup() {
createCanvas(windowWidth,windowHeight);
imageMode(CENTER);
rectMode(CENTER);
logoImage = loadImage("assets/dvd-logo-white-on-black.png");
logo = new ScreensaverLogo(logoImage);
tint(200, 0, 200);
waitress = millis() + 10000;
}
function draw() {
background(BACKGROUND_COLOR);
logo.update();
logo.display();
logo.checkBoundaryCollision();
}
//////////////////////////////
var ScreensaverLogo = function(logo, v) {
if(! this instanceof ScreensaverLogo) return new ScreensaverLogo(logo, v);
var that = this;
that.logo = logo;
that.position = createVector(logo.width/2, logo.height/2);
//default velocity to 0.9
v = (typeof v === 'undefined') ? 1.0 : v;
that.velocity = createVector(v, v);
//changes color each time it hits a boundary
that.colors = [
color(200, 0, 200),
color(0, 200, 200),
color(200, 200, 0),
color(240, 140, 0)
];
that.colorIndex = 0;
that.update = function() {
var that = this;
that.position.add(that.velocity);
};
that.checkBoundaryCollision = function() {
var that = this;
var hitBoundary = false;
var w = that.logo.width;
var h = that.logo.height;
if (that.position.x > width-w/2) {
hitBoundary = true;
that.position.x = width-w/2;
that.velocity.x *= -1;
}
else if (that.position.x < w/2) {
hitBoundary = true;
that.position.x = w/2;
that.velocity.x *= -1;
}
else if (that.position.y > height-h/2) {
hitBoundary = true;
that.position.y = height-h/2;
that.velocity.y *= -1;
}
else if (that.position.y < h/2) {
hitBoundary = true;
that.position.y = h/2;
that.velocity.y *= -1;
}
if(hitBoundary) {
that.colorIndex = ++that.colorIndex % that.colors.length;
tint(that.colors[that.colorIndex]);
}
};
this.display = function() {
var that = this;
image(that.logo, that.position.x, that.position.y);
// //testing with rectangle:
// noStroke();
// fill(200, 200, 200, 100);
// rect(that.position.x, that.position.y, that.logo.width, that.logo.height);
};
}
function windowResized() {
waitress = millis() + 2000;
if (fullscreen()) {
resizeCanvas(displayWidth, displayHeight);
} else {
resizeCanvas(windowWidth,windowHeight);
}
cursor();
showing = true;
background(0);
}