JS Bin // source http://jsbin.com/xenudacuje
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
.box { | |
width: 100px; | |
height: 100px; | |
background: red; | |
cursor: move; | |
position: relative; | |
top: 50px; | |
left: 50px; | |
} | |
</style> | |
</head> | |
<body> | |
<div style="width: 300px;"> | |
<div id="box1" class="box"></div> | |
</div> | |
<script id="jsbin-javascript"> | |
function coroutine(f) { | |
var o = f(); // instantiate the coroutine | |
o.next(); // execute until the first yield | |
return function(x) { | |
o.next(x); | |
}; | |
} | |
var loop = coroutine(function*(_) { | |
while (true) { // wait for a mousedown | |
console.log("event"); | |
var event = yield _; | |
console.log("checking event: ", event); | |
if (event.type == 'mousedown') { | |
console.log("mouse down event"); | |
while (true) { // process mousemoves until a mouseup | |
event = yield _; | |
if (event.type == 'mousemove') move(event); | |
if (event.type == 'mouseup') break; | |
} | |
} | |
} | |
}); | |
$('#box').mousedown(loop); | |
$(window).mousemove(loop).mouseup(loop); | |
</script> | |
<script id="jsbin-source-css" type="text/css">.box { | |
width: 100px; | |
height: 100px; | |
background: red; | |
cursor: move; | |
position: relative; | |
top: 50px; | |
left: 50px; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function coroutine(f) { | |
var o = f(); // instantiate the coroutine | |
o.next(); // execute until the first yield | |
return function(x) { | |
o.next(x); | |
}; | |
} | |
var loop = coroutine(function*(_) { | |
while (true) { // wait for a mousedown | |
console.log("event"); | |
var event = yield _; | |
console.log("checking event: ", event); | |
if (event.type == 'mousedown') { | |
console.log("mouse down event"); | |
while (true) { // process mousemoves until a mouseup | |
event = yield _; | |
if (event.type == 'mousemove') move(event); | |
if (event.type == 'mouseup') break; | |
} | |
} | |
} | |
}); | |
$('#box').mousedown(loop); | |
$(window).mousemove(loop).mouseup(loop);</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.box { | |
width: 100px; | |
height: 100px; | |
background: red; | |
cursor: move; | |
position: relative; | |
top: 50px; | |
left: 50px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function coroutine(f) { | |
var o = f(); // instantiate the coroutine | |
o.next(); // execute until the first yield | |
return function(x) { | |
o.next(x); | |
}; | |
} | |
var loop = coroutine(function*(_) { | |
while (true) { // wait for a mousedown | |
console.log("event"); | |
var event = yield _; | |
console.log("checking event: ", event); | |
if (event.type == 'mousedown') { | |
console.log("mouse down event"); | |
while (true) { // process mousemoves until a mouseup | |
event = yield _; | |
if (event.type == 'mousemove') move(event); | |
if (event.type == 'mouseup') break; | |
} | |
} | |
} | |
}); | |
$('#box').mousedown(loop); | |
$(window).mousemove(loop).mouseup(loop); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment