-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (105 loc) · 4.19 KB
/
app.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
var show = false;
function draw() {
const canvas = document.querySelector('#image-display');
const canvasW = 900;
const canvasH = 900;
const numOfImages = 7;
let pixelationFactor = 80;
// TODO - Pick random image with no repeats!
const getRandomImgNum = () => {
return Math.floor(Math.random() * numOfImages);
};
if (canvas.getContext) {
const ctx = canvas.getContext("2d", { willReadFrequently: true });
const img = new Image();
img.onload = () => {
// Scale image to the size of the canvas
let scaledImgW, scaledImgH;
if (img.naturalWidth > img.naturalHeight) {
// scale image dependent on width
const ratio = img.naturalHeight / img.naturalWidth;
scaledImgW = canvasW;
scaledImgH = scaledImgW * ratio;
canvas.setAttribute('width', scaledImgW);
canvas.setAttribute('height', scaledImgH);
}
else {
// scale image dependent on height
const ratio = img.naturalWidth / img.naturalHeight;
scaledImgH = canvasH;
scaledImgW = scaledImgH * ratio;
canvas.setAttribute('width', scaledImgW);
canvas.setAttribute('height', scaledImgH);
}
const centerX = (canvas.width - scaledImgW) / 2;
const centerY = (canvas.height - scaledImgH) / 2;
ctx.drawImage(img, centerX, centerY, scaledImgW, scaledImgH);
setBackground(img);
addNextImageBtn(img, getRandomImgNum());
addShowBtn();
let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
function animate() {
requestAnimationFrame(animate);
if (show) {
ctx.drawImage(img, centerX, centerY, scaledImgW, scaledImgH);
cancelAnimationFrame(animate);
} else {
// Draw the pixelized image
ctx.imageSmoothingEnabled = false;
for (let y = 0; y < canvas.height; y += pixelationFactor) {
for (let x = 0; x < canvas.width; x += pixelationFactor) {
// extract the position of the sample pixel
const pixelPos = (x + y * canvas.width) * 4;
ctx.fillStyle = `rgba(
${imgData[pixelPos]},
${imgData[pixelPos + 1]},
${imgData[pixelPos + 2]},
${imgData[pixelPos + 3]}
)`;
ctx.fillRect(x, y, pixelationFactor, pixelationFactor);
}
}
}
// setTimeout(() => { pixelationFactor -= 2; console.log('yes') }, 2000);
}
animate();
// Update pixelation based on a timer
// let pixelChange;
// if (pixelationFactor >= 4) {
// if (!pixelChange) {
// pixelChange = setInterval(() => { pixelationFactor -= 4 }, 800);
// }
// }
// else {
// clearInterval(pixelChange);
// show = true;
// }
};
// Randomize picture on refresh
img.src = `static/img/${getRandomImgNum()}.jpg`;
}
}
function setBackground(img) {
const imgURL = new URL(img.src);
document.body.style.backgroundImage = `url(${imgURL.pathname})`;
}
/*
TODO - The next button can't be clicked too fast. Something to do with changing the img.src??
- Improve the speed of next image loading
*/
function addNextImageBtn(img, randomImgNum) {
const nextImageBtn = document.querySelector("#btn-next-image");
nextImageBtn.addEventListener("click", ev => {
img.src = `static/img/${randomImgNum}.jpg`;
});
}
function addShowBtn() {
const showBtn = document.querySelector("#btn-show");
showBtn.addEventListener("click", ev => {
show = !show;
showBtn.textContent = show ? 'Hide' : 'Show';
});
}
window.onload = ev => {
draw();
};