This repository was archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy path15-pixel-move.html
More file actions
59 lines (51 loc) · 1.93 KB
/
15-pixel-move.html
File metadata and controls
59 lines (51 loc) · 1.93 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pixel Move</title>
<link rel="stylesheet" href="../include/style.css">
</head>
<body>
<header>
Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
</header>
<canvas id="canvas" width="400" height="400"></canvas>
<aside>Move mouse on canvas element (processor intensive).</aside>
<script src="../include/utils.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
mouse = utils.captureMouse(canvas);
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
//draw some stripes: red, green, and blue
for (var i = 0; i < canvas.width; i += 10) {
for (var j = 0; j < canvas.height; j += 10) {
context.fillStyle = (i % 20 === 0) ? "#f00" : ((i % 30 === 0) ? "#0f0" : "#00f");
context.fillRect(i, j, 10, 10);
}
}
var imagedata = context.getImageData(0, 0, canvas.width, canvas.height),
pixels = imagedata.data;
//pixel iteration
for (var y = 0; y < imagedata.height; y += 1) {
for (var x = 0; x < imagedata.width; x += 1) {
var dx = x - mouse.x,
dy = y - mouse.y,
dist = Math.sqrt(dx * dx + dy * dy),
offset = (x + y * imagedata.width) * 4,
r = pixels[offset],
g = pixels[offset + 1],
b = pixels[offset + 2];
pixels[offset] = Math.cos(r * dist * 0.001) * 256;
pixels[offset + 1] = Math.sin(g * dist * 0.001) * 256;
pixels[offset + 2] = Math.cos(b * dist * 0.0005) * 256;
}
}
context.putImageData(imagedata, 0, 0);
}());
};
</script>
</body>
</html>