将范围滑块绑定到图像序列
Binding a Range Slider to Image Sequence
我想显示一个延时视频,但它带有一个范围滑块,以便用户可以来回拖动它。我试过一段视频,但我不喜欢滚动时的加载效果。我还将视频导出为图像并将其绑定到范围滑块。第一个问题是,是否有任何插件或应用程序可以执行此操作?也许我在搜索错误的术语。其次,图像序列是执行此操作的最佳方法吗?在我下面的代码中,我可以让它部分工作,但我不知道如何调整图像的大小,而且它对于 canvas.
来说太大了
<style>
canvas {
height: 650px;
width: 1000px;
border: 1px solid black;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<canvas id="canvas" height="650px" width="1000px"></canvas>
<br>
<input id="my-input" type="range" min="1" max="50" value="1">
<script type="text/javascript">
var canvas = document.getElementById("canvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
var totalImages = 50;
var videoFrames = [];
for (var i = 1; i <= totalImages; i++) {
var videoFrame = new Image;
var videoFrameUrl = 'http://dacwebdesign.com/testing/images/timelapse-' + i + '.jpg';
videoFrame.src = videoFrameUrl;
videoFrames.push(videoFrame);
}
$("#my-input").on("input", function(event) {
var currentImage = videoFrames[event.target.value - 1];
var context = canvas.getContext("2d");
context.drawImage(currentImage, 0, 0);
});
</script>
您可以通过以下方式调整图像大小以适应您的canvas:
var currentImage = videoFrames[event.target.value - 1];
var context = canvas.getContext("2d");
var dstWidth = canvas.width;
var dstHeight = canvas.height;
var srcWidth = currentImage.naturalWidth;
var srcHeight = currentImage.naturalHeight;
context.drawImage(currentImage, 0, 0, srcWidth, srcHeight, 0, 0, dstWidth, dstHeight);
这没有考虑宽高比,因此如果您的 canvas 与图像的宽高比不同,它会显得失真。
我想显示一个延时视频,但它带有一个范围滑块,以便用户可以来回拖动它。我试过一段视频,但我不喜欢滚动时的加载效果。我还将视频导出为图像并将其绑定到范围滑块。第一个问题是,是否有任何插件或应用程序可以执行此操作?也许我在搜索错误的术语。其次,图像序列是执行此操作的最佳方法吗?在我下面的代码中,我可以让它部分工作,但我不知道如何调整图像的大小,而且它对于 canvas.
来说太大了<style>
canvas {
height: 650px;
width: 1000px;
border: 1px solid black;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<canvas id="canvas" height="650px" width="1000px"></canvas>
<br>
<input id="my-input" type="range" min="1" max="50" value="1">
<script type="text/javascript">
var canvas = document.getElementById("canvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
var totalImages = 50;
var videoFrames = [];
for (var i = 1; i <= totalImages; i++) {
var videoFrame = new Image;
var videoFrameUrl = 'http://dacwebdesign.com/testing/images/timelapse-' + i + '.jpg';
videoFrame.src = videoFrameUrl;
videoFrames.push(videoFrame);
}
$("#my-input").on("input", function(event) {
var currentImage = videoFrames[event.target.value - 1];
var context = canvas.getContext("2d");
context.drawImage(currentImage, 0, 0);
});
</script>
您可以通过以下方式调整图像大小以适应您的canvas:
var currentImage = videoFrames[event.target.value - 1];
var context = canvas.getContext("2d");
var dstWidth = canvas.width;
var dstHeight = canvas.height;
var srcWidth = currentImage.naturalWidth;
var srcHeight = currentImage.naturalHeight;
context.drawImage(currentImage, 0, 0, srcWidth, srcHeight, 0, 0, dstWidth, dstHeight);
这没有考虑宽高比,因此如果您的 canvas 与图像的宽高比不同,它会显得失真。