首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drawImage()不一致地工作

问drawImage()不一致地工作
EN

Stack Overflow用户
提问于 2014-03-31 09:27:15
回答 1查看 83关注 0票数 1

当您将名为logo.png的图片放在与此html文件相同的目录中并尝试在web浏览器中运行时,IE中的10次刷新中只会出现1次,而在Firefox中不会第一次出现,但在进一步刷新后确实会出现。

到底是怎么回事?

(drawImage()方法在showIntro()函数中调用)

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Example 1 - Title Screen</title>
    <script>
        window.onload = function () {
            var canvas = document.getElementById('myCanvas');
            var c = canvas.getContext('2d');
            var State = {
                _current: 0,
                INTRO: 0,
                LOADING: 1,
                LOADED: 2
            }
            window.addEventListener('click', handleClick, false);
            window.addEventListener('resize', doResize, false);
            doResize();
            function handleClick() {
                State._current = State.LOADING;
                fadeToWhite();
            }
            function doResize() {
                canvas.width = document.body.clientWidth;
                canvas.height = document.body.clientHeight;
                switch (State._current) {
                    case State.INTRO:
                        showIntro();
                        break;
                }
            }
            function fadeToWhite(alphaVal) {
                // If the function hasn't received any parameters, start with 0.02
                var alphaVal = (alphaVal == undefined) ? 0.02 : parseFloat(alphaVal) + 0.02;
                // Set the color to white
                c.fillStyle = '#FFFFFF';
                // Set the Global Alpha
                c.globalAlpha = alphaVal;
                // Make a rectangle as big as the canvas
                c.fillRect(0, 0, canvas.width, canvas.height);
                if (alphaVal < 1.0) {
                    setTimeout(function () {
                        fadeToWhite(alphaVal);
                    }, 30);
                }
            }

            function showIntro() {
                var phrase = "Click or tap the screen to start the game";
                // Clear the canvas
                c.clearRect(0, 0, canvas.width, canvas.height);
                // Make a nice blue gradient
                var grd = c.createLinearGradient(0, canvas.height, canvas.width, 0);
                grd.addColorStop(0, '#ceefff');
                grd.addColorStop(1, '#52bcff');
                c.fillStyle = grd;
                c.fillRect(0, 0, canvas.width, canvas.height);
                var logoImg = new Image();
                logoImg.src = './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);
            }
        }
    </script>
    <style type="text/css" media="screen">
      html { height: 100%; overflow: hidden }
      body {
        margin: 0px;
        padding: 0px;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="100" height="100">
      Your browser doesn't include support for the canvas tag.
    </canvas>
  </body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-31 09:32:13

问题是,在调用drawImage()时,您没有等待加载映像。

你可以使用这样的东西:

代码语言:javascript
复制
logo.img.onload = function(){
    c.drawImage(logo.img, logo.x, logo.y, logo.img.width, logo.img.height);
};

不过,请确保在这种情况发生之前,不要开始修改画布。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22758019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档