提问者:小点点

如何在画布方块中放置图像?


null

null

null

function SnakeTexture(context, x, y) {
  const Texture = new Image();
  Texture.src = '../../assets/images/snake.png';
  Texture.onload = () => {
    context.drawImage(Texture, x, x, 16, 16);
  }
}

export { SnakeTexture };

null

/* GAME CONTEXTS */
const context = canvasRef.current.getContext("2d");
context.setTransform(16, 0, 0, 16, 0, 0);
context.clearRect(0, 0, 320, 320);

/* SNAKE STYLE */
SnakeTexture(context, snake[0], snake[1])
    null

共1个答案

匿名用户

null

loadImage(SnakeTexture).then(img => {
   snake.forEach(([x, y]) => context.drawImage(img, x, y, 1, 1));
})