1

VideoClip

Objective
  • Responder
    • View
      • Clip
        • VideoClip

Move the pointer of the mouse over the clip. Click in the clip or press the space bar to start playing it. Click or press again the space bar to pause it, continue to play it. Use the right and left arrows to move forward or backward by one second, press the Shift key or the Ctrl key at the same time to move forward or backward by ten seconds or a hundred milliseconds. Press the 0 key to come back to the beginning. Press the plus key to play the clip faster, the minus key to play it slower, the asterisk key to play it at the normal speed.

NOTE: A shrink effect programmed on the clip changes the dimensions of the video display over a period of ten seconds.

In the console of the browser, turn the sound off:

clip.muted=true
  1. function VideoClip() {
  2.     Clip.call(this);
  3.  
  4.     this._drawingarea = new DrawingArea();
  5.  
  6.     this._video = null;
  7.  
  8.     this._animlist = null;
  9.  
  10.     this._timeWidget = null;
  11.  
  12.     this._mutedWidget = null;
  13.  
  14.     this._onmute = null;
  15.  
  16.     this._streamVideo = null;
  17. }
  18.  
  19. VideoClip.prototype = Object.create(Clip.prototype);
  20.  
  21. Object.defineProperty(VideoClip.prototype, 'constructor', { value: VideoClip, enumerable: false, writable: true });
  22.  
  23. Object.defineProperty(VideoClip.prototype, 'dimension', {
  24.     get:    function() {
  25.                 return this._drawingarea.size;
  26.             },
  27.     set:    function(d) {
  28.                 this._drawingarea.size = d;
  29.             }
  30. });
  31.  
  32. Object.defineProperty(VideoClip.prototype, 'duration', {
  33.     get:    function() {
  34.                 return this._video ? Math.floor(this._video.duration * 1000) : 0;
  35.             }
  36. });
  37.  
  38. Object.defineProperty(VideoClip.prototype, 'currentTime', {
  39.     get:    function() {
  40.                 return this._video && this._video.currentTime > 0 ? Math.floor(this._video.currentTime * 1000) - 1 : 0;
  41.             }
  42. });
  43.  
  44. Object.defineProperty(VideoClip.prototype, 'playbackRate', {
  45.     get:    function() {
  46.                 return this._playbackRate;
  47.             },
  48.     set:    function(r) {
  49.                 if (typeof r !== 'number')
  50.                     throw new TypeError();
  51.  
  52.                 if (r < Clip.minPlaybackRate || r > Clip.maxPlaybackRate)
  53.                     throw new RangeError();
  54.  
  55.                 this._playbackRate = r;
  56.  
  57.                 if (this._video)
  58.                     this._video.playbackRate = r;
  59.  
  60.                 if (this._animlist)
  61.                     for (let e of this._animlist)
  62.                         e.playbackRate = r;
  63.             }
  64. });
  65.  
  66. Object.defineProperty(VideoClip.prototype, 'muted', {
  67.     get:    function() {
  68.                 return this._video ? this._video.muted : false;
  69.             },
  70.     set:    function(muted) {
  71.                 if (this._video) {
  72.                     this._video.muted = muted ? true : false;
  73.  
  74.                     if (this._mutedWidget)
  75.                         this._mutedWidget.checked = this._video.muted;
  76.                 }
  77.         }
  78. });
  79.  
  80. VideoClip.prototype.get = function() {
  81.     return this._drawingarea.getOptions();
  82. }
  83.  
  84. VideoClip.prototype.set = function(options) {
  85.     this._drawingarea.setOptions(options);
  86.  
  87.     return this;
  88. }
  89.  
  90. VideoClip.prototype.setValue = function(prop, val) {
  91.     if (prop == 'size')
  92.         this._drawingarea.size = val;
  93.     else if (prop == 'hflip')
  94.         this._drawingarea.hflip = val;
  95.     else if (prop == 'vflip')
  96.         this._drawingarea.vflip = val;
  97.     else if (prop == 'grayscale')
  98.         this._drawingarea.grayscale = val;
  99.     else if (prop == 'sepia')
  100.         this._drawingarea.sepia = val;
  101.     else if (prop == 'blur')
  102.         this._drawingarea.blur = val;
  103.     else if (prop == 'invert')
  104.         this._drawingarea.invert = val;
  105.     else if (prop == 'contrast')
  106.         this._drawingarea.contrast = val;
  107.     else if (prop == 'saturate')
  108.         this._drawingarea.saturate = val;
  109.     else if (prop == 'brightness')
  110.         this._drawingarea.brightness = val;
  111.     else if (prop == 'opacity')
  112.         this._drawingarea.opacity = val;
  113.  
  114.     return this;
  115. }
  116.  
  117. VideoClip.prototype.animate = function(animations) {
  118.     if (! (Array.isArray(animations) && animations.length > 0))
  119.         throw new TypeError();
  120.  
  121.     if (!this._widget)
  122.         return this;
  123.  
  124.     if (this._animlist)
  125.         for (let e of this._animlist)
  126.             e.cancel();
  127.  
  128.     const animlist = [];
  129.  
  130.     for (let e of animations) {
  131.         if (! (Array.isArray(e) && e.length == 2))
  132.             throw new TypeError();
  133.  
  134.         let [keyframes, options] = e;
  135.  
  136.         let anim = this._widget.animate(keyframes, options);
  137.  
  138.         anim.pause();
  139.  
  140.         animlist.push(anim);
  141.     }
  142.  
  143.     this._animlist = animlist;
  144.  
  145.     return this;
  146. }
  147.  
  148. VideoClip.prototype.seek = function(ms) {
  149.     if (!this._video)
  150.         return this;
  151.  
  152.     this._video.currentTime = (ms + 1) / 1000;  // +1 for Chrome
  153.  
  154.     if (this._animlist) {
  155.         for (let e of this._animlist) {
  156.             e.currentTime = ms;
  157.             if (this._paused)
  158.                 e.pause();
  159.         }
  160.     }
  161.  
  162.     return this;
  163. }
  164.  
  165. VideoClip.prototype.play = function() {
  166.     if (!this._video)
  167.         return this;
  168.  
  169.     if (this._ended)
  170.         this.seek(0);
  171.  
  172.     this._ended = this._paused = false;
  173.  
  174.     this._video.play();
  175.  
  176.     if (this._animlist) {
  177.         for (let e of this._animlist)
  178.             e.play();
  179.     }
  180.  
  181.     return this;
  182. }
  183.  
  184. VideoClip.prototype.pause = function() {
  185.     if (!this._video)
  186.         return this;
  187.  
  188.     this._paused = true;
  189.  
  190.     this._video.pause();
  191.  
  192.     if (this._animlist) {
  193.         for (let e of this._animlist)
  194.             e.pause();
  195.     }
  196.  
  197.     return this;
  198. }
  199.  
  200. VideoClip.prototype.setWidget = function(w) {
  201.     if (w.tagName != 'VIDEO')
  202.         throw new TypeError();
  203.  
  204.     this._drawingarea.setWidget(w);
  205.  
  206.     Clip.prototype.setWidget.call(this, this._drawingarea.widget);
  207.  
  208.     w.style.display = 'none';
  209.  
  210.     w.playbackRate = this._playbackRate;
  211.  
  212.     w.after(this._drawingarea.widget);
  213.  
  214.     this._video = w;
  215.  
  216.     w.onseeked = () => {
  217.         this._drawingarea.setImage(this._video);
  218.  
  219.         if (this._timeWidget)
  220.             this._timeWidget.innerText = VideoClip._toHHMMSS(this._video.currentTime);
  221.  
  222.         this.notify('clipSeeked', this);
  223.     };
  224.  
  225.     w.onended = () => {
  226.         this._ended = this._paused = true;
  227.  
  228.         if (this._animlist) {
  229.             for (let e of this._animlist)
  230.                 e.pause();
  231.         }
  232.  
  233.         this.notify('clipEnded', this);
  234.     };
  235.  
  236.     w.load();
  237.  
  238.     return this;
  239. }
  240.  
  241. VideoClip.prototype.addTimeWidget = function(w) {
  242.     w.innerText = VideoClip._toHHMMSS(this._video.currentTime);
  243.  
  244.     this._timeWidget = w;
  245.  
  246.     return this;
  247. }
  248.  
  249. VideoClip.prototype.removeTimeWidget = function() {
  250.     this._timeWidget = null;
  251.  
  252.     return this;
  253. }
  254.  
  255. VideoClip.prototype.addMutedWidget = function(w) {
  256.     if (this._onmute)
  257.         this.removeEventListener('change', this._onmute);
  258.  
  259.     this._onmute = () => this._video.muted = this._mutedWidget.checked;
  260.  
  261.     w.addEventListener('change', this._onmute);
  262.  
  263.     w.checked = this._video.muted;
  264.  
  265.     this._mutedWidget = w;
  266.  
  267.     return this;
  268. }
  269.  
  270. VideoClip.prototype.removeMutedWidget = function() {
  271.     if (this._onmute)
  272.         this.removeEventListener('change', this._onmute);
  273.  
  274.     this._mutedWidget = null;
  275.  
  276.     return this;
  277. }
  278.  
  279. VideoClip.prototype.enablePlayer = function() {
  280.     if (this.hasPlayer())
  281.         return this;
  282.  
  283.     Clip.prototype.enablePlayer.call(this);
  284.  
  285.     if (this._streamvideo === undefined) {
  286.         this._streamvideo = () => {
  287.             if (this._video.paused || this._video.ended)
  288.                 return;
  289.  
  290.             this._drawingarea.setImage(this._video);
  291.  
  292.             if (this._timeWidget)
  293.                 this._timeWidget.innerText = VideoClip._toHHMMSS(this._video.currentTime);
  294.  
  295.             requestAnimationFrame(this._streamvideo);
  296.         };
  297.  
  298.         this._video.onplay = this._streamvideo;
  299.     }
  300.  
  301.     return this;
  302. }
  303.  
  304. VideoClip._toHHMMSS = function(nsecs) {
  305.     nsecs = Math.floor(nsecs);
  306.  
  307.     let hh = Math.floor(nsecs / 3600);
  308.     let mm = Math.floor((nsecs - (hh * 3600)) / 60);
  309.     let ss = nsecs - (hh * 3600) - (mm * 60);
  310.  
  311.     hh = (hh < 10 ? '0' : '') + hh;
  312.     mm = (mm < 10 ? '0' : '') + mm;
  313.     ss = (ss < 10 ? '0' : '') + ss;
  314.  
  315.     return `${hh}:${mm}:${ss}`;
  316. }
Test
  1. <?php $src='/files/videos/Horloge.webm'; ?>
  2. <?php $type='video/webm'; ?>
  3. <?php $width=854; ?>
  4. <?php $height=480; ?>
  1. <?php $id=uniqid('id'); ?>
  1. .test_display {
  2.     display: inline-flex;
  3.     flex-direction: column;
  4.     align-items: flex-start;
  5.     background: black;
  6. }
  1. <div id="<?php echo $id; ?>" class="noprint">
  2. <div class="test_display">
  3. <video width="<?php echo $width; ?>" height="<?php echo $height; ?>" preload="auto">
  4. <source src="<?php echo $src; ?>" type="<?php echo $type; ?>" />
  5. </video>
  6. </div>
  7. </div>
  1. <?php head('javascript', '/objectivejs/Objective.js'); ?>
  2. <?php head('javascript', '/objectivejs/Responder.js'); ?>
  3. <?php head('javascript', '/objectivejs/View.js'); ?>
  4. <?php head('javascript', '/objectivejs/Clip.js'); ?>
  5. <?php head('javascript', '/objectivejs/VideoClip.js'); ?>
  6. <?php head('javascript', '/objectivejs/DrawingArea.js'); ?>
  1. const clip = new VideoClip();
  2.  
  3. const container = document.querySelector('#<?php echo $id; ?>');
  4.  
  5. clip.setManagedWidget(container.querySelector('video'));
  1. const keyframes = [ {transform: 'scale(1)'}, {transform: 'scale(0.8)'}, {transform: 'scale(1)'}];
  2. const timing = { duration: 10000, iterations: Infinity };
  3.  
  4. clip.animate([[keyframes, timing]]);
  1. clip.dimension = [640, 360];
  1. clip.enablePlayer();
SEE ALSO

Clip, AnimateClip, ProgramClip, DrawingArea

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].