if(require("http")) {
var http = require("http");
http.get(url,function (res) {
res.setEncoding('binary');//二进制(binary)
var re ='';
res.on('data',function (data) {
re += data;
// collect the data chunks to the variable named "html"
}).on('end', function () {
//console.log(re);
var key = getKeyByURL(url);
var b = new Buffer(re);
var imageData =toArrayBuffer(b)
getCanvasData(key, imageData, order);
})
}).on('error', function (e) {
console.log("Got error: " + e.message);
});
}
};
function getCanvasData (key, buffer, order) {
createCanvas();
var _blob = new Blob(new Array(buffer), { type: "image/jpeg" });
var _url = URL.createObjectURL(_blob);
var _img = new Image();
var imgData = undefined;
_img.onload = function () {
ctx.clearRect(0, 0, 256, 256);
ctx.drawImage(this, 0, 0);
URL.revokeObjectURL(_url);
imgData = ctx.getImageData(0, 0, 256, 256);
cache[order](key, imgData);
exeCheckCallback(key, imgData);
};
_img.onerror = function () {
exeCheckCallback(key, imgData);
};
_img.src = _url;
};
function createCanvas() {
if (!canvas) {
canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
ctx = canvas.getContext('2d');
}
}