【问题标题】:Download multiple images from multiple canvas elements on single click单击即可从多个画布元素下载多个图像
【发布时间】:2017-09-02 17:30:55
【问题描述】:

下载图像时,我可以将画布数据中的图像附加到文档正文,但只下载最后一张图像,为什么?

var canvas = document.getElementsByClassName('canvas');
	for(var i=0; i<canvas.length; i++){
		function doCanvas(){
				ctx = canvas[i].getContext('2d');
				ctx.fillStyle = '#f90';
				ctx.fillRect(0, 0, canvas[i].width, canvas[i].height);
				ctx.fillStyle = '#fff';
				ctx.font = '30px sans-serif';
				ctx.fillText('Canvas_'+i, 10, canvas[i].height / 2 - 15);
		}
		doCanvas();
	}

	document.getElementById('download').addEventListener('click', downloadCanvas, false);
function downloadCanvas() {
				for(var j=0; j < canvas.length; j++){
				this.href = canvas[j].toDataURL();
					var im = document.createElement('img');
					im.src=this.href;
					document.body.appendChild(im);
				this.download = 'test'+j+'.png';
		}
	}
<canvas width="300" height="100" class="canvas"></canvas>
<canvas width="300" height="100" class="canvas"></canvas>
<a id="download" style="background: gray; height: 20px; text-align: center; display: block; width: 100px">Download</a>

【问题讨论】:

    标签: javascript html canvas html5-canvas


    【解决方案1】:

    经过多次搜索后,我在 JS 库下面找到了。

    jszip/
    jszip-utils
    FileSaver.js

    使用这三个,我现在只需单击即可下载多伙伴文件。

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      相关资源
      最近更新 更多