<html>
<head>
<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
click twice
</body>
</html>
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
return window.setTimeout(callback, 1000 / 60);
};
})();
window.cancelRequestAnimFrame = ( function() {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout;
} )();
// example code from mr doob : http://mrdoob.com/lab/javascript/requestanimationframe/
var canvas, context;
var start = {y:0,x:0};
var end = {y:220,x:100};
var amount = 0;
var request;
var clickCount = true;
init();
function init() {
canvas = document.createElement( 'canvas' );
canvas.width = $(window).width();
canvas.height = $(window).height();
context = canvas.getContext( '2d' );
document.body.appendChild( canvas );
$('body').click(function(e) {
if(clickCount) {
context.clearRect(0, 0, canvas.width, canvas.height);
start.x = e.pageX;
start.y = e.pageY;
clickCount = false;
} else {
end.x = e.pageX;
end.y = e.pageY;
animate();
clickCount = true;
}
});
}
function animate() {
request = requestAnimFrame(animate, canvas);
draw();
}
function draw() {
console.log('running');
amount += 0.05; // change to alter duration
if (amount > 1) amount = 1;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(start.x, start.y); // a
// lerp : a + (b - a) * f
var newX = start.x + (end.x - start.x) * amount;
var newY = start.y + (end.y - start.y) * amount;
console.log('newX - '+newX +' - '+'newY - '+newY);
console.log('end.x - '+end.x+' - '+'end.y - '+end.y);
context.lineTo(newX , newY);
//context.lineTo(start.x + (end.x - start.x) * amount, start.y + (end.y - start.y) * amount);
context.stroke();
if(newX === end.x && newY === end.y) {
cancelRequestAnimFrame(request);
console.log('fin');
request = null;
amount = 0;
}
}
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |