canvas2image.js
canvas2
var Canvas2Image = (function() {
var bHasCanvas = false;
var oCanvas = document.createElement("canvas");
if (oCanvas.getContext("2d")) {
bHasCanvas = true;
}
if (!bHasCanvas) {
return {
saveAsBMP: function() {},
saveAsPNG: function() {},
saveAsJPEG: function() {}
}
}
var bHasImageData = !!(oCanvas.getContext("2d").getImageData);
var bHasDataURL = !!(oCanvas.toDataURL);
var bHasBase64 = !!(window.btoa);
var strDownloadMime = "image/octet-stream";
var readCanvasData = function(oCanvas) {
var iWidth = parseInt(oCanvas.width);
var iHeight = parseInt(oCanvas.height);
return oCanvas.getContext("2d").getImageData(0, 0, iWidth, iHeight);
}
var encodeData = function(data) {
var strData = "";
if (typeof data == "string") {
strData = data;
} else {
var aData = data;
for (var i = 0; i < aData.length; i++) {
strData += String.fromCharCode(aData[i]);
}
}
return btoa(strData);
}
var createBMP = function(oData) {
var aHeader = [];
var iWidth = oData.width;
var iHeight = oData.height;
aHeader.push(0x42);
aHeader.push(0x4D);
var iFileSize = iWidth * iHeight * 3 + 54;
aHeader.push(iFileSize % 256);
iFileSize = Math.floor(iFileSize / 256);
aHeader.push(iFileSize % 256);
iFileSize = Math.floor(iFileSize / 256);
aHeader.push(iFileSize % 256);
iFileSize = Math.floor(iFileSize / 256);
aHeader.push(iFileSize % 256);
aHeader.push(0);
aHeader.push(0);
aHeader.push(0);
aHeader.push(0);
aHeader.push(54);
aHeader.push(0);
aHeader.push(0);
aHeader.push(0);
var aInfoHeader = [];
aInfoHeader.push(40);
aInfoHeader.push(0);
aInfoHeader.push(0);
aInfoHeader.push(0);
var iImageWidth = iWidth;
aInfoHeader.push(iImageWidth % 256);
iImageWidth = Math.floor(iImageWidth / 256);
aInfoHeader.push(iImageWidth % 256);
iImageWidth = Math.floor(iImageWidth / 256);
aInfoHeader.push(iImageWidth % 256);
iImageWidth = Math.floor(iImageWidth / 256);
aInfoHeader.push(iImageWidth % 256);
var iImageHeight = iHeight;
aInfoHeader.push(iImageHeight % 256);
iImageHeight = Math.floor(iImageHeight / 256);
aInfoHeader.push(iImageHeight % 256);
iImageHeight = Math.floor(iImageHeight / 256);
aInfoHeader.push(iImageHeight % 256);
iImageHeight = Math.floor(iImageHeight / 256);
aInfoHeader.push(iImageHeight % 256);
aInfoHeader.push(1);
aInfoHeader.push(0);
aInfoHeader.push(24);
aInfoHeader.push(0);
aInfoHeader.push(0);
aInfoHeader.push(0);
aInfoHeader.push(0);
aInfoHeader.push(0);
var iDataSize = iWidth * iHeight * 3;
aInfoHeader.push(iDataSize % 256);
iDataSize = Math.floor(iDataSize / 256);
aInfoHeader.push(iDataSize % 256);
iDataSize = Math.floor(iDataSize / 256);
aInfoHeader.push(iDataSize % 256);
iDataSize = Math.floor(iDataSize / 256);
aInfoHeader.push(iDataSize % 256);
for (var i = 0; i < 16; i++) {
aInfoHeader.push(0);
}
var iPadding = (4 - ((iWidth * 3) % 4)) % 4;
var aImgData = oData.data;
var strPixelData = "";
var y = iHeight;
do {
var iOffsetY = iWidth * (y - 1) * 4;
var strPixelRow = "";
for (var x = 0; x < iWidth; x++) {
var iOffsetX = 4 * x;
strPixelRow += String.fromCharCode(aImgData[iOffsetY + iOffsetX + 2]);
strPixelRow += String.fromCharCode(aImgData[iOffsetY + iOffsetX + 1]);
strPixelRow += String.fromCharCode(aImgData[iOffsetY + iOffsetX]);
}
for (var c = 0; c < iPadding; c++) {
strPixelRow += String.fromCharCode(0);
}
strPixelData += strPixelRow;
} while (--y);
var strEncoded = encodeData(aHeader.concat(aInfoHeader)) + encodeData(strPixelData);
return strEncoded;
}
var saveFile = function(strData) {
document.location.href = strData;
}
var makeDataURI = function(strData, strMime) {
return "data:" + strMime + ";base64," + strData;
}
var makeImageObject = function(strSource) {
var oImgElement = document.createElement("img");
oImgElement.src = strSource;
return oImgElement;
}
var scaleCanvas = function(oCanvas, iWidth, iHeight) {
if (iWidth && iHeight) {
var oSaveCanvas = document.createElement("canvas");
oSaveCanvas.width = iWidth;
oSaveCanvas.height = iHeight;
oSaveCanvas.style.width = iWidth + "px";
oSaveCanvas.style.height = iHeight + "px";
var oSaveCtx = oSaveCanvas.getContext("2d");
oSaveCtx.drawImage(oCanvas, 0, 0, oCanvas.width, oCanvas.height, 0, 0, iWidth, iHeight);
return oSaveCanvas;
}
return oCanvas;
}
return {
saveAsPNG: function(oCanvas, bReturnImg, iWidth, iHeight) {
if (!bHasDataURL) {
return false;
}
var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight);
var strData = oScaledCanvas.toDataURL("image/png");
if (bReturnImg) {
return makeImageObject(strData);
} else {
saveFile(strData.replace("image/png", strDownloadMime));
}
return true;
},
saveAsJPEG: function(oCanvas, bReturnImg, iWidth, iHeight) {
if (!bHasDataURL) {
return false;
}
var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight);
var strMime = "image/jpeg";
var strData = oScaledCanvas.toDataURL(strMime);
if (strData.indexOf(strMime) != 5) {
return false;
}
if (bReturnImg) {
return makeImageObject(strData);
} else {
saveFile(strData.replace(strMime, strDownloadMime));
}
return true;
},
saveAsBMP: function(oCanvas, bReturnImg, iWidth, iHeight) {
if (!(bHasImageData && bHasBase64)) {
return false;
}
var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight);
var oData = readCanvasData(oScaledCanvas);
var strImgData = createBMP(oData);
if (bReturnImg) {
return makeImageObject(makeDataURI(strImgData, "image/bmp"));
} else {
saveFile(makeDataURI(strImgData, strDownloadMime));
}
return true;
}
};
})();
月下夜想曲200.6(攻略1)
[url=http://xieye.iteye.com/blog/2425831]月下夜想曲 200.6(攻略1)[/url][url=http://xieye.iteye.com/blog/2425941]月下夜想曲
苏宁易购2019年年报:全年营收2692.29亿元,净利润98.43亿
站长之家(ChinaZ.com) 4月18日 消息:17 日晚间,苏宁易购发布了 2019 年年度报告。报告显示, 2019 年,苏宁易购全年营收2692. 29 亿元
excel2010试用版过期的解决方法
软件过期了如果还想继续使用的话就必须得激活,不然会老是弹出一些好烦人的窗口!我的Office 2010 版的的办公软件过期了,每打开一个
visio2007产品密钥,小编告诉你如何激活电脑visio2007
什么是visio 2007?visio 2007有什么作用?Office Visio 2007 非常适用于IT 和商务专业人员,它可以帮助用户就复杂信息、系统和流程进
荣耀MagicWatch 2值得买吗 荣耀MagicWatch 2智能手表
荣耀MagicWatch 2怎么样?好不好用?值得入手吗?下面小编带来荣耀MagicWatch 2智能手表评测,希望对大家有所帮助。荣耀MagicWatch 2智能
文章回顾
大家看了本文canvas2image.js的精彩教程资源内容,是不是对canvas2image.js了解更多,真心希望canvas2image.js能帮助到你, 小编会一直给你带来更多教程资源文章信息。
本文由第三方用户分享仅代表作者观点,不代表本网站立场,秉承互联网开放分享的精神,目的在于传递更多信息,加强各行业互通交流,但对内容不作任何保证或承诺,请读者自行参考斟酌。网站发布的信息(包含但不限于版式、图片、字体、文章等素材)由第三方用户分享,版权归原作者所有,本站不承担任何相关的版权纠纷等相关责任。如您认为本篇内容侵犯了您的权益,请与我们联系,我们会及时处理。
百科塔让百科知识为更多人带来价值。
转载请注明: 本文标题:canvas2image.js 本文地址:https://www.bktl.cn/12835.html