-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
177 lines (160 loc) · 3.54 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
automatically pixelizes either a video or image
only supports .mp4 videos
Controls
press:
p or P to pause / unpause
m or M to mute / unmute
, to decrease pixel size
. to increase pixel size
1 to switch between square / circular pixels
simply change the string in load to get to the file
*/
// playlists
p5.disableFriendlyErrors = true;
let obj;
let pixelSize = 8;
let isCircle = false;
let isVid = false;
let vW = 0; // video heigh and width, automatically set
let vH = 0;
let load = "data/bubz.mp4"
let isMute = false;
let isPause = false;
function preload() {
let ext = load.split(".").pop();
// determines if we are looking at a video
if (ext === "mp4") {
isVid = true;
} else {
isVid = false;
}
if (isVid) {
obj = createVideo(load);
} else {
obj = loadImage(load);
}
print(isVid);
}
function setup() {
print(vW, vH);
if (isVid) {
createCanvas(vW, vH);
} else {
createCanvas(obj.width, obj.height);
}
print(width);
print(height);
noStroke();
background(0);
//gif.position(0,0);
if (isVid) {
obj.loop();
obj.hide();
}
}
getVideoDimensionsOf(load).then(({
width,
height
}) => {
vW = width;
vH = height;
print("vd" + " " + vW + " " +vH);
reset();
//setup();
});
function reset() {
createCanvas(vW, vH);
obj.loop();
obj.hide();
}
/*
function reset() {
preload();
setc();
}
*/
function draw() {
Pixelate(0, 0, width, height, pixelSize);
}
function keyPressed() {
if (key === '1') {
isCircle = !isCircle;
}
if (key === ',') {
pixelSize = max(4, pixelSize - 2);
}
if (key === '.') {
pixelSize = min(50, pixelSize + 2);
}
if (key === 'm' || key === 'M') {
isMute = !isMute;
if (isVid) {
if (isMute) {
obj.volume(0);
} else {
obj.volume(1);
}
}
}
if (key === 'p' || key === 'P') {
isPause = !isPause;
if (isVid) {
if (isPause) {
obj.pause();
} else {
obj.play();
}
}
}
if (key === ' ') {
save("capture_pixel.jpg");
}
}
function Pixelate(startX, startY, endX, endY, pSize) {
obj.loadPixels();
for (let x = startX; x < endX; x += pixelSize) {
for (let y = startY; y < endY; y += pixelSize) {
let offset = ((y * width) + x) * 4;
if (isCircle) {
//math for determining the pixel density
let dim = 80;
fill(obj.pixels[offset] - dim, obj.pixels[offset + 1] - dim, obj.pixels[offset + 2] - dim);
rect(x, y, pSize, pSize);
fill(obj.pixels[offset], obj.pixels[offset + 1], obj.pixels[offset + 2]);
ellipse(x + pSize / 2, y + pSize / 2, pSize, pSize);
} else {
fill(obj.pixels[offset], obj.pixels[offset + 1], obj.pixels[offset + 2]);
rect(x, y, pSize, pSize);
}
}
}
}
function unPixelate() {
background(0);
image(obj, 0, 0);
}
/**
Returns the dimensions of a video asynchrounsly.
@param {String} url Url of the video to get dimensions from.
@return {Promise} Promise which returns the dimensions of the video in 'width' and 'height' properties.
*/
function getVideoDimensionsOf(url) {
return new Promise(function(resolve) {
// create the video element
let video = document.createElement('video');
// place a listener on it
video.addEventListener("loadedmetadata", function() {
// retrieve dimensions
let height = this.videoHeight;
let width = this.videoWidth;
// send back result
resolve({
height: height,
width: width
});
}, false);
// start download meta-datas
video.src = url;
});
}