简体   繁体   English

在html5画布上逐帧绘制视频并使用滑块进行控制

[英]Draw video frame-by-frame on html5 canvas and control with slider

I am creating a walkthrough video where the user can slide a UI slider across the screen, and the camera will walk through through a 3D space. 我正在创建一个演练视频,用户可以在其中跨屏幕滑动UI滑块,并且相机将在3D空间中漫游。

The video has been exported as jpg frames, and numbered 0 to 350.jpg. 视频已导出为jpg帧,编号为0至350.jpg。

I am pre-loading all of the frames first, and then applying the function to the slider change. 我先预加载所有帧,然后将功能应用于滑块更改。

This is the canvas 这是画布

<canvas id="walkthrough" width="1280" height="300"></canvas>

This is the function from the jQuery UI slider applying data.value 这是jQuery UI滑块中应用data.value的函数

$("[data-slider]")
.each(function () {
  var input = $(this);
  $("<span>")
    .addClass("output")
    .insertAfter($(this));
})
.bind("slider:ready slider:changed", function (event, data) {
  $(this)
    .nextAll(".output:first")
      .html(data.value.toFixed(3));
});

This is the image prelaod function 这是图像预编辑功能

var totalImages = 50; // Wow, so many images for such a short clip
var images = new Array();
for(var i = 1; i < totalImages; i++) {
    var filename = '/walkthrough/' + i + '.jpg'; // Filename of each image
    var img = new Image;
    img.src = filename;
    images.push(img);
}

This is the function that should draw the image to canvas when the slider is changed 更改滑块时,此功能应将图像绘制到画布上

$("#my-input").bind("slider:changed", function (event, data) {
    var currentimage = '/walkthrough/' + data.value + '.jpg';
    var canvas = document.getElementById("walkthrough");
    var context = canvas.getContext("2d");
    context.drawImage(currentimage,10,10);
});

I have tried to adapt this code from an article I read here which is using the scroll position to draw the image instead of a data.value. 我试图从我在此处阅读的文章中改编此代码,该文章使用滚动位置绘制图像而不是data.value。

http://elikirk.com/canvas-based-scroll-controlled-backgroud-video-use-parallax-style-web-design/ http://elikirk.com/canvas-based-scroll-controlled-backgroud-video-use-parallax-style-web-design/

I appreciate any help with this! 我对此表示感谢!

Here's a demo that uses a slider to change the image drawn on a canvas. 这是一个使用滑块更改在画布上绘制的图像的演示。 Some notable differences from your code: 与您的代码有一些明显的不同:

  • uses the native HTML5 slider instead of jQuery UI 使用原生HTML5滑块而不是jQuery UI
  • use the input event instead of the change event to detect slider changes 使用input事件而不是change事件来检测滑块更改
  • access the slider value with event.target.value instead of data (which isn't defined on the input event) 使用event.target.value而不是data (未在input事件中定义)访问滑块值
  • use the slider value as an index into the array of pre-loaded images instead of a file path 使用滑块值作为预加载图像数组的索引,而不是文件路径

 var canvas = document.getElementById("canvas"); canvas.height = 150; canvas.width = 400; var totalImages = 72; var videoFrames = []; for (var i = 1; i <= totalImages; i++) { var videoFrame = new Image; var videoFrameUrl = 'http://rphv.net/walkthrough/tmp-' + i + '.gif'; 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); }); 
 html, body { height: 100%; margin: 0 auto; } canvas { height: 150px; width: 400px; border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="canvas"></canvas> <br> <input id="my-input" type="range" min="1" max="72" value="1" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM