<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<canvas id="myCanvas" width=300 height=300></canvas>
</body>
</html>
window.addEventListener("load", init);
// window.onload = init;
/*
window.onload = function() {
...
}
*/
var canvas, ctx, w, h;
// objet singleton
var monstre = {
x:100,
y:10,
v:0,
angle: 0,
color: "red",
w: 100,
h:100,
move: function() {
this.x += this.v;
},
draw: function(ctx) {
// Bonne pratique : on sauve le contexte au début
// on le restaure
ctx.save();
// bonne pratique
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
// couleur en CSS3
ctx.fillStyle = this.color;
ctx.fillRect(0, 0, this.w, this.h);
// yeux
ctx.fillStyle = "yellow";
ctx.fillRect(20, 20, 10, 10);
ctx.beginPath();
ctx.arc(70, 25, 10, 0, 2* Math.PI);
ctx.fillStyle = "lightgreen";
ctx.fill();
// nez
//
// che
ctx.restore();
}
}
function init() {
// appelée quand la page est chargée
console.log("page chargée");
// On récupère le canvas avec l'API du DOM ou
// la selector API (recommandée)
// canvas = document.getElementById("myCanvas");
// syntaxe : selecteur CSS3
canvas = document.querySelector("#myCanvas");
// On récupère le contexte graphique
ctx = canvas.getContext("2d"); // autre possibilité webgl
w = canvas.width;
h = canvas.height;
//drawSomething(r1);
anime();
//ctx.fillRect(0, 0, 20, 20)
//ctx.strokeRect(150, 150, 50, 100);
document.addEventListener("keydown", traiteToucheEnfoncee);
document.addEventListener("keyup", traiteToucheRelachee);
}
// ECOUTEURS
function traiteToucheEnfoncee(evt) {
console.log("touche enfoncee code = " + event.keyCode);
switch(evt.keyCode) {
case 39:
// fleche à droite
monstre.x += monstre.v;
break;
case 37:
// fleche à gauche
monstre.x -= monstre.v;
break;
}
}
function traiteToucheRelachee(evt) {
console.log("touche relachee");
}
// BOUCLE D'ANIMATION
function anime(time) {
// 1 - on efface le canvas
ctx.clearRect(0, 0, w, h);
// 2 - je dessine mes objets
drawCercle(100, 100, 50, "blue");
monstre.draw(ctx);
// 3 - je regarde l'état du clavier, de la souris
// de la manette de jeu, etc...
// 4 - je teste les collisions, etc.
if(((monstre.x + monstre.w) >= w) || (monstre.x <= 0)) {
monstre.v = -monstre.v;
}
// 5 - Je déplace les objets etc.
monstre.move();
// A LA FIN : je relance l'animation si possible
// dans 1/60ème de seconde (16.6ms)
requestAnimationFrame(anime);
}
function drawCercle(x, y, r, color) {
ctx.save();
ctx.translate(x, y);
ctx.beginPath();
ctx.arc(0, 0, r, 0, 2* Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.restore();
}
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. |