2014-10-02 114 views
2

我有一个SVG数据uri成功呈现到页面中的img元素。在Chrome中,使用drawImage将图像绘制到画布上效果很好,但在FireFox中,我获得了NS_ERROR_NOT_AVAILABLE。Firefox无法使用数据uri在画布上绘制图像:NS_ERROR_NOT_AVAILABLE

这是一个可在Chrome浏览器中使用但不是Firefox的小提琴。点击按钮触发复制。

http://jsfiddle.net/YbA39/181/

是否有任何变通办法或其他方式来说服火狐得出这样的形象呢?

回答

0

,我想这里的问题,是当资源可用?,但有一种方法,以确认该资源可用,只是检查图像对象的“完整的”属性。

if (img.complete == true){ 
    // is available. 
} else { 
    // wait until is ready. 
} 

此外,您可以使用onload事件和检查两个东西的延迟方法进行合并方法。

var img = new Image(); 
//first attach handler 
img.onload = function(e){ 
    if (e.target.complete == true) { 
      yourHandler(e); 
     } else {    
      var maxTimes = 10; 
      var countTimes = 0; 
      function retryLoadImage() { 
       setTimeout(function() { 
        if (countTimes > maxTimes) { 
         // -- cannot load image. 
         return; 
        } else { 
         if (e.target.complete == true) { 
          yourHandler(e); 
         } else { 
          retryLoadImage(); 
         } 
        } 
        countTimes++; 
       }, 50); 
      }; 
     } 
}; 
img.src = yourSource; 

这对我有用!!!在IE和FF上。