1

Based on this javascript tutorial I wrote a simple animation as a test. It worked without any problems with Edge/IE 11, then I tried running it on Chrome and the canvas and the script will load and apparently work, but nothing is being shown on screen. No errors are being shown in the debugger either. I tried reinstalling Chrome but the problem persists. Can anybody explain me what's going on? Thanks in advance.

Here is a jsFiddle with the example, which doesn't load on chrome (at least for me)

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


    }

Update: Logging to Dropbox seems to fix the problem with the jsFiddle. This wasn't my actual question though, since my own version of it worked with a local file. I'll mark this as solved anyways.

  • FYI, I can't get to your image `http://dl.dropbox.com/u/1143870/sprite%20sheet/robot2.png` in either Chrome or Firefox. I can get there in IE. – jfriend00 Oct 27 '15 at 00:49
  • I could hit the image directly in IE11, but not any other browser. I'm not logged into dropbox AFAIK. – chiliNUT Oct 27 '15 at 00:51
  • FYI, if I use `https` as the protocol, I can load the image in both Firefox and Chrome. – jfriend00 Oct 27 '15 at 00:54
  • its obnoxious that it works in IE at all, seems like the expected behavior of trying to hit an https resource over http would be that it just doesn't load at all, unless the site is doing something to redirect http to https, in which case it should be working in all browsers – chiliNUT Oct 27 '15 at 00:56
  • @chiliNUT - I can't yet tell if this is an IE problem or potentially a DropBox problem or even possibly a Chrome/Firefox problem. We like to assume IE is at fault (it certainly deserves that assumption), but I don't have any evidence either way yet. – jfriend00 Oct 27 '15 at 01:06
  • @jfriend00 word. I would love to hear the final answer on that one – chiliNUT Oct 27 '15 at 01:09
  • I can't even load the `http` version of the image in IE any more so I can't really test the differences any further. – jfriend00 Oct 27 '15 at 01:11
  • weird. it redirects to https automatically for me in IE11 – chiliNUT Oct 27 '15 at 01:35

1 Answers1

1

If you change the protocol on your image URL to https, your jsFiddle works in Chrome and Firefox. Modifed and working jsFiddle here.

Likewise, I can only load the bare image into Chrome and Firefox when the URL starts with https.


I see that Chrome is including a header with the original request when using http:

Upgrade-Insecure-Requests:1

You can read about that header here. I wonder if Dropbox is not handling that header appropriately.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • After entering to my Dropbox account the animation started working on chrome. I thought somehow it would only work with `http` if you were logged in, but after logging out, the animation kept loading. For curiosity, I cleaned the browser data on chrome and the animation did not load after refreshing. So the https trick seems to solve it, yet it will work fine if you are logged into your Dropbox account. Strange. – Mauricio Gómez Oct 27 '15 at 03:00
  • @MauricioGómez - it probably works once it is in your browser cache. – jfriend00 Oct 27 '15 at 03:23