简体   繁体   English

将图像加载到 HTML5 Canvas

[英]Loading image onto HTML5 Canvas

I am following an antiquated tutorial for building an game using HTML5 and JavaScript.我正在关注使用 HTML5 和 JavaScript 构建游戏的过时教程。 I have worked with JavaScript in the past, but canvas is new to me.我过去曾与 JavaScript 合作过,但 canvas 对我来说是新的。 The code that I'm trying to run seems fairly straightforward, but it seems that HTML5 has changed since this tutorial was published, and I'm having trouble getting images to load.我尝试运行的代码似乎相当简单,但似乎 HTML5 自本教程发布以来发生了变化,我无法加载图像。

I have done some research and was able to get an image to load correctly by first ensuring it was loaded before drawing it to the canvas.我已经进行了一些研究,并且能够通过在将其绘制到 canvas之前首先确保它已加载来正确加载图像。 That was pretty straightforward, but now I'm not quite sure how to adapt the more in-depth code to work for the situation.这很简单,但现在我不太确定如何调整更深入的代码来适应这种情况。

Given the following code, how and where would I need to ensure that the image is loaded first before drawing it.给定以下代码,我需要如何以及在哪里确保在绘制图像之前先加载图像。 The main problems I'm facing are both where to put the code to wait for the load and how to ensure that the logo utility object is usable in the code below.我面临的主要问题是在哪里放置代码以等待加载以及如何确保徽标实用程序 object 在下面的代码中可用。

var phrase = "Click or tap the screen to start the game";

    // Clear the canvas
    c.clearRect (0, 0, canvas.width, canvas.height);

    var logoImg = new Image();
    logoImg.src = '../img/logo.png';

    // Store the original width value so that we can keep the same width/height ratio later
    var originalWidth = logoImg.width;

    // Compute the new width and height values
    logoImg.width = Math.round((50 * document.body.clientWidth) / 100);
    logoImg.height = Math.round((logoImg.width * logoImg.height) / originalWidth);

    // Create an small utility object
    var logo = {
        img: logoImg,
        x: (canvas.width/2) - (logoImg.width/2),
        y: (canvas.height/2) - (logoImg.height/2)
    }

    // Present the image
    c.drawImage(logo.img, logo.x, logo.y, logo.img.width, logo.img.height);

    // Change the color to black
    c.fillStyle = '#000000';
    c.font = 'bold 16px Arial, sans-serif';

    var textSize = c.measureText (phrase);
    var xCoord = (canvas.width / 2) - (textSize.width / 2);

    c.fillText (phrase, xCoord, (logo.y + logo.img.height) + 50);

Figured it out, thanks to YosefTukachinsky's cryptic advice.多亏了 YosefTukachinsky 的神秘建议,才弄明白。

var logoImg = new Image();
logoImg.src = '../img/logo.png';

logoImg.onload = function(){ 
     // Store the original width value so that we can keep the same width/height ratio later
     var originalWidth = logoImg.width;

     // Compute the new width and height values
     logoImg.width = Math.round((50 * document.body.clientWidth) / 100);
     logoImg.height = Math.round((logoImg.width * logoImg.height) / originalWidth);

     // Create an small utility object
     var logo = {
          img: logoImg,
          x: (canvas.width/2) - (logoImg.width/2),
          y: (canvas.height/2) - (logoImg.height/2)
     }

     // Present the image
     c.drawImage(logo.img, logo.x, logo.y, logo.img.width, logo.img.height);

     // Change the color to black
     c.fillStyle = '#000000';
     c.font = 'bold 16px Arial, sans-serif';

     var textSize = c.measureText (phrase);
     var xCoord = (canvas.width / 2) - (textSize.width / 2);

     c.fillText (phrase, xCoord, (logo.y + logo.img.height) + 50);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM