javascript之canvas 二进制数据图片显示 #12

github:https://github.com/abaiweb/blog/issues/12

需求通过socket发送图片二进制数据,需要显示在canvas上,代码如下:

var canvas;  
var context;  
var image; 
var url; 
window.onload = init;  
  
function init() {  
	canvas=document.getElementById('receiver');
	context = canvas.getContext("2d");
	image = new Image();
}

//释放
if(url != undefined)
    URL.revokeObjectURL(url);
image.onload = function() {
    context.drawImage(image, 0, 0);
}
url = URL.createObjectURL( "图片二进制数据" ); 
image.src = url;