为什么不't javascript在画布上绘制图像
Why doesn't javascript draw the images on the canvas?
我在画布上绘制图像时遇到问题。没有错误,但仍然没有绘制任何图像。请查看代码块,看看你是否能看到我看不到的东西:
function loadPacman(posX, posY) {
this.posX = posX;
this.posY = posY;
pacman = new Image();
pacman.src="http://www.frogbug.se/pacman/img/naziman.png";
pacman.addEventListener("load", function(){
canvas.drawImage(pacman, this.posX, this.posY)
}, false);
}
function loadGhost(posX, posY) {
this.posX = posX;
this.posY = posY;
ghost = new Image();
ghost.src="http://www.frogbug.se/pacman/img/ghost.png";
ghost.addEventListener("load", function(){
canvas.drawImage(ghost, this.posX, this.posY)
}, false);
}
这是我在页面加载时加载的函数:
function onLoad() {
var xy = document.getElementById('canvas');
canvas = xy.getContext('2d');
//calculate the x and y for canvas
x = xy.width;
y = xy.height;
//divide the width and length of the canvas to get small cubes
//which is 40x40
xtile = Math.floor(x/40);
ytile = Math.floor(y/40);
//draw lines around the canvas
canvas.strokeRect(0, 0, x, y);
//the array for the map
var map = getMapArray();
//draw the map
drawMap(map);
//fillCanvas("0010", 0, 40, 40, 40);
//load the ghost and pacman
loadPacman(0, 0);
loadGhost((xtile*40)-40, (ytile*40)-40);
}
提前感谢!您可以在此处查看完整的来源等信息:http://www.picturds.com/pacman_serious/
从加载回调中的this.posX
和this.posY
中删除this
。该上下文中的this
是图像元素,与分配给this.posX
(即window
)时的this
不同。
http://jsfiddle.net/5dkx4/
相关文章:
- 画布:逐像素绘制图像并请求动画帧计时
- WebGL绘制图像
- 多重绘制图像后画布清除
- 可以'清除Rect后,不要在画布上绘制图像
- 如何在单页应用程序中重新绘制图像
- 画布中的图像在绘制图像后立即消失
- 使用Canvas绘制图像
- 如何在javascript中在图像上绘制图像(没有CSS和HTML)
- 使用模式在 HTML5/JS 中绘制图像
- 绘制并重新绘制图像以模拟动画是行不通的
- Dart Canvas未绘制图像
- 在新窗口中打开时,未在画布中绘制图像
- 在画布上绘制图像并导出打印质量的图像
- 用一个加载项绘制图像阵列
- html画布绘制图像不起作用
- Chrome 31.0 HTML5画布绘制图像
- 用HTML5和Javascript绘制图像
- 在新框架上绘制图像,同时仍然使用P5.js中的利萨茹曲线
- 如何在画布上更快地绘制图像
- 在 HTML5 画布中绘制图像,同时保留图像