js实现粒子时钟特效(渡一)

该代码示例展示了如何用JavaScript和HTML5Canvas创建一个粒子时钟效果。它涉及到粒子的生成、移动以及根据时间文本更新粒子位置,同时利用getImageData方法检测文本颜色来确定粒子的起点。
摘要由CSDN通过智能技术生成

js实现粒子时钟特效

源码:
JS

const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d', {
  willReadFrequently: true,
});

function initCanvasSize() {
  canvas.width = window.innerWidth * devicePixelRatio;
  canvas.height = window.innerHeight * devicePixelRatio;
}

initCanvasSize();

/**
 * 获取 [min, max] 范围内的随机整数
 */
function getRandom(min, max) {
  return Math.floor(Math.random() * (max + 1 - min) + min);
}

class Particle {
  constructor() {
    const r = Math.min(canvas.width, canvas.height) / 2;
    const cx = canvas.width / 2;
    const cy = canvas.height / 2;
    const rad = (getRandom(0, 360) * Math.PI) / 180;
    this.x = cx + r * Math.cos(rad);
    this.y = cy + r * Math.sin(rad);
    this.size = getRandom(2 * devicePixelRatio, 7 * devicePixelRatio);
  }

  draw() {
    ctx.beginPath();
    ctx.fillStyle = '#5445544d';
    ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
    ctx.fill();
  }

  moveTo(tx, ty) {
    const duration = 500; // 500ms的运动时间
    const sx = this.x,
      sy = this.y;
    const xSpeed = (tx - sx) / duration;
    const ySpeed = (ty - sy) / duration;
    const startTime = Date.now();
    const _move = () => {
      const t = Date.now() - startTime;
      const x = sx + xSpeed * t;
      const y = sy + ySpeed * t;
      this.x = x;
      this.y = y;
      if (t >= duration) {
        this.x = tx;
        this.y = ty;
        return;
      }
      // x,y改动一点
      requestAnimationFrame(_move);
    };
    _move();
  }
}

const partciles = [];
let text = null;
function clear() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

function draw() {
  clear();
  update();
  partciles.forEach((p) => p.draw());
  requestAnimationFrame(draw);
}

draw();

function getText() {
  return new Date().toTimeString().substring(0, 8);
}

function update() {
  const newText = getText();
  if (newText === text) {
    return;
  }
  clear();
  text = newText;
  // 画文本
  const { width, height } = canvas;
  ctx.fillStyle = '#000';
  ctx.textBaseline = 'middle';
  ctx.font = `${140 * devicePixelRatio}px 'DS-Digital', sans-serif`;
  ctx.fillText(text, (width - ctx.measureText(text).width) / 2, height / 2);
  const points = getPoints();
  clear();
  for (let i = 0; i < points.length; i++) {
    let p = partciles[i];
    if (!p) {
      p = new Particle();
      partciles.push(p);
    }
    const [x, y] = points[i];
    p.moveTo(x, y);
  }
  if (points.length < partciles.length) {
    partciles.splice(points.length);
  }
}

function getPoints() {
  const { width, height, data } = ctx.getImageData(
    0,
    0,
    canvas.width,
    canvas.height
  );
  const points = [];
  const gap = 6;
  for (let i = 0; i < width; i += gap) {
    for (let j = 0; j < height; j += gap) {
      const index = (i + j * width) * 4;
      const r = data[index];
      const g = data[index + 1];
      const b = data[index + 2];
      const a = data[index + 3];
      if (r === 0 && g === 0 && b === 0 && a === 255) {
        points.push([i, j]);
      }
    }
  }
  return points;
}

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>颗粒时钟</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      canvas {
        background: radial-gradient(#fff, #8c738c);
        display: block;
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <canvas></canvas>
    <script src="./index.js"></script>
  </body>
</html>

拿都拿了,点个赞收藏一下吧~~~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Three.js是一个基于WebGL的JavaScript库,用于创建和展示3D图形。它提供了丰富的功能和工具,可以轻松地实现各种动态粒子特效。 要实现动态粒子特效,可以按照以下步骤进行: 1. 创建场景(Scene):使用Three.js创建一个场景,用于容纳所有的物体和效果。 2. 创建相机(Camera):选择适合你需求的相机类型,例如透视相机(PerspectiveCamera)或正交相机(OrthographicCamera)。设置相机的位置和朝向,以便正确地观察场景。 3. 创建渲染器(Renderer):创建一个渲染器,将场景和相机渲染到屏幕上。可以选择使用WebGLRenderer或者CanvasRenderer,具体取决于你的需求。 4. 创建粒子(Particle):使用Particle或者Points等对象创建粒子。可以设置粒子的位置、大小、颜色等属性。 5. 创建材质(Material):为粒子创建材质,可以使用PointsMaterial或者ShaderMaterial等。设置材质的颜色、透明度、纹理等属性。 6. 创建动画(Animation):使用Tween.js或者自定义的动画库来实现粒子的动态效果。可以通过改变粒子的位置、大小、颜色等属性来实现动画效果。 7. 添加光源(Light):根据需要添加光源,例如环境光(AmbientLight)、平行光(DirectionalLight)或点光源(PointLight)等。光源可以影响粒子的明暗效果。 8. 渲染场景:在每一帧中,使用渲染器将场景和相机渲染到屏幕上。可以使用requestAnimationFrame来实现动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值