所以我教你做一些随机的东西。然后我想画一个图像,所以我在网上做了记录,回来后带了一小段代码,由于某些原因,这些代码不起作用。
const cvs = document.getElementById("gameArea");
const ctx = cvs.getContext("2d");
let playArea = new Image();
playArea.src="./Textures/play_area.png";
function draw(){
ctx.drawImage(playArea,0,0);
}
draw();
图像位于与脚本相同的文件夹中的textures文件夹中。
发布于 2018-08-08 06:26:04
您应该在window.onload
上执行绘图,以防止它在内容加载之前运行。
下面是一个示例:
const cvs = document.getElementById("gameArea");
cvs.width = cvs.height = 800;
const ctx = cvs.getContext("2d");
let playArea = new Image();
playArea.src="https://upload.wikimedia.org/wikipedia/commons/e/e2/3D_Gyroscope.png";
function draw(){
ctx.drawImage(playArea,0,0);
}
window.onload = function() {
draw();
}
<canvas id=gameArea></canvas>
https://stackoverflow.com/questions/51731224
复制相似问题