English 中文(简体)
为什么用 t子把图像引向 can?
原标题:Why doesn t javascript draw the images on the canvas?

I have a problem with drawing images to my canvas. There is no error but still there is no images drawn. Please review the chunk of code and see if you can see what I cannot:

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);
}

Thanks in advance! You can view the full source and so on here: http://www.picturds.com/pacman_serious/

最佳回答

Remove this from this.posX and this.posY in theload callsback. http://www.un.org/Depts/DGACM/index_chinese.htm (window)。

http://jsfiddle.net/5dkx4/

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签