<html lang="en">
<head>
<meta charset=utf-8 />
<title>Set framerate using a high resolution timer</title>
</head>
<body>
<p>This example measures and sums deltas of time between consecutive frames of animation. It includes a <code>setFrameRateInFramesPerSecond</code> function you can use to reduce the number of frames per second of the main animation.</p>
<canvas id="myCanvas" width="700" height="350">
</canvas>
<script>
var canvas = document.querySelector("#myCanvas");
var ctx = canvas.getContext("2d");
var width = canvas.width, height = canvas.height;
var lastX = width * Math.random();
var lastY = height * Math.random();
var hue = 0;
// Michel Buffa : set the target framerate TRY TO CHANGE THIS VALUE AND SEE
// THE RESULT. Try 2 frames/s, 10 frames/s, 60 frames/s Normally there
// should be a limit at 60 frames/s in the browser's implementations.
setFrameRateInFramesPerSecond(60);
// for time based animation. DelayInMS corresponds to the target framerate
var now, delta, delayInMS, totalTimeSinceLastRedraw = 0;
// High resolution timer
var then = performance.now();
// start the animation
requestAnimationFrame(mainloop);
function setFrameRateInFramesPerSecond(frameRate) {
delayInMs = 1000 / frameRate;
}
// each function that is going to be run as an animation should end by
// asking again for a new frame of animation
function mainloop(time) {
// Here we will redraw something only if the time we want between frames has
// been elapsed
// Measure time, with high resolution timer
now = time;
// How long between the current frame and the previous one ?
delta = now - then;
// TRY TO UNCOMMENT THIS LINE AND LOOK AT THE CONSOLE
//console.log("delay = " + delayInMs + " delta = " + delta + " total time = " + totalTimeSinceLastRedraw);
// If the total time since the last redraw is > delay corresponding to the wanted
// framerate, then redraw, else add the delta time between the last call to line() by requestAnimFrame
// to the total time..
if (totalTimeSinceLastRedraw > delayInMs) {
// if the time between the last frame and now is > delay then we
// clear the canvas and redraw
ctx.save();
// Trick to make a blur effect: instead of clearing the canvas
// we draw a rectangle with a transparent color. Change the 0.1
// for smaller value will increase the blur...
ctx.fillStyle = "rgba(0,0,0,0.1)";
ctx.fillRect(0, 0, width, height);
ctx.translate(width / 2, height / 2);
ctx.scale(0.9, 0.9);
ctx.translate(-width / 2, -height / 2);
ctx.beginPath();
ctx.lineWidth = 5 + Math.random() * 10;
ctx.moveTo(lastX, lastY);
lastX = width * Math.random();
lastY = height * Math.random();
ctx.bezierCurveTo(width * Math.random(),
height * Math.random(),
width * Math.random(),
height * Math.random(),
lastX, lastY);
hue = hue + 10 * Math.random();
ctx.strokeStyle = "hsl(" + hue + ", 50%, 50%)";
ctx.shadowColor = "white";
ctx.shadowBlur = 10;
ctx.stroke();
ctx.restore();
// reset the total time since last redraw
totalTimeSinceLastRedraw = 0;
} else {
// sum the total time since last redraw
totalTimeSinceLastRedraw += delta;
}
// Store time
then = now;
// request new frame
requestAnimationFrame(mainloop);
}
</script>
</body>
</html>
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard 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. |