首页 > 软件开发 > JQUERY >

Canvas分子式粒子动画特效

来源:互联网 2023-03-16 23:53:57 424

Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

方法/步骤

  • 1

    新建html文档。0x8办公区 - 实用经验教程分享!

    Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

  • 2

    书写hmtl代码。div id="jsi-particle-container" class="container">Canvas分子式粒子动画特效/div>0x8办公区 - 实用经验教程分享!

    Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

  • 3

    书写css代码。0x8办公区 - 实用经验教程分享!

    style>0x8办公区 - 实用经验教程分享!

    html, body { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; }0x8办公区 - 实用经验教程分享!

    .container { width: 100%; height: 100%; margin: 0; padding: 0; background-color: #000000; }0x8办公区 - 实用经验教程分享!

    /style>0x8办公区 - 实用经验教程分享!

    Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

  • 4

    书写并添加js代码。0x8办公区 - 实用经验教程分享!

    script src="js/jquery.min.js">/script>0x8办公区 - 实用经验教程分享!

    script>0x8办公区 - 实用经验教程分享!

    var RERER = {0x8办公区 - 实用经验教程分享!

    BASE_PARTICLE_COUNT :20,0x8办公区 - 实用经验教程分享!

    WATCH_INTERVAL : 50,0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    init : function(){0x8办公区 - 实用经验教程分享!

    this.setParameters();0x8办公区 - 实用经验教程分享!

    this.reconstructMethods();0x8办公区 - 实用经验教程分享!

    this.setup();0x8办公区 - 实用经验教程分享!

    this.bindEvent();0x8办公区 - 实用经验教程分享!

    this.render();0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    setParameters : function(){0x8办公区 - 实用经验教程分享!

    this.$window = $(window);0x8办公区 - 实用经验教程分享!

    this.$container = $('#jsi-particle-container');0x8办公区 - 实用经验教程分享!

    this.$canvas = $('canvas />');0x8办公区 - 实用经验教程分享!

    this.context = this.$canvas.appendTo(this.$container).get(0).getContext('2d');0x8办公区 - 实用经验教程分享!

    this.particles = [];0x8办公区 - 实用经验教程分享!

    this.watchIds = [];0x8办公区 - 实用经验教程分享!

    this.gravity = {x : 0, y : 0, on : false, radius : 100, gravity : true};0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    setup : function(){0x8办公区 - 实用经验教程分享!

    this.particles.length = 0;0x8办公区 - 实用经验教程分享!

    this.watchIds.length = 0;0x8办公区 - 实用经验教程分享!

    this.width = this.$container.width();0x8办公区 - 实用经验教程分享!

    this.height = this.$container.height();0x8办公区 - 实用经验教程分享!

    this.$canvas.attr({width : this.width, height : this.height});0x8办公区 - 实用经验教程分享!

    this.distance = Math.sqrt(Math.pow(this.width / 2, 2) Math.pow(this.height / 2, 2));0x8办公区 - 实用经验教程分享!

    this.createParticles();0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    reconstructMethods : function(){0x8办公区 - 实用经验教程分享!

    this.watchWindowSize = this.watchWindowSize.bind(this);0x8办公区 - 实用经验教程分享!

    this.jdugeToStopResize = this.jdugeToStopResize.bind(this);0x8办公区 - 实用经验教程分享!

    this.render = this.render.bind(this);0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    createParticles : function(){0x8办公区 - 实用经验教程分享!

    for(var i = 0, count = (this.BASE_PARTICLE_COUNT * this.width / 500 * this.height / 500) | 0; i count; i ){0x8办公区 - 实用经验教程分享!

    this.particles.push(new PARTICLE(this));0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    watchWindowSize : function(){0x8办公区 - 实用经验教程分享!

    this.clearTimer();0x8办公区 - 实用经验教程分享!

    this.tmpWidth = this.$window.width();0x8办公区 - 实用经验教程分享!

    this.tmpHeight = this.$window.height();0x8办公区 - 实用经验教程分享!

    this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL));0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    clearTimer : function(){0x8办公区 - 实用经验教程分享!

    while(this.watchIds.length > 0){0x8办公区 - 实用经验教程分享!

    clearTimeout(this.watchIds.pop());0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    jdugeToStopResize : function(){0x8办公区 - 实用经验教程分享!

    var width = this.$window.width(),0x8办公区 - 实用经验教程分享!

    height = this.$window.height(),0x8办公区 - 实用经验教程分享!

    stopped = (width == this.tmpWidth && height == this.tmpHeight);0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    this.tmpWidth = width;0x8办公区 - 实用经验教程分享!

    this.tmpHeight = height;0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    if(stopped){0x8办公区 - 实用经验教程分享!

    this.setup();0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    bindEvent : function(){0x8办公区 - 实用经验教程分享!

    this.$window.on('resize', this.watchWindowSize);0x8办公区 - 实用经验教程分享!

    this.$container.on('mousemove', this.controlForce.bind(this, true));0x8办公区 - 实用经验教程分享!

    this.$container.on('mouseleave', this.controlForce.bind(this, false));0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    controlForce : function(on, event){0x8办公区 - 实用经验教程分享!

    this.gravity.on = on;0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    if(!on){0x8办公区 - 实用经验教程分享!

    return;0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    var offset = this.$container.offset();0x8办公区 - 实用经验教程分享!

    this.gravity.x = event.clientX - offset.left this.$window.scrollLeft();0x8办公区 - 实用经验教程分享!

    this.gravity.y = event.clientY - offset.top this.$window.scrollTop();0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    render : function(){0x8办公区 - 实用经验教程分享!

    requestAnimationFrame(this.render);0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    var context = this.context;0x8办公区 - 实用经验教程分享!

    context.save();0x8办公区 - 实用经验教程分享!

    context.fillStyle = 'hsla(0, 0%, 0%, 0.3)';0x8办公区 - 实用经验教程分享!

    context.fillRect(0, 0, this.width, this.height);0x8办公区 - 实用经验教程分享!

    context.globalCompositeOperation = 'lighter';0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    for(var i = 0, particles = this.particles, gravity = this.gravity, count = particles.length; i count; i ){0x8办公区 - 实用经验教程分享!

    var particle = particles[i];0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    for(var j = i 1; j count; j ){0x8办公区 - 实用经验教程分享!

    particle.checkForce(context, particles[j]);0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    particle.checkForce(context, gravity);0x8办公区 - 实用经验教程分享!

    particle.render(context);0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    context.restore();0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    };0x8办公区 - 实用经验教程分享!

    var PARTICLE = function(renderer){0x8办公区 - 实用经验教程分享!

    this.renderer = renderer;0x8办公区 - 实用经验教程分享!

    this.init();0x8办公区 - 实用经验教程分享!

    };0x8办公区 - 实用经验教程分享!

    PARTICLE.prototype = {0x8办公区 - 实用经验教程分享!

    THRESHOLD : 100,0x8办公区 - 实用经验教程分享!

    SPRING_AMOUNT : 0.001,0x8办公区 - 实用经验教程分享!

    LIMIT_RATE : 0.2,0x8办公区 - 实用经验教程分享!

    GRAVIY_MAGINIFICATION : 10,0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    init : function(){0x8办公区 - 实用经验教程分享!

    this.radius = this.getRandomValue(5, 15);0x8办公区 - 实用经验教程分享!

    this.x = this.getRandomValue(-this.renderer.width * this.LIMIT_RATE, this.renderer.width * (1 this.LIMIT_RATE)) | 0;0x8办公区 - 实用经验教程分享!

    this.y = this.getRandomValue(-this.renderer.width * this.LIMIT_RATE, this.renderer.height * (1 this.LIMIT_RATE)) | 0;0x8办公区 - 实用经验教程分享!

    this.vx = this.getRandomValue(-3, 3);0x8办公区 - 实用经验教程分享!

    this.vy = this.getRandomValue(-3, 3);0x8办公区 - 实用经验教程分享!

    this.ax = 0;0x8办公区 - 实用经验教程分享!

    this.ay = 0;0x8办公区 - 实用经验教程分享!

    this.gravity = false;0x8办公区 - 实用经验教程分享!

    this.transformShape();0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    getRandomValue : function(min, max){0x8办公区 - 实用经验教程分享!

    return min (max - min) * Math.random();0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    transformShape : function(){0x8办公区 - 实用经验教程分享!

    var velocity = Math.sqrt(this.vx * this.vx this.vy * this.vy);0x8办公区 - 实用经验教程分享!

    this.scale = 1 - velocity / 15;0x8办公区 - 实用经验教程分享!

    this.hue = ((180 velocity * 10) % 360) | 0;0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    checkForce : function(context, particle){0x8办公区 - 实用经验教程分享!

    if(particle.gravity && !particle.on){0x8办公区 - 实用经验教程分享!

    return;0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    var dx = particle.x - this.x,0x8办公区 - 实用经验教程分享!

    dy = particle.y - this.y,0x8办公区 - 实用经验教程分享!

    distance = Math.sqrt(dx * dx dy * dy),0x8办公区 - 实用经验教程分享!

    magnification = (particle.gravity ? this.GRAVIY_MAGINIFICATION : 1);0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    if(distance > this.THRESHOLD * magnification){0x8办公区 - 实用经验教程分享!

    return;0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    var rate = this.SPRING_AMOUNT / magnification / (this.radius particle.radius);0x8办公区 - 实用经验教程分享!

    this.ax = dx * rate * particle.radius;0x8办公区 - 实用经验教程分享!

    this.ay = dy * rate * particle.radius;0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    if(!particle.gravity){0x8办公区 - 实用经验教程分享!

    particle.ax = -dx * rate * this.radius;0x8办公区 - 实用经验教程分享!

    particle.ay = -dy * rate * this.radius;0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    if(distance > this.THRESHOLD){0x8办公区 - 实用经验教程分享!

    return;0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    context.lineWidth = 3;0x8办公区 - 实用经验教程分享!

    context.strokeStyle = 'hsla(' this.hue ', 70%, 30%, ' (Math.abs(this.THRESHOLD - distance) / this.THRESHOLD) ')';0x8办公区 - 实用经验教程分享!

    context.beginPath();0x8办公区 - 实用经验教程分享!

    context.moveTo(this.x, this.y);0x8办公区 - 实用经验教程分享!

    context.lineTo(particle.x, particle.y);0x8办公区 - 实用经验教程分享!

    context.stroke();0x8办公区 - 实用经验教程分享!

    },0x8办公区 - 实用经验教程分享!

    render : function(context){0x8办公区 - 实用经验教程分享!

    context.save();0x8办公区 - 实用经验教程分享!

    context.fillStyle = 'hsl(' this.hue ', 70%, 40%)';0x8办公区 - 实用经验教程分享!

    context.translate(this.x, this.y);0x8办公区 - 实用经验教程分享!

    context.rotate(Math.atan2(this.vy, this.vx) Math.PI / 2);0x8办公区 - 实用经验教程分享!

    context.scale(this.scale, 1);0x8办公区 - 实用经验教程分享!

    context.beginPath();0x8办公区 - 实用经验教程分享!

    context.arc(0, 0, this.radius, 0, Math.PI * 2, false);0x8办公区 - 实用经验教程分享!

    context.fill();0x8办公区 - 实用经验教程分享!

    context.restore();0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    this.x = this.vx;0x8办公区 - 实用经验教程分享!

    this.y = this.vy;0x8办公区 - 实用经验教程分享!

    this.vx = this.ax;0x8办公区 - 实用经验教程分享!

    this.vy = this.ay;0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    if(this.x -this.radius && this.vx 0 || (this.x > this.renderer.width this.radius) && this.vx > 0 || this.y -this.radius && this.vy 0 || (this.y > this.renderer.height this.radius) && this.vy > 0){0x8办公区 - 实用经验教程分享!

    var theta = this.getRandomValue(0, Math.PI * 2),0x8办公区 - 实用经验教程分享!

    sin = Math.sin(theta),0x8办公区 - 实用经验教程分享!

    cos = Math.cos(theta),0x8办公区 - 实用经验教程分享!

    velocity = this.getRandomValue(-3, 3);0x8办公区 - 实用经验教程分享!

    0x8办公区 - 实用经验教程分享!

    this.x = -(this.renderer.distance this.radius) * cos this.renderer.width / 2;0x8办公区 - 实用经验教程分享!

    this.y = -(this.renderer.distance this.radius) * sin this.renderer.height / 2;0x8办公区 - 实用经验教程分享!

    this.vx = velocity * cos;0x8办公区 - 实用经验教程分享!

    this.vy = velocity * sin;0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    this.transformShape();0x8办公区 - 实用经验教程分享!

    }0x8办公区 - 实用经验教程分享!

    };0x8办公区 - 实用经验教程分享!

    $(function(){0x8办公区 - 实用经验教程分享!

    RERER.init();0x8办公区 - 实用经验教程分享!

    });0x8办公区 - 实用经验教程分享!

    /script>0x8办公区 - 实用经验教程分享!

    Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

  • 5

    代码整体结构。0x8办公区 - 实用经验教程分享!

    Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

  • 5本页面非法爬取自百度经验
  • 6

    查看效果。0x8办公区 - 实用经验教程分享!

    Canvas分子式粒子动画特效0x8办公区 - 实用经验教程分享!

  • 以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!0x8办公区 - 实用经验教程分享!


    标签: JQUERY

    办公区 Copyright © 2016-2023 www.bgqu.net. Some Rights Reserved. 备案号:湘ICP备2020019561号统计代码