context.translate(120, 120);
сделайте context.translate(120 + x_pos, 120);
, а context.fillRect(x_pos, 0, 100, 100);
замените на context.fillRect(0, 0, 100, 100);
. function calcRowAndColumn(coordinates) { let currPosition; if (coordinates > 0 && coordinates < 4) { currPosition = 0; } if (coordinates > 4 && coordinates < 8) { currPosition = 1; } if (coordinates > 8 && coordinates < 12) { currPosition = 2; } if (coordinates > 12 && coordinates < 16) { currPosition = 3; } if (coordinates > 16 && coordinates < 20) { currPosition = 4; } if (coordinates > 20 && coordinates < 24) { currPosition = 5; } if (coordinates > 24 && coordinates < 28) { currPosition = 6; } if (coordinates > 28 && coordinates < 32) { currPosition = 7; } if (coordinates > 32 && coordinates < 36) { currPosition = 8; } if (coordinates > 36 && coordinates < 40) { currPosition = 9; } if (coordinates > 40 && coordinates < 44) { currPosition = 10; } if (coordinates > 44 && coordinates < 48) { currPosition = 11; } if (coordinates > 48 && coordinates < 52) { currPosition = 12; } if (coordinates > 52 && coordinates < 56) { currPosition = 13; } if (coordinates > 56 && coordinates < 60) { currPosition = 14; } if (coordinates > 60 && coordinates < 64) { currPosition = 15; } if (coordinates > 64 && coordinates < 68) { currPosition = 16; } if (coordinates > 68 && coordinates < 72) { currPosition = 17; } if (coordinates > 72 && coordinates < 76) { currPosition = 18; } if (coordinates > 76 && coordinates < 80) { currPosition = 19; } if (coordinates > 80 && coordinates < 84) { currPosition = 20; } if (coordinates > 84 && coordinates < 88) { currPosition = 21; } if (coordinates > 88 && coordinates < 92) { currPosition = 22; } if (coordinates > 92 && coordinates < 96) { currPosition = 23; } if (coordinates > 96 && coordinates < 100) { currPosition = 24; } if (coordinates > 100 && coordinates < 104) { currPosition = 25; } if (coordinates > 104 && coordinates < 108) { currPosition = 26; } if (coordinates > 108 && coordinates < 112) { currPosition = 27; } if (coordinates > 112 && coordinates < 116) { currPosition = 28; } if (coordinates > 116 && coordinates < 120) { currPosition = 29; } if (coordinates > 120 && coordinates < 124) { currPosition = 30; } if (coordinates > 124 && coordinates < 128) { currPosition = 31; } return currPosition; }
const calcRowAndColumn = coord => Math.floor(coord / 4);
if (coordinates > 112 && coordinates < 116) { currPosition = 28; } if (coordinates > 116 && coordinates < 120) { currPosition = 29; }
const WIDTH = 300;
const HEIGHT = 300;
const canvas = document.getElementById('canvas');
canvas.width = WIDTH;
canvas.height = HEIGHT;
const ctx = canvas.getContext('2d');
const
X = 100,
Y = 100,
A = 90,
B = 50;
const
N = 1000,
SIDE = 4,
HALF_SIDE = SIDE / 2;
const rand = (min, max) => Math.random() * (max - min) + min;
ctx.beginPath();
ctx.ellipse(X, Y, B, A, Math.PI / 2, 0, 2 * Math.PI);
ctx.stroke();
for (let i = 0; i < N; i++) {
const
x = rand(-A + HALF_SIDE, A - HALF_SIDE),
maxAbsY = (B - HALF_SIDE) / A * Math.sqrt(A * A - x * x),
y = rand(-maxAbsY, maxAbsY);
ctx.beginPath();
ctx.rect(x + X - HALF_SIDE, y + Y - HALF_SIDE, SIDE, SIDE);
ctx.stroke();
}
const { position: { x, y }, cell_radius, food_size, food } = this;
const isIntersects = item =>
(item.x - x) ** 2 + (item.y - y) ** 2 <= (cell_radius + food_size) ** 2;
food.splice(0, food.length, ...food.filter(n => !isIntersects(n)));
// или
for (let i = food.length; i--;) {
if (isIntersects(food[i])) {
food.splice(i, 1);
}
}
// или
let countEaten = 0;
for (let i = 0; i < food.length; i++) {
food[i - countEaten] = food[i];
countEaten += isIntersects(food[i]);
}
food.length -= countEaten;
if(axis === "x") ctx.moveTo((position + x0), yMax); ctx.lineTo((position + x0), (y0 - dec)); ctx.stroke(); if(axis === "y") ctx.moveTo(x0, (yMax - position)); ctx.lineTo(xMax, (yMax - position)); ctx.stroke();
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');
const TILE_SIDE = 32;
let pickX = 0;
let pickY = 0;
const ground = new Image();
ground.src = 'Ground.png';
const pick = new Image();
pick.src = 'Pick.png';
document.addEventListener('keydown', function(e) {
switch (e.keyCode) {
case 87: pickY -= TILE_SIDE; break;
case 65: pickX -= TILE_SIDE; break;
case 83: pickY += TILE_SIDE; break;
case 68: pickX += TILE_SIDE; break;
default: return;
}
draw();
});
function draw() {
for (let x = 0; x < canvas.width; x += TILE_SIDE) {
for (let y = 0; y < canvas.height; y += TILE_SIDE) {
ctx.drawImage(ground, x, y);
}
}
ctx.drawImage(pick, pickX, pickY);
}
draw();
Что не так ?
$(canvas).clone()
.canvas = document.getElementById('myCanvas'); <...> drawImage(canvas[0],
canvas = $('#myCanvas')
, либо drawImage(canvas,
.