-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
187 lines (154 loc) · 6.23 KB
/
index.html
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
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body,
html {
margin: 0;
padding: 0;
}
canvas {
position: absolute;
background: black;
width: 100%;
height: 100vh;
top: 0;
touch-action: none;
left: 0;
}
#fileInput {
position: absolute;
z-index: 999;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<input type="file" name="" id="fileInput" onchange="readFile(event)">
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var height, width, startHeight = 0, startWidth = 0;
var particleArray = [];
const nParticle = 5000;
var imageArr;
window.onload = () => {
canvas.height = window.innerHeight / 1.5
canvas.width = window.innerWidth / 1.5
}
const getRandom = (min, max) => min + Math.random() * (max - min)
class Particle {
constructor() {
this.x = getRandom(startWidth, width + startWidth)
this.y = startHeight
this.velocity = getRandom(0, 0.5)
this.size = getRandom(1, 2.5)
this.offset = 0
}
update() {
var tx = parseInt(this.x - startWidth)
var ty = parseInt(this.y - startHeight)
this.offset = imageArr[ty][tx][1]
this.y += this.velocity + (255 - this.offset) / 100
if (this.y >= height) {
this.y = startHeight
this.x = getRandom(startWidth, width + startWidth)
}
ctx.beginPath()
ctx.fillStyle = imageArr[ty][tx][0]
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI)
ctx.fill()
}
}
function animate() {
// ctx.drawImage(myImage, startWidth, startHeight, width, height);
max = 0
ctx.globalAlpha = 0.05
ctx.fillStyle = 'rgb(0,0,0)'
// ctx.globalAlpha = 0.2
ctx.fillRect(startWidth, startHeight, width, height);
for (let p of particleArray) {
p.update()
// ctx.globalAlpha = p.cc * 0.1
}
// console.log(max)
requestAnimationFrame(animate)
}
var max = 0
var contrast = 0.9
contrast *= 2.55; // or *= 255 / 100; scale integer percent to full range
var factor = (255 + contrast) / (255.01 - contrast); //add .1 to avoid /0 error
function relativeContrast(r, g, b, a) {
x = Math.sqrt(r*r*0.299 + g*g*0.587 + b*b*0.114)
// x = (r + g + b) / 3
// r = factor * (r-128)*128
// g = factor * (g-128)*128
// b = factor * (b-128)*128
// if(x>max) max=x
return [`rgb(${r},${g},${b})`, x]
}
function readFile(ev) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const reader = new FileReader();
let files = ev.target.files
if (files.length) reader.readAsDataURL(files[0])
reader.onload = function (e) {
const myImage = new Image();
myImage.src = e.target.result
myImage.onload = () => {
height = myImage.height
width = myImage.width
// console.log('width', width, canvas.width)
// console.log('height', height, canvas.height)
if (width > canvas.width && height > canvas.height) {
let canvasRatio = canvas.width / canvas.height
let imRation = width / height;
if (height > width) {
height = canvas.height
width = imRation * height
} else {
width = canvas.width
height = width / imRation
}
}
if (width > canvas.width) {
let myImageratio = height / width
width = canvas.width
height = width * myImageratio
}
if (height > canvas.height) {
let myImageratio = width / height
height = canvas.height
width = height * myImageratio
}
if (height < canvas.height) startHeight = (canvas.height - height) / 2.0
if (width < canvas.width) startWidth = (canvas.width - width) / 2.0
height = parseInt(height)
width = parseInt(width)
console.log('current', height, width)
ctx.drawImage(myImage, startWidth, startHeight, width, height);
const pixel = ctx.getImageData(startWidth, startHeight, width, height).data
ctx.clearRect(0, 0, canvas.width, canvas.height)
// ctx.putImageData(pixel, startWidth, startHeight)
imageArr = new Array(height).fill(null).map((_, i) => new Array(width).fill(null).map((_, j) => relativeContrast(...pixel.subarray(4 * (i * width + j), 4 * (i * width + j + 1)))))
// console.log('done', height, width)
particleArray = new Array(nParticle).fill(null).map(_=>new Particle())
animate()
}
};
}
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-140377407-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-140377407-1');
</script>
</body>
</html>