我如何按比例调整画布中的视频大小
How do I proportionally resize a video in canvas?
在画布上渲染视频元素时,我在按比例缩放视频帧时遇到了问题。
我试过了:
我读了选择的答案如何按比例调整画布中的图像?但这对我不起作用。我正在渲染HTML5视频帧而不是图像。这可能看起来不寻常,所以在这里解释一下:video + canvas = magic
我在回答相关问题时遇到的问题是:
- 可能视频大小与图像大小不同。
- 也许我错误地将用于特定情况的答案转换为我的答案,因为它在多个地方使用了令人困惑的静态测量('300'),而没有真正解释它们。
目前我有什么
现在,我的播放器工作,适合视频,但它延伸到适合。
context.drawImage(videoPlayer, 0, 0, canvasPlayer.width, canvasPlayer.height);
//canvasPlayer -> HTML5 canvas, videoPlayer -> HTML5 video element
我想知道(分别):
如何垂直填充高度,保持比例
如何横向填充宽度,保持成比例
我该怎么做?
这是一个让视频按比例绘制到画布(保持aspectRatio)的方法:
// var c = canvasElement, v=videoElement;
// fill vertically
var vRatio = (c.height / v.videoHeight) * v.videoWidth;
ctx.drawImage(v, 0,0, vRatio, c.height);
// fill horizontally
var hRatio = (c.width / v.videoWidth) * v.videoHeight;
ctx.drawImage(v, 0,0, c.width, hRatio);
function draw(){
// fill vertically
var vratio = (c.height / v.videoHeight) * v.videoWidth;
ctx.drawImage(v, 0,0, vratio, c.height);
// fill horizontally
var hratio = (c.width / v.videoWidth) * v.videoHeight;
ctx1.drawImage(v, 0,0, c1.width, hratio);
requestAnimationFrame(draw);
}
var c=document.createElement('canvas');
c.width = 640;
c.height= 480;
var ctx = c.getContext('2d')
var c1 = c.cloneNode(true);
var ctx1 = c1.getContext('2d')
var v = document.createElement('video');
v.controls=true;
document.body.appendChild(document.createTextNode('fill vertical'n'));
document.body.appendChild(c);
document.body.appendChild(document.createTextNode('fill horizontal'n'));
document.body.appendChild(c1);
document.body.appendChild(document.createTextNode('original'));
document.body.appendChild(v);
var anim;
v.onplaying = function(){
anim = requestAnimationFrame(draw);
};
v.onpause= function(){
cancelAnimationFrame(anim);
};
v.onloadedmetadata = function(){v.play();};
v.src = 'http://media.w3.org/2010/05/sintel/trailer.mp4';
var inputs = document.querySelectorAll('input');
inputs[0].addEventListener('change', inpHandler);
inputs[1].addEventListener('change', inpHandler);
function inpHandler(){
c[this.name]=this.value;
c1[this.name]=this.value;
v.currentTime=0;
v.play();
}
canvas{border:solid 1px;}
canvas,video{ display:block;}
<label for="width">width</label>
<input name="width" type="number" value="640"/>
<label for="height">height</label>
<input name="height" type="number" value="480"/></br>
我给你做了一个例子,看看吧!
代码笔:http://codepen.io/cstony0917/pen/BNvxZP?editors=101
var v = document.getElementById("video1");
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var i;
var video_width = v.offsetWidth;
var video_height = v.offsetHeight;
var ratio = video_width / video_height;
var target_width;
var target_height;
var y_of_video = 0;
var x_of_video = 0
if ( video_width > video_height ){
target_width = c.width;
target_height = c.width / ratio;
y_of_video = (c.height - target_height) / 2 ;
}else{
target_width = c.height;
target_height = c.height * ratio;
x_of_video = (c.width - target_width) / 2 ;
}
v.addEventListener("play", function() {
i = window.setInterval(function() {
ctx.drawImage(v, x_of_video, y_of_video, target_width, target_height);
},20);
}, false);
v.addEventListener("pause", function() {window.clearInterval(i);}, false);
v.addEventListener("ended", function() {clearInterval(i);}, false);
@Tony Hsieh解决方案的一个小修复
if ( video_width > video_height ){
target_width = c.width;
target_height = c.width / ratio;
y_of_video = (c.height - target_height) / 2 ;
} else{
target_width = c.height * ratio;
target_height = c.height;
x_of_video = (c.width - target_width) / 2 ;
}
相关文章:
- 在javascript中添加和显示对象的随机数组中的视频
- 如何解析Brightcove视频中的视频缩略图
- HTML5视频无法在chrome上工作,在服务器上托管后的IE,mov文件中的视频(quicktime格式)
- 通过Jquery更改html5中的视频(不同质量)文件
- 安卓系统:通过loadUrl运行javascript时,网络视图中的视频会冻结
- 更改网络播放器中的视频源
- html5中的视频,内容类型视频/x-msvideo
- 触摸设备中的视频JS播放器覆盖按钮
- 尝试运行下一个上一个按钮以播放列表媒体播放器代码中的视频,出现错误
- 读取视频中的视频元素/属性的正确方法是什么.js
- jquery插件,引用DOM中的视频元素
- 无法查看此代码中的视频
- HTML5播放器不会在智能手机上播放子域中的视频
- 移动浏览器中的视频DOM功能:如何从定时功能停止视频
- 3djs中的视频标签
- loadVideoById()不会更改youtube播放器中的视频
- Youtube视频播放按钮在Android网络视图中的视频iframe中不起作用
- 从UIWebView引用存储在NSDocumentDirectory中的视频
- 在souce代码中的视频标签src属性中显示不同的URL或隐藏URL
- 如何停止jwplayer中的视频点击功能