当前位置:首页 > JavaScript

js实现光圈效果

2026-04-04 10:13:03JavaScript

js实现光圈效果

js实现光圈效果

使用CSS和JavaScript实现光圈效果

光圈效果可以通过CSS动画结合JavaScript动态控制实现。以下是一种常见的实现方式,通过圆形元素和透明度变化模拟光圈扩散效果。

创建HTML结构

<div class="circle-container">
  <div class="circle"></div>
</div>

添加基础CSS样式

.circle-container {
  position: relative;
  width: 100px;
  height: 100px;
}

.circle {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(0, 150, 255, 0.5);
  transform: scale(0);
  animation: pulse 2s infinite;
}

添加动画关键帧

@keyframes pulse {
  0% {
    transform: scale(0);
    opacity: 1;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}

使用JavaScript动态创建光圈

function createRipple(event) {
  const container = document.querySelector('.circle-container');
  const circle = document.createElement('div');
  circle.classList.add('circle');

  // 设置光圈位置(基于点击事件)
  circle.style.left = `${event.clientX - container.offsetLeft - 50}px`;
  circle.style.top = `${event.clientY - container.offsetTop - 50}px`;

  container.appendChild(circle);

  // 动画结束后移除元素
  circle.addEventListener('animationend', () => {
    circle.remove();
  });
}

// 点击触发光圈
document.addEventListener('click', createRipple);

多光圈同时存在的实现

function createAdvancedRipple() {
  const colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00'];
  const container = document.querySelector('.circle-container');

  setInterval(() => {
    const circle = document.createElement('div');
    circle.classList.add('circle');

    // 随机属性
    const size = Math.random() * 50 + 50;
    const color = colors[Math.floor(Math.random() * colors.length)];

    circle.style.width = `${size}px`;
    circle.style.height = `${size}px`;
    circle.style.background = color;
    circle.style.left = `${Math.random() * 100}px`;
    circle.style.top = `${Math.random() * 100}px`;
    circle.style.animationDuration = `${Math.random() * 2 + 1}s`;

    container.appendChild(circle);

    circle.addEventListener('animationend', () => {
      circle.remove();
    });
  }, 500);
}

createAdvancedRipple();

使用Canvas实现高性能光圈

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext('2d');

const ripples = [];

class Ripple {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.radius = 0;
    this.maxRadius = 100 + Math.random() * 50;
    this.speed = 1 + Math.random();
    this.opacity = 1;
    this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
  }

  update() {
    this.radius += this.speed;
    this.opacity = 1 - (this.radius / this.maxRadius);
    return this.radius < this.maxRadius;
  }

  draw() {
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
    ctx.strokeStyle = this.color.replace(')', `, ${this.opacity})`).replace('hsl', 'hsla');
    ctx.lineWidth = 2;
    ctx.stroke();
  }
}

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // 更新和绘制所有光圈
  for (let i = ripples.length - 1; i >= 0; i--) {
    const ripple = ripples[i];
    if (ripple.update()) {
      ripple.draw();
    } else {
      ripples.splice(i, 1);
    }
  }

  requestAnimationFrame(animate);
}

// 点击创建光圈
canvas.addEventListener('click', (e) => {
  ripples.push(new Ripple(e.clientX, e.clientY));
});

animate();

实现光圈跟随鼠标效果

document.addEventListener('mousemove', (e) => {
  const mouseX = e.clientX;
  const mouseY = e.clientY;

  const ripple = document.createElement('div');
  ripple.classList.add('circle');

  ripple.style.left = `${mouseX - 50}px`;
  ripple.style.top = `${mouseY - 50}px`;

  document.body.appendChild(ripple);

  setTimeout(() => {
    ripple.remove();
  }, 2000);
});

这些方法提供了从简单到复杂的光圈效果实现,可以根据具体需求选择合适的方案。CSS方案适合简单场景,Canvas方案则更适合需要高性能和复杂效果的场景。

标签: 光圈效果
分享给朋友:

相关文章

vue实现预览效果

vue实现预览效果

实现预览效果的常见方法 在Vue中实现预览效果可以通过多种方式完成,以下列举几种典型场景和对应的实现方案: 图片/文件预览 使用<input type="file">结合FileRead…

vue效果实现

vue效果实现

Vue 效果实现方法 Vue.js 提供了多种方式实现动态效果,包括过渡动画、状态驱动的 UI 变化以及第三方动画库集成。以下是常见效果的实现方案: 过渡动画 使用 Vue 内置的 <tran…

vue实现旋转效果

vue实现旋转效果

使用CSS动画实现旋转 在Vue中可以通过CSS的@keyframes或transform属性实现旋转效果。在组件的<style>部分定义旋转动画: .rotate { animat…

vue实现选中效果

vue实现选中效果

实现选中效果的方法 在Vue中实现选中效果可以通过多种方式,常见的有使用v-bind:class动态绑定类名、利用v-model与表单元素结合,或通过状态管理控制选中样式。以下是几种典型实现方案:…

vue实现聊天效果

vue实现聊天效果

Vue 实现聊天效果 在 Vue 中实现聊天效果,可以通过组件化设计、数据绑定和事件处理来完成。以下是关键步骤和代码示例。 数据结构和状态管理 使用 Vue 的 data 或 ref(Composi…

vue实现拖拽效果

vue实现拖拽效果

实现拖拽效果的基本方法 在Vue中实现拖拽效果可以通过原生HTML5的拖放API或第三方库如vuedraggable。以下是两种常见方法的详细说明。 使用HTML5拖放API HTML5提供了原生的…