-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelizer.js
More file actions
216 lines (183 loc) · 5.72 KB
/
pixelizer.js
File metadata and controls
216 lines (183 loc) · 5.72 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
*Creates an instance of Pixelizer.
*
* is initiated on creation. to manually initiate pass option autoinit: false
* @param {*} [{
* canvas = null, //mandatory: DOM-Selector, eg '#canvas'
* src, //mandatory: Image Source as string, eg './image.svg'
* options: { // optional
* pixelRadius = 5, // size of pixels
* amount = 150, // amount of pixel
* threshhold = 150, //range: 1-10, threshhold for pixel values from original img
* colors = ["#FFFFFF"], // possible colors
* verticalDistribution = 5, // how far pixels fly around vertically
* horizontalDistribution = 5, // how far pixels fly around horizontally
* friction = false, // use friction: if true, speed is ignored
* frictionValue = 1, // range: 1-10, speed variety between pixels
* autoinit = true // wether animation starts on instance creation
* autostop = 100 // stops the rendering loop after give iterations
* speed = 100 // 1-1000: how fast pixels will get to destination
* } = {},
* }={}]
* @memberof Pixelizer
*/
export default class Pixelizer {
constructor({
canvas = null,
src,
options: {
pixelRadius = 0,
amount = 150,
threshhold = 150,
colors = ["#FFFFFF"],
verticalDistribution = 5,
horizontalDistribution = 5,
friction = false, // use friction
frictionValue = 1, // 1 - 10,
autoinit = true,
autostop = 100,
speed = 10, // 1-100 (fast - slow)
} = {},
} = {}) {
if (canvas === null || typeof src === "undefined") {
console.log(canvas, src);
return;
}
//### select div with canvas
this.canvas = document.querySelector(canvas);
this.cw = this.canvas.width = window.innerWidth;
this.ch = this.canvas.height = window.innerHeight;
//### get context
this.ctx = this.canvas.getContext("2d");
//### set colors
this.colors = colors;
//### set pixel max size
this.pixelRadius = pixelRadius;
//### set width / height of pixel flight in animation
this.verticalDistribution = verticalDistribution;
this.horizontalDistribution = horizontalDistribution;
//### set friction
this.frictionValue = frictionValue;
this.friction = friction;
//### set image source
this.src = src;
//### set image
this.img = new Image();
this.imgData = null;
//### set particle array
this.particles = [];
//### set amount of particles
this.amount = amount;
//### set threshhold for int value of a pixel in imgData: 0 - 255
this.threshhold = threshhold < 255 && threshhold > 0 ? threshhold : 150;
//### set speed
this.speed = speed;
//### set autostop
this.autstop = autostop;
this.autoStopCounter = 0;
//### initiate animation on creation
if (autoinit) {
this.init();
}
}
particle(x, y) {
let particle = {};
//### set random start position
particle.x = Math.random() * this.cw;
particle.y = Math.random() * this.ch;
//### set destinatin position
particle.dest = {
x: x,
y: y,
};
//### set pixel radius
particle.pixelRadius = Math.random() * this.pixelRadius + 2;
//### set random flight realm for pixel
particle.vx = (Math.random() - 0.5) * this.horizontalDistribution;
particle.vy = (Math.random() - 0.5) * this.verticalDistribution;
//### set acceleration of pixel
particle.accX = 0;
particle.accY = 0;
//### set friction to define how fast the pixel will end up in dest position
particle.friction = (Math.random() * this.frictionValue) / 100 + 0.94;
//### set color
particle.color = this.colors[
Math.floor(Math.random() * this.colors.length)
];
return particle;
}
//### draw a pixel
render(p) {
if (this.friction) {
// friction mode
p.accX = (p.dest.x - p.x) / 1000;
p.accY = (p.dest.y - p.y) / 1000;
p.vx += p.accX;
p.vy += p.accY;
p.vx *= p.friction;
p.vy *= p.friction;
p.x += p.vx;
p.y += p.vy;
} else {
p.accX = (p.dest.x - p.x) / this.speed;
p.accY = (p.dest.y - p.y) / this.speed;
p.x += p.accX;
p.y += p.accY;
}
this.ctx.fillStyle = p.color;
this.ctx.beginPath();
this.ctx.arc(p.x, p.y, p.pixelRadius, Math.PI * 2, false);
this.ctx.fill();
}
init() {
//### clear canvas
this.clearCanvas();
this.img.onload = () => {
this.drawImage();
this.createParticles();
this.renderLoop();
};
this.img.src = this.src;
}
drawImage() {
//### scale image
const scale = Math.min(
this.canvas.width / this.img.width,
this.canvas.height / this.img.height
);
const x = this.cw / 2 - (this.img.width / 2) * scale;
const y = this.ch / 2 - (this.img.height / 2) * scale;
//### draw image
this.ctx.drawImage(
this.img,
x,
y,
this.img.width * scale,
this.img.height * scale
);
this.imgData = this.ctx.getImageData(0, 0, this.cw, this.ch).data;
//this.clearCanvas();
this.ctx.globalCompositeOperation = "screen";
}
createParticles() {
for (let i = 0; i < this.cw; i += Math.ceil(this.cw / this.amount)) {
for (let j = 0; j < this.ch; j += Math.ceil(this.ch / this.amount)) {
if (this.imgData[(i + j * this.cw) * 4 + 3] > this.threshhold) {
this.particles.push(this.particle(i, j));
}
}
}
}
renderLoop() {
this.autoStopCounter++;
if (this.autoStopCounter > this.autstop) return;
window.requestAnimationFrame(this.renderLoop.bind(this));
this.clearCanvas();
for (let i = 0; i < this.particles.length; i++) {
this.render(this.particles[i]);
}
}
clearCanvas() {
this.ctx.clearRect(0, 0, this.cw, this.ch);
}
}