git @ Cat's Eye Technologies The-Dipple / a07bc1d
Improve the mouse-tracking demo A LOT. Chris Pressey 10 years ago
2 changed file(s) with 51 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
1616 </canvas>
1717 </div>
1818
19 <p id="info"></p>
20
1921 <div>
20 <button id="dump">Dump</button>
22 <button onclick="mt.start();">Record</button>
23 <button onclick="mt.stop();">Stop</button>
24 <button onclick="mt.replay();">Replay</button>
25 <button onclick="mt.clear();">Clear</button>
2126 </div>
2227
2328 <p>As the mouse is moved across the canvas, its position
24 at each point in time is recorded. Clicking the "Dump" button displays
25 these events in the Javascript console.</p>
29 at each point in time is recorded. Clicking the "Replay" button replays
30 these events, showing a red square where the pointer was.</p>
2631
2732 </body>
2833 <script>
2934 var mt = MouseTracker(document.getElementById('canvas'));
30 mt.start();
31 document.getElementById('dump').onclick = function (e) { mt.dump(); }
3235 </script>
55 elem.onmousemove = function(event) {
66 events.push(event);
77 };
8 elem.style.background = "blue";
9 };
10
11 self.stop = function() {
12 elem.onmousemove = null;
13 elem.style.background = "white";
14 };
15
16 self.clear = function() {
17 events = [];
818 };
919
1020 self.offset = function() {
1929 }
2030
2131 return [left, top];
32 };
33
34 self.replay = function() {
35 var i = 0;
36 var t = 0.0;
37 var base = events[0].timeStamp;
38 var offs = self.offset();
39 var interval;
40 interval = setInterval(function() {
41 t += 0.01;
42 s = "z";
43 var x = undefined;
44 var y = undefined;
45 var sec;
46 while (i < events.length && ((events[i].timeStamp - base) / 1000.0) < t) {
47 x = events[i].pageX - offs[0];
48 y = events[i].pageY - offs[1];
49 sec = (events[i].timeStamp - base) / 1000.0;
50 s += " (" + i + ") " + x + ", " + y + " @ " + sec;
51 i++;
52 }
53 if (x !== undefined) {
54 var ctx = elem.getContext("2d");
55 ctx.clearRect(0, 0, elem.width, elem.height);
56 ctx.fillStyle = "red";
57 ctx.fillRect(x - 4, y - 4, 8, 8);
58 }
59 //document.getElementById('info').innerHTML = s;
60 if (i >= events.length) {
61 clearInterval(interval);
62 return;
63 }
64 }, 10);
2265 };
2366
2467 self.dump = function() {