JavaScript plays MP4 animation with transparent channel

inspirer 2020-11-12 16:44:02
javascript plays mp4 mp animation


With the development of the Internet , Animation effects are constantly updated , From the beginning flsh Animation ,cocos Skeletal animation , To YY Open source svga Animation . lately 1 Over the years , With transparent channels mp4 Animation is widely used , about app End .github Open source AlphaPlayer have access to , But for web End , There is little information about it . There are many ways to solve this problem , Proposed the use of webm Format . But in the end, it's time to play mp4 Format .

Play mp4 Transparent animation of format , What do you need . First , about web Front end ,webgl We should seldom introduce , Let alone shaders (shader) and OpenGL ES Grammar. . Gave up several times , Finally, I saw a post , It can be used THREEJS To carry out . Let's start with a picture

 

  You can see that the left part of the material uses RGB The channel stores the original transparent video Alpha value , The right part uses RGB The channel stores the original transparent video RGB value , And then on the end through OpenGL Re put each pixel's Alpha Values and RGB Values to combine , Get... Again ARGB Video footage , Realize the animation effect of transparent video .

stay web End , Concrete realization .

1. There is one video player , Play mp4 Animation

2. Use threejs and openel Redraw the picture into a container .

 1 var lastUpdate;
2 var container;
3 var camera, scene, renderer;
4 var uniforms;
5
6 function init() {
7
8 container = document.getElementById('container');
9 camera = new THREE.Camera();
10 camera.position.z = 1;
11 scene = new THREE.Scene();
12
13 var video = document.getElementById('video');
14 video.src = "https://devimage.91banban.com/topcard.mp4";
15  video.play();
16 video.onended = () => {
17  video.play();
18  }
19 videoTexture = new THREE.VideoTexture(video);
20 videoTexture.minFilter = THREE.LinearFilter;
21 videoTexture.magFilter = THREE.LinearFilter;
22 videoTexture.format = THREE.RGBAFormat;
23
24 // shader stuff
25 uniforms = {
26 time: { type: "f", value: 1.0 },
27 texture: { type: "sampler2D", value: videoTexture }
28  };
29 var material = new THREE.ShaderMaterial({
30  uniforms: uniforms,
31  vertexShader: `varying vec2 vUv;
32 void main() {
33 vUv = uv;
34 gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
35  }`,
36  fragmentShader: `#ifdef GL_ES
37 precision highp float;
38  #endif
39
40 uniform float time;
41  uniform sampler2D texture;
42  varying vec2 vUv;
43
44 void main( void ) {
45 gl_FragColor = vec4(
46 texture2D(texture, vec2(0.5+vUv.x/2., vUv.y)).rgb,
47 texture2D(texture, vec2(vUv.x/2., vUv.y)).r
48  );
49  }`,
50 transparent: true
51  });
52 lastUpdate = new Date().getTime();
53 var geometry = new THREE.PlaneBufferGeometry(2, 2);
54 var mesh = new THREE.Mesh(geometry, material);
55 mesh.scale.setScalar(0.8);
56  scene.add(mesh);
57 renderer = new THREE.WebGLRenderer({ alpha: true });
58 renderer.setPixelRatio(window.devicePixelRatio / 1);
59
60  container.appendChild(renderer.domElement);
61
62 document.getElementById('play-button').addEventListener('click', e => { video.play(); });
63 renderer.setSize(1000, 1100);
64 this.animate();
65 }
66
67 function animate() {
68 var currentTime = new Date().getTime()
69 var timeSinceLastUpdate = currentTime - lastUpdate;
70 lastUpdate = currentTime;
71  requestAnimationFrame(animate);
72  render(timeSinceLastUpdate);
73
74 }
75 function render(timeDelta) {
76 uniforms.time.value += (timeDelta ? timeDelta / 1000 : 0.05);
77  renderer.render(scene, camera);
78 }
79
80 init();

Final effect drawing

 

 

版权声明
本文为[inspirer]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple