2017-04-10 67 views
0

我想使用toDataURL()獲取圖像數據,但是我的Imagedraw()方法不起作用。canvas imageDraw()不工作

var video = document.getElementById('media-video'); 
      var videoCurrentTime = document.getElementById('media-video').currentTime; 
      var canvasWidth = $('video').width(); 
      var canvasHeight = $('video').height(); 
      var c = $('body').append($('<canvas>',{id: 'myCanvas', width:canvasWidth, height:canvasHeight})); 

    var ctx; 

    $(document).ready(function() { 
     c = document.getElementById('myCanvas');       
     ctx = c.getContext("2d");    
    }); 
    c.onload=function(){ 
     ctx.drawImage(video, 0, 0,canvasWidth , canvasHeight); 
    } 
    imageData = c.toDataURL("image/jpeg",1.0); 

    window.open(imageData, "toDataURL() image", "width=600, height=200"); 
} 

我可以看到imageData中返回的空白畫布。

+0

我已經試過了,提供靜態值 – arun

+0

上面的代碼在iframe中。它與它有什麼關係? – arun

回答

0

Becouse c.onload=是acyns。 onload之後,您最多可以訪問DataURL。

c.onload=function(){ 
    ctx.drawImage(video, 0, 0,canvasWidth , canvasHeight); 
    imageData = this.toDataURL("image/jpeg",1.0); 
    window.open(imageData, "toDataURL() image", "width=600, height=200"); 
} 
+0

沒有工作。 $(document).ready(function(){ ctx.drawImage(video,0,0,canvasWidth,canvasHeight); }); imageData = c.toDataURL(「image/jpeg」,1.0);現在的問題是要DataURL()代碼 – arun

+0

c.onload你有toDataURL()? –