Canvas HTML 加载时间
Canvas HTML loading time
我尝试用 HTML Canvas 制作游戏,但我遇到了问题。
当我用 'window.onload' 调用我的 'init' 函数时,我的程序没有加载任何图像。 canvas 已创建,但图像不存在,当我在 chrome 控制台中提交我的函数时,出现图像...所以我想加载我的图像并在完成后执行该函数.
感谢您的帮助!
var requestAnimId;
// Initialisation
function init() {
var tilemapCtx = new Canvas('tilemap', 800, 600, 1);
var tilemap = new Tilemap("sand", tilemapCtx);
requestAnimId = window.requestAnimationFrame(update);
}
// Boucle de rafraîchissement
function update() {
requestAnimId = window.requestAnimationFrame(update);
}
window.onload = init;
这是瓷砖地图:
var Tilemap = function(map, canvas) {
for (var y = 0; y < 10; y++) {
for (var x = 0; x < 10; x++) {
// Création de la tile
this.image = new Image();
// Définition de l'image
switch(tilemaps[map][y][x]) {
case " ":
this.image.src = "assets/sand.png";
break;
case "#":
this.image.src = "assets/wall.png";
break;
}
// Affichage de l'image
//canvas.drawImage(this.image, tileX, tileY, tilesetWidth, tilesetHeight, x, y, 32, 32);
canvas.drawImage(this.image, 32, 32);
}
}
}
和Canvas:
var Canvas = function(name, width, height, zIndex) {
this.canvas = window.document.createElement("canvas");
this.canvas.id = name;
this.canvas.style.position = "absolute";
this.canvas.width = width;
this.canvas.height = height;
this.canvas.style.zIndex = zIndex;
document.body.appendChild(this.canvas);
return this.canvas.getContext('2d');
}
请注意,对于您的图块,您只使用了两张图片。您使用将加载 100 张图像 的双循环。
一个问题是您正在创建 100 张图像,但最终您 只需要两个 。
现在你的图像没有被绘制的原因是因为它们还没有 loaded
。在尝试绘制图像之前,您需要给图像加载时间。
您需要创建一个图像数组列表,并确保在调用 init()
之前加载它们
This 将确保在调用 init()
之前已加载所有图像。
var len = pics.length;
var loadCounter = 0;
for(var i = 0; i < len; i++) {
$(document.createElement(img)).attr('src', pics[i]).load(function() {
alert(pics[i] + 'is loaded');
loadCounter++;
if(loadCounter === len) {
alert("all are loaded");
}
});
}
我尝试用 HTML Canvas 制作游戏,但我遇到了问题。
当我用 'window.onload' 调用我的 'init' 函数时,我的程序没有加载任何图像。 canvas 已创建,但图像不存在,当我在 chrome 控制台中提交我的函数时,出现图像...所以我想加载我的图像并在完成后执行该函数.
感谢您的帮助!
var requestAnimId;
// Initialisation
function init() {
var tilemapCtx = new Canvas('tilemap', 800, 600, 1);
var tilemap = new Tilemap("sand", tilemapCtx);
requestAnimId = window.requestAnimationFrame(update);
}
// Boucle de rafraîchissement
function update() {
requestAnimId = window.requestAnimationFrame(update);
}
window.onload = init;
这是瓷砖地图:
var Tilemap = function(map, canvas) {
for (var y = 0; y < 10; y++) {
for (var x = 0; x < 10; x++) {
// Création de la tile
this.image = new Image();
// Définition de l'image
switch(tilemaps[map][y][x]) {
case " ":
this.image.src = "assets/sand.png";
break;
case "#":
this.image.src = "assets/wall.png";
break;
}
// Affichage de l'image
//canvas.drawImage(this.image, tileX, tileY, tilesetWidth, tilesetHeight, x, y, 32, 32);
canvas.drawImage(this.image, 32, 32);
}
}
}
和Canvas:
var Canvas = function(name, width, height, zIndex) {
this.canvas = window.document.createElement("canvas");
this.canvas.id = name;
this.canvas.style.position = "absolute";
this.canvas.width = width;
this.canvas.height = height;
this.canvas.style.zIndex = zIndex;
document.body.appendChild(this.canvas);
return this.canvas.getContext('2d');
}
请注意,对于您的图块,您只使用了两张图片。您使用将加载 100 张图像 的双循环。
一个问题是您正在创建 100 张图像,但最终您 只需要两个 。
现在你的图像没有被绘制的原因是因为它们还没有 loaded
。在尝试绘制图像之前,您需要给图像加载时间。
您需要创建一个图像数组列表,并确保在调用 init()
This 将确保在调用 init()
之前已加载所有图像。
var len = pics.length;
var loadCounter = 0;
for(var i = 0; i < len; i++) {
$(document.createElement(img)).attr('src', pics[i]).load(function() {
alert(pics[i] + 'is loaded');
loadCounter++;
if(loadCounter === len) {
alert("all are loaded");
}
});
}