【发布时间】:2020-04-02 11:09:15
【问题描述】:
我正在使用此代码从我网站中的视频URL 中将图像捕获为画布:
var videoId = 'video';
var scaleFactor = 0.55; // increase or decrease size
var snapshots = [];
/**
* Captures a image frame from the provided video element.
*
* @param {Video} video HTML5 video element from where the image frame will be captured.
* @param {Number} scaleFactor Factor to scale the canvas element that will be return. This is an optional parameter.
*
* @return {Canvas}
*/
function capture(video, scaleFactor) {
if(scaleFactor == null){
scaleFactor = 1;
}
var w = video.videoWidth * scaleFactor;
var h = video.videoHeight * scaleFactor;
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, w, h);
var uniq = 'img_' + (new Date()).getTime();
canvas.setAttribute('id', uniq);
return canvas ;
}
/**
* Invokes the <code>capture</code> function and attaches the canvas element to the DOM.
*/
function shoot(){
var video = document.getElementById(videoId);
var output = document.getElementById('output');
var canvas = capture(video, scaleFactor);
snapshots.unshift(canvas);
output.innerHTML = '' ;
for(var i=0; i<10; i++){
output.appendChild(snapshots[i]);
}
}
我的两个问题:
1 - 目前,浏览器对待 <canvas> 就像对待 <div> 一样,这使得无法将任何生成的画布保存为图像,因为当我在每个人上 right-click 时,它总是打开窗口对话框这里我要选择Save image as...。
2 - 默认情况下,Windows right-click 对话框始终打开将图像另存为 transfer.png 的选项,我想使用其ID attribute (var uniq) 和 jpg 扩展名保存图像。
我需要的示例:
输出画布是这样的:<canvas width="352" height="198" id="img_1575807516362"></canvas>。
我希望right-click 打开窗口对话框,提供像这样img_1575807516362.jpg 保存图像。
或者,最好为每个画布设置一个下载按钮,以将canvas 导出为类似transfer.jpg 的图像。
是否可以使用此代码进行此操作?
【问题讨论】:
标签: javascript canvas html5-canvas export jpeg