返回列表 发布新帖
查看: 713|回复: 0

利用Canvas实现粒子图形变形动画特效

捣蛋鬼魔神·索伦森

梦之瑶-飞哥发表于 2020-3-22 15:21:25 | 查看全部 |阅读模式

您需要登录账号才能看到图片及隐藏内容,马上注册享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
10.png
利用Canvas实现粒子图形变形动画特效
特效描述:利用Canvas实现 粒子图形变形 动画特效,利用Canvas实现粒子图形变形动画特效
代码结构

1. 引入JS

  1. <script type="text/javascript" src="js/jquery.min.js"></script>
复制代码



2. HTML代码

  1. <div id="jsi-particle-container" class="container"></div>
  2. <script>
  3. var RENDERER = {
  4.     PARTICLE_COUNT : 1000,
  5.     PARTICLE_RADIUS : 1,
  6.     MAX_ROTATION_ANGLE : Math.PI / 60,
  7.     TRANSLATION_COUNT : 500,
  8.     init : function(strategy){
  9.         this.setParameters(strategy);
  10.         this.createParticles();
  11.         this.setupFigure();
  12.         this.reconstructMethod();
  13.         this.bindEvent();
  14.         this.drawFigure();
  15.     },
  16.     setParameters : function(strategy){
  17.         this.$window = $(window);
  18.         this.$container = $('#jsi-particle-container');
  19.         this.width = this.$container.width();
  20.         this.height = this.$container.height();
  21.         this.$canvas = $('<canvas />').attr({width : this.width, height : this.height}).appendTo(this.$container);
  22.         this.context = this.$canvas.get(0).getContext('2d');
  23.         this.center = {x : this.width / 2, y : this.height / 2};
  24.         this.rotationX = this.MAX_ROTATION_ANGLE;
  25.         this.rotationY = this.MAX_ROTATION_ANGLE;
  26.         this.strategyIndex = 0;
  27.         this.translationCount = 0;
  28.         this.theta = 0;
  29.         this.strategies = strategy.getStrategies();
  30.         this.particles = [];
  31.     },
  32.     createParticles : function(){
  33.         for(var i = 0; i < this.PARTICLE_COUNT; i ++){
  34.             this.particles.push(new PARTICLE(this.center));
  35.         }
  36.     },
  37.     reconstructMethod : function(){
  38.         this.setupFigure = this.setupFigure.bind(this);
  39.         this.drawFigure = this.drawFigure.bind(this);
  40.         this.changeAngle = this.changeAngle.bind(this);
  41.     },
  42.     bindEvent : function(){
  43.         this.$container.on('click', this.setupFigure);
  44.         this.$container.on('mousemove', this.changeAngle);
  45.     },
  46.     changeAngle : function(event){
  47.         var offset = this.$container.offset(),
  48.             x = event.clientX - offset.left + this.$window.scrollLeft(),
  49.             y = event.clientY - offset.top + this.$window.scrollTop();
  50.         this.rotationX = (this.center.y - y) / this.center.y * this.MAX_ROTATION_ANGLE;
  51.         this.rotationY = (this.center.x - x) / this.center.x * this.MAX_ROTATION_ANGLE;
  52.     },
  53.     setupFigure : function(){
  54.         for(var i = 0, length = this.particles.length; i < length; i++){
  55.             this.particles[i].setAxis(this.strategies[this.strategyIndex]());
  56.         }
  57.         if(++this.strategyIndex == this.strategies.length){
  58.             this.strategyIndex = 0;
  59.         }
  60.         this.translationCount = 0;
  61.     },
  62.     drawFigure : function(){
  63.         requestAnimationFrame(this.drawFigure);
  64.         this.context.fillStyle = 'rgba(0, 0, 0, 0.2)';
  65.         this.context.fillRect(0, 0, this.width, this.height);
  66.         for(var i = 0, length = this.particles.length; i < length; i++){
  67.             var axis = this.particles[i].getAxis2D(this.theta);
  68.             this.context.beginPath();
  69.             this.context.fillStyle = axis.color;
  70.             this.context.arc(axis.x, axis.y, this.PARTICLE_RADIUS, 0, Math.PI * 2, false);
  71.             this.context.fill();
  72.         }
  73.         this.theta++;
  74.         this.theta %= 360;
  75.         for(var i = 0, length = this.particles.length; i < length; i++){
  76.             this.particles[i].rotateX(this.rotationX);
  77.             this.particles[i].rotateY(this.rotationY);
  78.         }
  79.         this.translationCount++;
  80.         this.translationCount %= this.TRANSLATION_COUNT;
  81.         if(this.translationCount == 0){
  82.             this.setupFigure();
  83.         }
  84.     }
  85. };
  86. var STRATEGY = {
  87.     SCATTER_RADIUS :150,
  88.     CONE_ASPECT_RATIO : 1.5,
  89.     RING_COUNT : 5,
  90.     getStrategies : function(){
  91.         var strategies = [];
  92.         for(var i in this){
  93.             if(this[i] == arguments.callee || typeof this[i] != 'function'){
  94.                 continue;
  95.             }
  96.             strategies.push(this[i].bind(this));
  97.         }
  98.         return strategies;
  99.     },
  100.     createSphere : function(){
  101.         var cosTheta = Math.random() * 2 - 1,
  102.             sinTheta = Math.sqrt(1 - cosTheta * cosTheta),
  103.             phi = Math.random() * 2 * Math.PI;
  104.         return {
  105.             x : this.SCATTER_RADIUS * sinTheta * Math.cos(phi),
  106.             y : this.SCATTER_RADIUS * sinTheta * Math.sin(phi),
  107.             z : this.SCATTER_RADIUS * cosTheta,
  108.             hue : Math.round(phi / Math.PI * 30)
  109.         };
  110.     },
  111.     createTorus : function(){
  112.         var theta = Math.random() * Math.PI * 2,
  113.             x = this.SCATTER_RADIUS + this.SCATTER_RADIUS / 6 * Math.cos(theta),
  114.             y = this.SCATTER_RADIUS / 6 * Math.sin(theta),
  115.             phi = Math.random() * Math.PI * 2;
  116.         return {
  117.             x : x * Math.cos(phi),
  118.             y : y,
  119.             z : x * Math.sin(phi),
  120.             hue : Math.round(phi / Math.PI * 30)
  121.         };
  122.     },
  123.     createCone : function(){
  124.         var status = Math.random() > 1 / 3,
  125.             x,
  126.             y,
  127.             phi = Math.random() * Math.PI * 2,
  128.             rate = Math.tan(30 / 180 * Math.PI) / this.CONE_ASPECT_RATIO;
  129.         if(status){
  130.             y = this.SCATTER_RADIUS * (1 - Math.random() * 2);
  131.             x = (this.SCATTER_RADIUS - y) * rate;
  132.         }else{
  133.             y = -this.SCATTER_RADIUS;
  134.             x = this.SCATTER_RADIUS * 2 * rate * Math.random();
  135.         }
  136.         return {
  137.             x : x * Math.cos(phi),
  138.             y : y,
  139.             z : x * Math.sin(phi),
  140.             hue : Math.round(phi / Math.PI * 30)
  141.         };
  142.     },
  143.     createVase : function(){
  144.         var theta = Math.random() * Math.PI,
  145.             x = Math.abs(this.SCATTER_RADIUS * Math.cos(theta) / 2) + this.SCATTER_RADIUS / 8,
  146.             y = this.SCATTER_RADIUS * Math.cos(theta) * 1.2,
  147.             phi = Math.random() * Math.PI * 2;
  148.         return {
  149.             x : x * Math.cos(phi),
  150.             y : y,
  151.             z : x * Math.sin(phi),
  152.             hue : Math.round(phi / Math.PI * 30)
  153.         };
  154.     }
  155. };
  156. var PARTICLE = function(center){
  157.     this.center = center;
  158.     this.init();
  159. };
  160. PARTICLE.prototype = {
  161.     SPRING : 0.01,
  162.     FRICTION : 0.9,
  163.     FOCUS_POSITION : 300,
  164.     COLOR : 'hsl(%hue, 100%, 70%)',
  165.     init : function(){
  166.         this.x = 0;
  167.         this.y = 0;
  168.         this.z = 0;
  169.         this.vx = 0;
  170.         this.vy = 0;
  171.         this.vz = 0;
  172.         this.color;
  173.     },
  174.     setAxis : function(axis){
  175.         this.translating = true;
  176.         this.nextX = axis.x;
  177.         this.nextY = axis.y;
  178.         this.nextZ = axis.z;
  179.         this.hue = axis.hue;
  180.     },
  181.     rotateX : function(angle){
  182.         var sin = Math.sin(angle),
  183.             cos = Math.cos(angle),
  184.             nextY = this.nextY * cos - this.nextZ * sin,
  185.             nextZ = this.nextZ * cos + this.nextY * sin,
  186.             y = this.y * cos - this.z * sin,
  187.             z = this.z * cos + this.y * sin;
  188.         this.nextY = nextY;
  189.         this.nextZ = nextZ;
  190.         this.y = y;
  191.         this.z = z;
  192.     },
  193.     rotateY : function(angle){
  194.         var sin = Math.sin(angle),
  195.             cos = Math.cos(angle),
  196.             nextX = this.nextX * cos - this.nextZ * sin,
  197.             nextZ = this.nextZ * cos + this.nextX * sin,
  198.             x = this.x * cos - this.z * sin,
  199.             z = this.z * cos + this.x * sin;
  200.         this.nextX = nextX;
  201.         this.nextZ = nextZ;
  202.         this.x = x;
  203.         this.z = z;
  204.     },
  205.     rotateZ : function(angle){
  206.         var sin = Math.sin(angle),
  207.             cos = Math.cos(angle),
  208.             nextX = this.nextX * cos - this.nextY * sin,
  209.             nextY = this.nextY * cos + this.nextX * sin,
  210.             x = this.x * cos - this.y * sin,
  211.             y = this.y * cos + this.x * sin;
  212.         this.nextX = nextX;
  213.         this.nextY = nextY;
  214.         this.x = x;
  215.         this.y = y;
  216.     },
  217.     getAxis3D : function(){
  218.         this.vx += (this.nextX - this.x) * this.SPRING;
  219.         this.vy += (this.nextY - this.y) * this.SPRING;
  220.         this.vz += (this.nextZ - this.z) * this.SPRING;
  221.         this.vx *= this.FRICTION;
  222.         this.vy *= this.FRICTION;
  223.         this.vz *= this.FRICTION;
  224.         this.x += this.vx;
  225.         this.y += this.vy;
  226.         this.z += this.vz;
  227.         return {x : this.x, y : this.y, z : this.z};
  228.     },
  229.     getAxis2D : function(theta){
  230.         var axis = this.getAxis3D(),
  231.             scale = this.FOCUS_POSITION / (this.FOCUS_POSITION + axis.z);
  232.         return {x : this.center.x + axis.x * scale, y : this.center.y - axis.y * scale, color : this.COLOR.replace('%hue', this.hue + theta)};
  233.     }
  234. };
  235. $(function(){
  236.     RENDERER.init(STRATEGY);
  237. });
  238. </script>
  239. <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';color:#ffffff">
  240. </div>
复制代码





<
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

投诉/建议联系

xm520vip@gmail.com

未经授权禁止转载,复制和建立镜像,
如有违反,追究法律责任
  • 关注公众号
  • 关注小程序
Copyright © 2001-2025 灵歆论坛 版权所有 All Rights Reserved. |网站地图
关灯 在本版发帖
扫一扫进入小程序页面
QQ客服返回顶部
快速回复 返回顶部 返回列表