Skip to content

Instantly share code, notes, and snippets.

@dmoss18
Created May 21, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmoss18/989df5a83705bcb9db2e to your computer and use it in GitHub Desktop.
Save dmoss18/989df5a83705bcb9db2e to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xenudacuje
<!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>
.box {
width: 100px;
height: 100px;
background: red;
cursor: move;
position: relative;
top: 50px;
left: 50px;
}
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