JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://strd6.com/space_demo/javascripts/jquery.hotkeys.js"></script>
<script src="http://strd6.com/space_demo/javascripts/key_status.js"></script>
<canvas id="Canvas"></canvas>
CSS
#Canvas
{
width:400px;
height:400px;
border:1px solid black;
}
JavaScript
$(document).ready(function () {
var cw = 400;
var ch = 400;
var Canvas = $("#Canvas")[0];
Canvas.width = cw;
Canvas.height = ch;
var ctx = Canvas.getContext("2d");
var gravity = 0.5;
var fps = 60;
var KenImage = new Image();
var FieldImage = new Image();
KenImage.src = "http://front-back.com/wp-content/uploads/2013/01/ken.png";
FieldImage.src = "http://front-back.com/wp-content/uploads/2013/01/sf2-bg.jpg";
var ken = {
w: 70,
h: 80,
x: 0,
y: ch - 80,
Row: 1,
Pos: 0,
Speed: 1,
IsWaiting: false,
IsMoving: false,
lastRenderTime: 0,
RenderRate: 200,
draw: function () {
ctx.drawImage(KenImage, this.Pos * this.w, this.Row * this.h, this.w, this.h, this.x, this.y, this.w, this.h);
},
update: function () {
if (keydown.right) {
this.x += 1;
this.IsMoving = true;
if (this.x > (cw - this.h)) {
this.x = cw - this.h;
}
}
if (!this.IsWaiting) {
this.IsWaiting = true;
this.lastRenderTime = now;
this.Pos = 1 + (this.Pos + 1) % 3;
}
else {
if (now - this.lastRenderTime >= this.RenderRate) this.IsWaiting = false;
}
}
};
function Draw() {
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(FieldImage, 0, 0, cw, ch);
ken.draw();
}
...