简体   繁体   中英

IE9 security error when reading from canvas (non cross-domain)

I'm playing a video in a video tag. The video file is in the same directory as the index.html. I am then putting the video pixels on a canvas , doing some logic on them, reading them and putting on another canvas . All this works fine in firefox and chrome, but not in IE9. IE gives a security error when I'm trying to read the pixels from canvas. Which would be understandable if the video originated from some other domain, but it doesn't. What is even more curious, the error occurs when I put the relevant code in setTimeout or trigger it from the console, but not when it is called directly in the script. Here's the relevant javascript:

$(document).ready(function(){
fun = function(){
    var main= $("#main");
    var video = $('<video autoplay="autoplay">
                <source src="orange.mp4" type="video/mp4" />
                <source src="orange.ogv" type="video/ogg" /></video>');
    video.css({"width" : 100, "height" : 200});
    var canvas1 = $('<canvas></canvas>');
    canvas1.css({"position" : "relative", "top" :0, 
                "width" : 100, "height" : 200, "background-color":"red"});
    canvas1.attr({"width" : 100, "height" : 200});
    var context1=canvas1[0].getContext("2d");

    var canvas2 = $('<canvas></canvas>');
    canvas2.css({"position" : "relative", "top" :0, 
                 "width" : 100, "height" : 200, "background-color":"purple", 
                 "margin" : "5px"});
    canvas2.attr({"width" : 100, "height" : 200});
    var context2=canvas2[0].getContext("2d");

    main.append(video);
    main.append(canvas1);
    main.append(canvas2);


    var drawFrame = function(){
            context1.drawImage(video[0],0,0,100,200);
            var data = context1.getImageData(0,0,100,200);
            context2.putImageData(data, 0, 0);
            setTimeout(drawFrame, 50);
    }

    drawFrame();
}
fun();                  // <--- this one works
var wurst = setTimeout(fun,50);     // <--- this one doesn't
});

What is happening here, and what can be done to get around it?

Hmm.. that seems weird! Have you tried using requestAnimationFrame instead of setTimeout?

// Polyfill for HTML5's requestAnimationFrame API.
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
          window.webkitRequestAnimationFrame || 
          window.mozRequestAnimationFrame    || 
          window.oRequestAnimationFrame      || 
          window.msRequestAnimationFrame     || 
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

requestAnimFrame(fun);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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