<html lang="ru">
<head>
<meta charset="utf-8">
<title>html5 game</title>
<style>
body{
background: #2f2f2f;
}
canvas{
display: block;
position: absolute;
border: 1px solid #000;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
</body>
</html>
var canvas, ctx, keystate = {}, frames,
fps = 60, cur_fps = 0, dt, lastTime = Date.now(), frame_tick = 0,
_width, _height,
states = {Splash: 0, Game: 1, Score: 2}, //состояния игры
keyUP = 38, keyDOWN = 40, keyLEFT = 37, keyRIGHT = 39, keySPACE = 32,
player, _gravity = 0.5;
player = {
x: null,
y: null,
speed: 150,
frame: 0,
animation: [0,1],
width: 64,
height: 64,
update: function(){
/*if (keystate[keyLEFT]) this.x -=this.speed;
if (keystate[keyRIGHT]) this.x +=this.speed;*/
this.x +=this.speed * dt;
if (this.x >= canvas.width){
this.x = 0;
}
},
draw: function(ctx){
ctx.fillStyle = "#000";
ctx.fillRect(this.x, this.y, this.width, this.height);
}
};
function main(){
canvas = document.createElement("canvas");
canvas.width = 600;
canvas.height = 520;
ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
_width = canvas.width;
_height = canvas.height;
//Input
document.addEventListener("keydown", function(evt){
keystate[evt.keyCode] = true;
});
document.addEventListener("keyup", function(evt){
delete keystate[evt.keyCode];
});
run();
player.x = 0;
player.y = 128;
}
function run(){
update();
render();
}
function update(){
var d_now = Date.now();
dt = (d_now - lastTime)/1000;
player.update();
lastTime = d_now;
}
function render(){
ctx.fillStyle = "#00aeef";
ctx.fillRect(0,0,_width,_height);
player.draw(ctx);
}
main();
setInterval(run, 1000/60);
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. |