-
Notifications
You must be signed in to change notification settings - Fork 3
/
coroutine-dragging.html
46 lines (40 loc) · 1.86 KB
/
coroutine-dragging.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Javascript Coroutine Event Loop Demo</title>
<link rel="stylesheet" type="text/css" href="normalize.css" />
<link rel="stylesheet" type="text/css" href="prettify.css" />
<script src="prettify.js"></script>
<link rel="stylesheet" type="text/css" href="index.css" />
<script src="jquery-1.8.2.min.js"></script>
<script src="coroutine-dragging.js"></script>
</head>
<body onload="prettyPrint(); initDragging($('#box'), true);">
<p>This demo of a <a href="index.html">coroutine event loop</a> uses features of ECMAScript 6 and thus <strong>only works in recent versions of Firefox or Chrome</strong>.</p>
<p>Try dragging the box around, and observe how the code behaves:</p>
<div class="space">
<div id="box" class="box"></div>
</div>
<p></p>
<pre class="prettyprint">
var loop = coroutine(function*() {
var event;
<span class="not-dragging mousemove-1 mousedown-1">while (event = <span class="not-dragging yield">yield</span>) { // not dragging</span>
<span class="not-dragging mousedown-2">if (event.type == 'mousedown') {</span>
<span class="dragging mousemove-1 mouseup-1">while (event = <span class="dragging yield">yield</span>) { // dragging</span>
<span class="dragging mousemove-2">if (event.type == 'mousemove')</span>
<span class="dragging mousemove-3">move(event);</span>
<span class="dragging mouseup-2">if (event.type == 'mouseup')</span>
<span class="dragging mouseup-3">break;</span>
}
}
<span class="not-dragging mousemove-2 mousemove-3">// ignore mousemoves</span>
}
});
$('#box').mousedown(loop);
$(window).mousemove(loop).mouseup(loop);
</pre>
<p>You can <a href="index.html">read more about this</a>.</p>
</body>
</html>