<html lang="en">
<head>
<meta charset="utf-8">
<title>First example of the Game Framework</title>
</head>
<body>
<div id="fps"></div>
<canvas id="myCanvas" width=400 height=400></canvas>
</body>
</html>
#myCanvas {
border:1px solid black;
background-color:lightgrey;
}
window.onload = init;
let canvas, ctx;
let fpsContainer;
function init() {
fpsContainer = document.querySelector('#fps');
fpsContainer.innerHTML = 'FPS:' + 0;
canvas = document.querySelector("#myCanvas");
ctx = canvas.getContext("2d");
let game = new Game(ctx);
game.start(fpsContainer);
}
// Dans game.js
class Game {
ctx;
joueur;
// Pour compter les FPS
fpsContainer;
frameCount=0;
lastTime;
fpsContainer;
fps=0;
constructor(ctx) {
this.ctx = ctx;
this.joueur = new Joueur(10, 10);
}
measureFPS(newTime){
// Pour la première invocation
if(this.lastTime === undefined) {
this.lastTime = newTime;
return;
}
//temps écoulé depuis la dernière seconde écoulée
const diffTime = newTime - this.lastTime;
if (diffTime >= 1000) {
this.fps = this.frameCount;
this.frameCount = 0;
this.lastTime = newTime;
//and display it in an element we appended to the
// document in the start() function
this.fpsContainer.innerHTML = 'FPS: ' + this.fps;
}
this.frameCount++;
};
drawAll() {
this.joueur.draw(this.ctx);
}
updateAll() {
this.joueur.move();
}
mainLoop(time) {
this.measureFPS(time);
// 1 - on efface le canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 2 - je dessine
this.drawAll();
// 3 - j'update les états
this.updateAll();
// call the animation loop every 1/60th of second
requestAnimationFrame(this.mainLoop.bind(this));
}
start(fpsContainer) {
this.fpsContainer = fpsContainer;
requestAnimationFrame(this.mainLoop.bind(this));
}
}
class Joueur {
x;
y;
constructor(x, y) {
this.x = x;
this.y = y;
}
draw(ctx) {
ctx.save();
ctx.fillStyle = 'red'
ctx.fillRect(this.x, this.y, 100, 100);
ctx.restore();
}
move() {
this.x+=0.1;
}
}
/* Ici pour scheduler
let seconds=0;
function scheduler() {
fpsContainer.innerHTML = seconds;
seconds++;
// ici faire des choses quand le temps écoulé est égal
// à n secondes, ou toutes les n secondes avec un modulo
}
setInterval(scheduler, 1000);
*/
Output
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. |