var gameField = document.getElementById("gameField");
var graphics = gameField.getContext("2d");
var m = 1;
var s = 30;
var tm;
var c = 3;
var b = 0;
var img = new Image();
var shipH = 80;
var shipW = 180;
var shipX = (gameField.width - shipW)/2;
var shipY = gameField.height - shipH;
var shipDx = shipW / 20;
var shipDirection = 0;
var rowsCount = 1;
var columsCount = 5;
var enemyMargin = 30;
var offsetX = 100;
var offsetY = 50;
var img2 = new Image();
var enemyW =30;
var enemyH=30;
var enemy=[];
function timer() {
graphics.clearRect(0, 0, 60, 60);
graphics.fillStyle = "#f6ff00";
graphics.font = "20px Century Gothic";
if (c === 1) {
s = 59;
m = m - 1;
if (s >= 10) {
graphics.fillText(m + ":" + s, 20, 20);
}
if (s < 10) {
graphics.fillText(m + ":" + "0" + s, 20, 20);
}
b = 2;
}
if (c === 3) {
if ((s > 0) && (m >= 0)) {
s = s - 1;
if (s >= 10) {
graphics.fillText(m + ":" + s, 20, 20);
}
if (s < 10) {
graphics.fillText(m + ":" + "0" + s, 20, 20);
}
}
if ((s === 0) && (m > 0)) {
c = 1;
if (s >= 10) {
graphics.fillText(m + ":" + s, 20, 20);
}
if (s < 10) {
graphics.fillText(m + ":" + "0" + s, 20, 20);
}
}
if ((s === 0) && (m === 0)) {
clearTimeout(tm);
clearTimeout(tm2);
}
}
if (b === 2) {
c = 3;
}
}
tm = setInterval(timer, 1000);
function drawImg() {
graphics.save();
graphics.rect(shipX, shipY, shipW, shipH);
graphics.clip();
graphics.drawImage(img, shipX, shipY, shipW, shipH);
graphics.restore();
img.src = 'ship.jpg';
}
function keyDownHandler(key) {
if (key.keyCode===37) {shipDirection = -1;}
if (key.keyCode===39) {shipDirection = 1;}
}
function keyUpHandler(key) {
if (key.keyCode===37) {shipDirection = 0;}
if (key.keyCode===39) {shipDirection = 0;}
}
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function moveShip() {
if (shipDirection === -1) {
shipX -= shipDx;
if(shipX <= 0) {
shipX = 0;
}
}
if (shipDirection === 1) {
shipX += shipDx;
if (shipX >= gameField.width - shipW) {
shipX = gameField.width - shipW;
}
}
}
function createEnemy() {
var k=0;
for (var i=0; i < rowsCount; i++)
{
for(var j=0;j < columsCount; j++)
{
enemy[k]=
{
x:j*(enemyW + enemyMargin)+offsetX,
y:i*(enemy + enemyMargin)+offsetY
};
k++;
}
}
}
function drawEnemy() {
for (var i = 0; i < enemy.length; i++) {
graphics.save();
graphics.rect(enemy[i].x, enemy[i].y, enemyW, enemyH);
graphics.clip();
graphics.drawImage(img2, enemy[i].x, enemy[i].y, enemyW, enemyH);
graphics.restore();
img2.src = 'enemy.jpg';
}
}
createEnemy();
function updateGameField() {
graphics.clearRect(100,0,gameField.width-100, gameField.height);
graphics.clearRect(0, 100, 100, gameField.height-100);
moveShip();
drawImg();
drawEnemy();
}
tm2 = setInterval (updateGameField, 10);
вот изображения с масштабом 500% и 33%, все равно съехали