经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
canvas结合三角函数实现直播效果
来源:今日头条  作者:畅哥聊技术  时间:2019/6/4 0:51:48  对本文有异议

canvas可以说是html5其中的一大亮点,有了它,我们可以基于canvas画布实现很多之前只有flash和视频才能实现的效果。废话少说,先上效果。

1.gif

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  <meta charset="UTF-8">
  5.  <meta name="viewport" content="width=750, user-scalable=no">
  6.  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.  <title>canvas结合三角函数实现一个视频直播效果</title>
  8.  <style lang="">
  9.  * {
  10.  margin: 0;
  11.  padding: 0;
  12.  }
  13.  html,
  14.  body {
  15.  font-size: 0;
  16.  height: 100%;
  17.  }
  18.  canvas {
  19.  background: #000;
  20.  }
  21.  </style>
  22. </head>
  23. <body>
  24.  <canvas id='canvas'></canvas>
  25.  <script>
  26.  (function (document) {
  27.  class Point {
  28.  constructor(option) {
  29.  this.= option.context.canvas.width / 1.5;//对象的X坐标
  30.  this.= Math.random() * option.context.canvas.height/2 + option.context.canvas.height / 2 ;//对象的Y坐标
  31.  this.defaultX = this.x;
  32.  this.defaultY = this.y;
  33.  this.img = option.img;
  34.  this.angle = Math.random()*360 | 0;
  35.  this.context = option.context;
  36.  this.width = this.context.canvas.width;
  37.  this.height = this.context.canvas.height;
  38.  this.speedX = 0;//元素在x轴上的速度,下面要通过三角函数来实现。
  39.  this.speedY = -4* Math.random() - 8;
  40.  this.alpha = 1;
  41.  this.render();
  42.  }
  43.  render() {
  44.  var {
  45.  context,
  46.  img,
  47.  x,
  48.  y
  49.  } = this;
  50.  context.save();
  51.  context.globalAlpha = this.alpha ;
  52.  context.drawImage(img, x, y,img.width/2,img.height/2);
  53.  context.restore();
  54.  }
  55.  animate() {
  56.  this.angle +=3;
  57.  this.angle %= 360;
  58.  this.speedX = 4* Math.sin(this.angle/180*Math.PI*2);
  59.  this.+= this.speedX;
  60.  this.+= this.speedY;
  61.  var {width,height} = this;
  62.  this.alpha = this./ height / 2 + .2;
  63.  if (Math.abs(this.<= height / 2)) {
  64.  this.= height;
  65.  this.angle = Math.random() * 360 | 0;
  66.  this.alpha = 1;
  67.  this.= this.defaultX;
  68.  
  69.  }
  70.  this.render();
  71.  }
  72.  
  73.  }
  74.  var zmitiUtil = {
  75.  viewW: window.innerWidth,
  76.  viewH: window.innerHeight,
  77.  init() {
  78.  this.setSize();
  79.  this.createParticals();
  80.  this.animate();
  81.  },
  82.  setSize() {
  83.  this.canvas = document.querySelector('#canvas');
  84.  this.context = this.canvas.getContext('2d');
  85.  this.canvas.width = this.viewW;
  86.  this.canvas.height = this.viewH;
  87.  },
  88.  createParticals(){
  89.  this.particals = [];
  90.  var img = new Image();
  91.  var self = this;
  92.  img.onload = function(){
  93.  var _this = this;//这里面this指向的是img对象
  94.  for (var i = 0; i < 30; i++) {
  95.  self.particals.push(new Point({
  96.  img: _this,
  97.  context: self.context
  98.  }))
  99.  }
  100.  }
  101.  img.src = './images/heart.png';
  102.  
  103.  },
  104.  animate(){
  105.  var _this = this;
  106.  (function render(){//这里面this 发生了变化,请注意哦,因为出现了function 哦
  107.  requestAnimationFrame(render);
  108.  
  109.  _this.context.clearRect(0,0,_this.viewW,_this.viewH);
  110.  _this.particals.forEach(partical => {
  111.  partical.animate();
  112.  })
  113.  })();
  114.  }
  115.  };
  116.  zmitiUtil.init();
  117.  })(document);
  118.  </script>
  119. </body>
  120. </html>

技术总结:

先来简单回顾下高中的正弦曲线

3.jpg

  1. 我们会发现,图中的曲线和我们的效果是反着来的,所以我们要把x,y对应的调换下位置即可,运动起来就会在X轴的速度上体现出来了。具体可参考下源代码。

  2. 简单的解释下 this.speedX = 4* Math.sin(this.angle/180*Math.PI); this.angle为当前的角度,取值范围为:[0,360],js提供的三角函数是要接收一下弧度的,所以我们需要转一下。

  3. 解释 this.angle = Math.random() * 360 | 0; // 有的可能不太知道后面的 ”| 0“是什么意思,本屌之前也不知道是什么意思,自己试,多试几次,于是就知道了,这个是去掉了小数的部分,相当于 Math.floor() , 但要问这是什么原理,好像是二进制的算法,其实我也不知道,但我知道这个用法就行了。

  4. 注意下这个代码里在的有几个地方涉及到了this的指向问题哦。我在代码中已有注释。

  5. 像这种粒子动画的实现原理,先实现一个粒子的动画,然后循环生成一堆,把所有的push到一个数组中去。然后用动画函数遍历数组,执行数组中的每一个对象的运动函数。

  6. html5 canvas 画布一是无状态的机制。像类似 context 的 globalAlpha,translate,rotate等一些属性在操作之前需要加上context.save();在后面再context.restore();代码中也有体现。如果我们不加上context.save()那么,会给所有的粒子都设置了相同的透明试,这可不是我想要的。

  7. ES6的类的创建,解构赋值。

写在最后:

我们可以看到很漂亮的数学曲线,应用到web页面上,充分体现出了数学之美。希望大家在学习上遇到一些自己不懂的写法,一定要自己先去尝试,一定要自己尝试,还要要想尽办法应该到你们的项目中,这样我们印象才深刻。


 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号