限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

js基于canvas鼠标跟随几何动画特效

所属分类: 网页特效-动画效果    2024-03-21 01:44:05

js基于canvas鼠标跟随几何动画特效 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

js基于canvas鼠标跟随几何动画特效(共3个文件)

    • index.html

使用方法

const { PI, cos, sin, abs, sqrt, pow, floor, round, random } = Math;
const HALF_PI = 0.5 * PI;
const TAU = 2 * PI;
const TO_RAD = PI / 180;
const rand = n => n * random();
const randRange = n => n - rand(2 * n);
const fadeIn = (t, m) => t / m;
const fadeOut = (t, m) => (m - t) / m;
const fadeInOut = (t, m) => {
  let hm = 0.5 * m;
  return abs((t + hm) % m - hm) / hm;
};

let canvas;
let ctx;
let particles;
let hover;
let mouse;
let tick;

function setup() {
  canvas = {
    a: document.createElement('canvas'),
    b: document.createElement('canvas') };

  ctx = {
    a: canvas.a.getContext('2d'),
    b: canvas.b.getContext('2d') };

  canvas.b.style = `
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	`;
  document.body.appendChild(canvas.b);
  particles = [];
  hover = false;
  mouse = { x: 0, y: 0 };
  tick = 0;
  resize();
  draw();
}

function resize() {
  canvas.a.width = canvas.b.width = window.innerWidth;
  canvas.a.height = canvas.b.height = window.innerHeight;
}

function mousehandler(e) {
  hover = e.type === 'mousemove';
  if (hover) {
    mouse.x = e.clientX;
    mouse.y = e.clientY;
  }
}

function getParticle(x, y) {
  return {
    position: { x, y },
    size: 2 + rand(20),
    speed: 2 + rand(5),
    direction: floor(rand(6)) * 60 * TO_RAD,
    turnDirection: randRange(1) * 0.1,
    directionChangeRate: 20 + round(rand(10)),
    hue: rand(90) + 180,
    ttl: 100 + rand(50),
    life: 0,
    destroy: false,
    update() {
      this.destroy = this.life++ > this.ttl;
      this.direction += this.life % this.directionChangeRate === 0 && round(randRange(1)) * 60 * TO_RAD;
      this.velocity = fadeInOut(this.life, this.ttl) * this.speed;
      this.position.x += cos(this.direction) * this.velocity;
      this.position.y += sin(this.direction) * this.velocity;
    },
    draw() {
      this.update();

      ctx.a.beginPath();
      ctx.a.lineWidth = 2;
      ctx.a.strokeStyle = `hsla(${this.hue},100%,50%,${fadeInOut(this.life, this.ttl)})`;
      ctx.a.strokeRect(this.position.x - 0.5 * this.size, this.position.y - 0.5 * this.size, this.size, this.size);
      ctx.a.closePath();
    } };

}

function draw() {
  tick++;
  ctx.a.clearRect(0, 0, canvas.a.width, canvas.a.height);
  if (!hover) {
    mouse.x = window.innerWidth * 0.5 + cos(tick * 0.05) * 200;
    mouse.y = window.innerHeight * 0.5 + sin(tick * 0.05) * 200;
  }
  tick % 2 === 0 && particles.push(getParticle(mouse.x, mouse.y));
  for (let i = particles.length - 1; i >= 0; i--) {
    particles[i].draw();
    if (particles[i].destroy) particles.splice(i, 1);
  }

  ctx.b.fillStyle = 'rgba(0,0,0,0.05)';
  ctx.b.fillRect(0, 0, canvas.b.width, canvas.b.height);

  ctx.b.save();
  ctx.b.globalCompositeOperation = "lighter";
  ctx.b.filter = "blur(8px)";
  ctx.b.drawImage(canvas.a, 0, 0, canvas.b.width, canvas.b.height);
  ctx.b.restore();

  ctx.b.save();
  ctx.b.globalCompositeOperation = "lighter";
  ctx.b.drawImage(canvas.a, 0, 0, canvas.b.width, canvas.b.height);
  ctx.b.restore();

  window.requestAnimationFrame(draw);
}

window.addEventListener("load", setup);
window.addEventListener("resize", resize);
window.addEventListener("mousemove", mousehandler);
window.addEventListener("mouseout", mousehandler);

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 动画效果

jquery css3扑克洗牌和扑克牌翻转效果代码

一款扑克自动发牌动画特效,带鼠标hover扑克牌翻转效果!
  动画效果
 2291  0

jquery动态数字翻滚计数到指定数字的动画特效

一款动态数字跳动计数动画插件, 这个功能实现了页面到达可视区域,实现动态数字翻滚计数到指定数字的文字特效。
  动画效果
 4672  0

jquery酷炫的视差动画效果插件

一款滚动的图片文字视差动画特效,背景图片静止固定,文字内容向上滚动。
  动画效果
 3452  0

javascript箭头跟随鼠标交互动画特效

一款跟随鼠标摆动方向的箭头动画代码,页面中鼠标移动,页面上的箭头会自动跟随鼠标调整方向。
  动画效果
 9366  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论