-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch1_FastSine.html
70 lines (65 loc) · 1.52 KB
/
ch1_FastSine.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fast Sine Demonstration</title>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<style type="text/css">
#draw-target {
width: 480px;
height: 320px;
background-color: #000;
position: relative;
}
</style>
<script>
$(function(){
(function(){
// set up a lookup table: fastSin
var fastSin = function(steps) {
var table = [],
ang = 0,
angStep = (Math.PI * 2) / steps;
do {
table.push(Math.sin(ang));
ang += angStep;
} while (ang < Math.PI * 2);
return table;
};
var sinTable = fastSin(4096),
$drawTarget = $('#draw-target'),
divs = '',
i,
bars,
x = 0;
// drawGraph
var drawGraph = function(ang, freq, height) {
var height2 = height * 2;
for (var i = 0; i < 480; i++) {
bars[i].style.top = 160 - height
+ sinTable[(ang + (i * freq)) & 4095] * height + 'px';
bars[i].style.height = height2 + 'px';
}
};
for (i = 0; i < 480; i++) {
divs += '<div style="position:absolute;width:1px;'
+ 'height:400px;background-color:#0d0;top:0;'
+ 'left:' + i + 'px;"></div>';
}
$drawTarget.append(divs);
bars = $drawTarget.children();
setInterval(function(){
var ang = x * 50,
freq = 32 - (sinTable[(x * 20) & 4095] * 16),
height = 50 - (sinTable[(x * 10) & 4095] * 20);
drawGraph(ang, freq, height);
x++;
}, 20);
})();
});
</script>
</head>
<body>
<div id="draw-target"></div>
</body>
</html>