【问题标题】:Need to change the JS events format to fit into Jquery and Meteor (Template.mysite.events) format需要更改 JS 事件格式以适应 Jquery 和 Meteor (Template.mysite.events) 格式
【发布时间】:2015-05-19 02:03:13
【问题描述】:

我想将此脚本粘贴到我的流星应用程序中,我不知道如何更改一些格式以使其适合我的 "Template.page.events" ,有人可以帮忙吗?

我是一个初学者,很难用它

(function(){
  var v = document.querySelector('video'),
      n = document.querySelector('source').src.replace(/.*\/|\..*$/g,''),
      c = document.querySelector('canvas'),
      save = document.querySelector('#save ul'),
      ctx = c.getContext('2d');
  v.addEventListener('loadedmetadata',function(ev){

    var ratio = v.videoWidth/v.videoHeight,
        w = 400,
        h = parseInt(w / ratio, 10),
        time = 0,
        img = null,
        li = null;
    c.width = w;
    c.height = h + 40;

    v.addEventListener('timeupdate',function(ev){
      if(v.paused){
        ctx.fillStyle = 'rgb(0, 0, 0)';
        ctx.fillRect(0, 0, w, h);
        ctx.drawImage(v, 0, 40, w, h);
        ctx.font = '20px Calibri';
        ctx.fillStyle = 'lime';
        ctx.fillText(n, 5, 20);
        time = format(v.currentTime);
        ctx.fillStyle = 'white';
        ctx.fillText(time, 395 - ctx.measureText(time).width, 20);
      }
    },false);
    c.addEventListener('click',function(ev){
      li = document.createElement('li');
      img = document.createElement('img');
      li.appendChild(img);
      save.appendChild(li);
      img.src = ctx.canvas.toDataURL('image/png');
    },false);
  },false);
  function format(time){
    var hours = parseInt((time / 60 / 60) % 60, 10),
        mins = parseInt((time / 60) % 60, 10),
        secs = parseInt(time, 10) % 60,
        hourss = (hours < 10 ? '0' : '') + parseInt(hours, 10) + ':',
        minss = (mins < 10 ? '0' : '') + parseInt(mins, 10) + ':',
        secss  = (secs < 10 ? '0' : '') +(secs % 60),
        timestring = ( hourss !== '00:' ? hourss : '' ) + minss + secss;
    return timestring;
  };
})();

【问题讨论】:

    标签: javascript jquery meteor


    【解决方案1】:

    假设我们有这个 Meteor 模板标记:

    <template name="page">
      <video controls autoplay>
        <source src="/videos/video.mp4">
      </video>
      <canvas></canvas>
      <ul>
        {{#each screenshots}}
          {{> screenshot}}
        {{/each}}
      </ul>
    </template>
    
    <template name="screenshot">
      <li>
        <img src="{{source}}">
      </li>
    </template>
    

    我添加了一个子模板来处理以 Meteor 方式生成的屏幕截图列表:无需直接操作 DOM 来插入 HTML 元素。

    首先我们需要创建一个reactive-var 来将截图列表源保存为一个数组:

    Template.page.onCreated(function(){
      this.screenshots = new ReactiveVar([]);
    });
    
    Template.page.helpers({
      screenshots:function(){
        return Template.instance().screenshots.get();
      }
    });
    

    别忘了meteor add reactive-var

    我们可以将videocanvas 元素在onRendered 模板生命周期事件中的引用保存为模板属性:

    Template.page.onRendered(function(){
      this.video = this.find("video");
      this.canvas = this.find("canvas");
    });
    

    然后我们必须使用 Meteor 事件映射重写标准的 HTML 事件处理,这非常简单,只需使用语法 "event selector"

    Template.page.events({
      "loadedmetadata video":function(event,template){
        var ratio = template.video.videoWidth / template.video.videoHeight;
        var canvasWidth = 400;
        var canvasHeight = parseInt(canvasWidth / ratio, 10);
        template.canvas.width = w;
        template.canvas.height = h + 40;
      },
      "timeupdate video":function(event,template){
        function format(time){
          var hours = parseInt((time / 60 / 60) % 60, 10);
          var mins = parseInt((time / 60) % 60, 10);
          var secs = parseInt(time, 10) % 60;
          var hourss = (hours < 10 ? '0' : '') + parseInt(hours, 10) + ':';
          var minss = (mins < 10 ? '0' : '') + parseInt(mins, 10) + ':';
          var secss  = (secs < 10 ? '0' : '') +(secs % 60);
          var timestring = ( hourss !== '00:' ? hourss : '' ) + minss + secss;
          return timestring;
        }
        //
        if(template.video.paused){
          var ctx = template.canvas.getContext("2d");
          ctx.fillStyle = "rgb(0, 0, 0)";
          ctx.fillRect(0, 0, template.canvas.width, template.canvas.height);
          ctx.drawImage(template.video, 0, 40, template.canvas.width, template.canvas.height);
          ctx.font = "20px Calibri";
          ctx.fillStyle = "lime";
          ctx.fillText("caption", 5, 20);
          var time = format(template.video.currentTime);
          ctx.fillStyle = "white";
          ctx.fillText(time, 395 - ctx.measureText(time).width, 20);
        }
      },
      "click canvas":function(event,template){
        var screenshots = template.screenshots.get();
        screenshots.push({
          source: template.canvas.toDataURL("image/png")
        });
        template.screenshots.set(screenshots);
      }
    });
    

    【讨论】:

    • 嗨,感谢您的回答,这帮助我学到了很多东西(代码的前 3 次打印)但是在设置事件映射时我感到困惑,任何机会完成代码以使其可行我会从那里学习吗?我看到您也在 timeupdate 和 loadmetadata 中定义了 var v,所以我对如何复制附加代码(以及时间格式部分)以使其可行......感谢您的帮助,非常感谢它,并会看到如何给你积分。
    • 并且需要指出我的反应变量,第一次发现它。
    • 我修改了我的答案以提供一个完整的工作示例(在本地测试)。
    • 谢谢 :) 感谢您的宝贵时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多