39 lines
1.1 KiB
HTML
39 lines
1.1 KiB
HTML
<html>
|
|
|
|
<head>
|
|
<script>
|
|
function attachEvents() {
|
|
document.onkeypress = function (event) {
|
|
var xoffset = 10 * parseInt(String.fromCharCode(event.keyCode || event.charCode));
|
|
draw(xoffset);
|
|
}
|
|
}
|
|
|
|
function draw(xoffset) {
|
|
var canvas = document.getElementById("canvas");
|
|
var context = canvas.getContext('2d');
|
|
|
|
// remove previous translation if any
|
|
context.save();
|
|
|
|
// over-write previous content, with a white rectangle
|
|
context.fillStyle = "#FFFFFF";
|
|
context.fillRect(0, 0, 300, 300);
|
|
|
|
// translate based on numerical keypress
|
|
context.translate(xoffset, 0);
|
|
|
|
// purple rectangle
|
|
context.fillStyle = "#CC00FF";
|
|
context.fillRect(0, 0, 50, 50);
|
|
context.restore();
|
|
}
|
|
</script>
|
|
</head>
|
|
|
|
<body onload="attachEvents();">
|
|
<canvas id="canvas" width="300" height="300"></canvas>
|
|
</body>
|
|
|
|
</html>
|