-
Notifications
You must be signed in to change notification settings - Fork 53
/
part16d_pixelation.html
63 lines (42 loc) · 1.34 KB
/
part16d_pixelation.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
<!DOCTYPE html>
<html>
<title>Creative Coding Yea!</title>
<head>
<meta charset="utf-8">
<script language="javascript" src="js/creative_coding.js"></script>
<script language="javascript" src="js/canvas.js"></script>
<link rel="stylesheet" href="css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
</head>
<body>
<script>
var ctx = createCanvas("canvas1");
// define the sample size
var sample_size = randomInt(20, 80);
//load an image
var img = new Image();
img.src = 'img/stevie-nicks2.jpg';
function draw(){
if(frameCount%5 == 0) sample_size = randomInt(2, 80);
// loop through the rows from to to bottom of page
ctx.drawImage(img, 0, 0, w, h);
pixelate(sample_size, ctx)
}
function pixelate(sample_size, _ctx){
var context = _ctx || ctx;
var sourceBuffer32 = new Uint32Array(context.getImageData(0,0,w,h).data.buffer);
context.clearRect(0,0,w,h);
for(var y = 0; y < h; y += sample_size){
for(var x = 0; x < w; x += sample_size){
var pos = (x + y * w);
var b = (sourceBuffer32[pos] >> 16) & 0xff;
var g = (sourceBuffer32[pos] >> 8) & 0xff;
var r = (sourceBuffer32[pos] >> 0) & 0xff;
context.fillStyle = rgb(r,g,b);
context.centreFillRect(x, y, sample_size+1, sample_size+1);
}
}
}
</script>
</body>
</html>