ExportCanvas是一款非常实用的可将canvas图像导出为多种格式图片的jQuery插件。该canvas图像导出插件使用浏览器的下载功能将canvas中的图像下载到本地,你可以指定多种类型的图片格式:PNG,JPEG,GIF,BMP,WEBP。
使用方法
在页面中引入jQuery和export-canvas.js文件。
导出图片
按照下面的格式编写js代码,即可将canvas中的图像通过浏览器下载到本地。
// The ID of canvas element you want to export
var exampleCanvasID = "myCanvas";
var exportCanvas = new ExportCanvas();
// Load the config & language javascript files
exportCanvas.loadConfigFile("core/config/config.js");
exportCanvas.loadLanguageFile("core/language/de.js");
// Set the MIMEType
exportCanvas.setMimeType("image/jpeg");
// Set the quality level when exporting (0.1 - 1.0)
exportCanvas.setExportQuality(1.0);
// The export method
exportCanvas.export(exampleCanvasID, function() {
// callback function
}, function(getLastReport) {
// callback function fired after the download has failed
});
// Export canvas without callbacks
exportCanvas.export(exampleCanvasID);
// Export canvas on document ready
window.onload = function() {
exportCanvas.export(exampleCanvasID);
插件中提供了两种语言:德语和英语。可以通过.loadLanguageFile()方法来切换。setMimeType()方法用于设置导出图片的类型。setExportQuality()是导出图片的质量,取值在0.1-1.0之间。最后可以使用export方法来导出图片。