>使用之前要引入 html2Canvas.js
>https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js
~~~
function saveDomImage(html2Canvas, dom, fileName) {
if(mui.os.plus) {
if(typeof html2Canvas == null)
throw Error("html2Canvas is not defined");
if(dom == null)
throw Error("saveDomImage param : dom is null");
if(fileName == null || fileName == "")
fileName = "untitled.png";
var getPixelRatio = function(context) {
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio || 1;
return(window.devicePixelRatio || 1) / backingStore;
};
var _canvas = document.createElement('canvas');
var ctx = _canvas.getContext('2d');
var ratio = getPixelRatio(ctx);
ctx.scale(ratio, ratio);
var w = dom.offsetWidth;
var h = dom.offsetHeight;
_canvas.width = w;
_canvas.height = h;
_canvas.style.width = w * ratio + 'px';
_canvas.style.height = h * ratio + "px";
html2Canvas(dom, {
allowTaint: true,
logging: false,
profile: true,
useCROS: true,
canvas: _canvas,
onrendered: function(canvas) {
var dataUrl = canvas.toDataURL();
var b = new plus.nativeObj.Bitmap('bitblmap');
b.loadBase64Data(dataUrl, function() {
/*这里一定要是_doc目录*/
b.save("_doc/" + fileName, {
overwrite: true
}, function(object) {
//保存到相册
plus.gallery.save("_doc/" + fileName, function() {
mui.toast("图片已保存到相册");
}, function() {
mui.toast("图片保存失败");
});
}, function() {
mui.toast("图片保存失败");
});
}, function() {
mui.toast("图片保存失败");
});
}
});
}
}
~~~