-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdraw.js
More file actions
81 lines (65 loc) · 1.57 KB
/
draw.js
File metadata and controls
81 lines (65 loc) · 1.57 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
var buf = new Float32Array(500);
for (var i=0; i<buf.length; i++) {
buf[i] = 0;
}
function gebi(id) {
return document.getElementById(id);
}
var el = gebi("wave");
var wave = new Wave(el, {buf: buf, waveStyle: 'darkgreen' });
wave.zoomby(0);
var area = wave.widthbox; /* draggable area, excluding scrollbars */
gebi("zin").onclick = function() { wave.zoomby(200); };
gebi("zout").onclick = function() { wave.zoomby(50); };
var dragging = false;
var last;
function drag (e) {
/* It's possible to get here with no buttons pressed, e.g. by
* dragging outside the window. We should abort in this case.
* Sadly, the buttons property doesn't yet exist everywhere, so
* explicitly check for zero.
*/
if (e.buttons===0) {
stopdrag();
return;
}
var where = wave.getPos(e);
var p = where, q = last;
if (p.pos > q.pos) {
/* swap */
var tmp = p;
p = q;
q = tmp;
}
var count = q.pos - p.pos;
if (count) {
for (var i=p.pos; i<=q.pos; i++) {
/* interpolate */
var f = (i - p.pos) / count;
var val = q.value * f + p.value * (1-f);
buf[i] = val;
}
} else
buf[where.pos] = where.value;
last = where;
wave.invalidate();
}
function down (e) {
area.addEventListener('mousemove',
drag,
false);
last = wave.getPos(e);
buf[last.pos] = last.value;
wave.invalidate();
}
function stopdrag() {
area.removeEventListener('mousemove',
drag,
false);
}
area.addEventListener('mousedown',
down,
false);
area.addEventListener('mouseup',
stopdrag,
false);