【问题标题】:Why is this animation working in IE 11 and Edge but not in chrome?为什么此动画在 IE 11 和 Edge 中有效,但在 chrome 中无效?
【发布时间】:2016-01-26 06:43:00
【问题描述】:

基于this javascript tutorial我写了一个简单的动画作为测试。它在 Edge/IE 11 上没有任何问题,然后我尝试在 Chrome 和画布上运行它,脚本将加载并且显然可以工作,但屏幕上没有显示任何内容。调试器中也没有显示错误。我尝试重新安装 Chrome,但问题仍然存在。 任何人都可以向我解释发生了什么事吗?提前致谢。

这是一个带有示例的jsFiddle,它不会在 chrome 上加载(至少对我而言)

    // screen size variables
    var SCREEN_WIDTH = window.innerWidth,
    SCREEN_HEIGHT = window.innerHeight;        

      var canvas = document.createElement('canvas');
      var c = canvas.getContext('2d');

    canvas.width = SCREEN_WIDTH;
    canvas.height = SCREEN_HEIGHT;

    var xpos=0, 
        ypos=0, 
        index=0, 
        numFrames = 30, 
        frameSize= 200;

      // Add our drawing canvas
      document.body.appendChild(canvas); 

       //load the image
    image = new Image();
    image.src = "http://dl.dropbox.com/u/1143870/sprite%20sheet/robot2.png";

    image.onload = function() {

        //we're ready for the loop
        setInterval(loop, 1000 / 30);
    }


    function loop() {
        //clear the canvas!
        c.clearRect(0,0, SCREEN_HEIGHT,SCREEN_WIDTH);

        /*our big long list of arguments below equates to: 
            1: our image source
            2 - 5: the rectangle in the source image of what we want to draw
            6 - 9: the  rectangle of our canvas that we are drawing into

            the area of the source image we are drawing from will change each time loop() is called.
            the rectangle of our canvas that we are drawing into however, will not. 
            tricky!
        */
        c.drawImage(image,xpos,ypos,frameSize,frameSize,0,0,frameSize, frameSize);

        //each time around we add the frame size to our xpos, moving along the source image
        xpos += frameSize;
        //increase the index so we know which frame of our animation we are currently on
        index += 1;

        //if our index is higher than our total number of frames, we're at the end and better start over
        if (index >= numFrames) {
            xpos =0;
            ypos =0;
            index=0;    
        //if we've gotten to the limit of our source image's width, we need to move down one row of frames                
        } else if (xpos + frameSize > image.width){
            xpos =0;
            ypos += frameSize;
        }


    }

更新:登录到 Dropbox 似乎解决了 jsFiddle 的问题。不过,这不是我的实际问题,因为我自己的版本适用于本地文件。无论如何我都会将此标记为已解决。

【问题讨论】:

  • 仅供参考,我无法在 Chrome 或 Firefox 中访问您的图像 http://dl.dropbox.com/u/1143870/sprite%20sheet/robot2.png。我可以在 IE 中到达那里。
  • 我可以直接在 IE11 中点击图片,但不能在任何其他浏览器中点击。我没有登录Dropbox AFAIK。
  • 仅供参考,如果我使用https 作为协议,我可以在 Firefox 和 Chrome 中加载图像。
  • 它在 IE 中工作令人讨厌,似乎试图通过 http 访问 https 资源的预期行为是它根本不加载,除非网站正在做某事将http重定向到https,在这种情况下它应该在所有浏览器中都可以工作
  • @chiliNUT - 我还不能确定这是 IE 问题还是潜在的 DropBox 问题,甚至可能是 Chrome/Firefox 问题。我们喜欢假设 IE 有问题(它当然值得这样假设),但我还没有任何证据。

标签: javascript google-chrome animation canvas


【解决方案1】:

如果您将图像 URL 上的协议更改为 https,您的 jsFiddle 可以在 Chrome 和 Firefox 中运行。修改和工作的 jsFiddle here.

同样,当 URL 以 https 开头时,我只能将裸图像加载到 Chrome 和 Firefox 中。


我看到 Chrome 在使用 http 时包含原始请求的标头:

Upgrade-Insecure-Requests:1

您可以阅读该标题here。我想知道 Dropbox 是否没有正确处理该标头。

【讨论】:

  • 进入我的 Dropbox 帐户后,动画开始在 chrome 上运行。我想不知何故,如果您已登录,它只能与 http 一起使用,但在注销后,动画继续加载。出于好奇,我清理了chrome上的浏览器数据,刷新后动画没有加载。所以 https 技巧似乎可以解决它,但如果您登录到您的 Dropbox 帐户,它会正常工作。奇怪。
  • @MauricioGómez - 一旦它在您的浏览器缓存中,它可能会起作用。
猜你喜欢
  • 2011-03-12
  • 2011-07-19
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多