当前位置:首页 > VUE

vue实现点亮灯光

2026-02-11 04:42:56VUE

vue实现点亮灯光效果

使用CSS动画和v-bind

通过v-bind动态绑定class实现灯光开关效果。定义一个lightOn的data属性控制灯光状态,点击事件切换该属性值。

vue实现点亮灯光

<template>
  <div>
    <div 
      class="light" 
      :class="{ 'on': lightOn }"
      @click="toggleLight"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      lightOn: false
    }
  },
  methods: {
    toggleLight() {
      this.lightOn = !this.lightOn
    }
  }
}
</script>

<style>
.light {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #ccc;
  transition: all 0.3s ease;
}
.light.on {
  background-color: yellow;
  box-shadow: 0 0 20px yellow;
}
</style>

结合Canvas绘制发光效果

对于更复杂的发光效果,可以使用Canvas绘制。通过requestAnimationFrame实现流畅的动画。

vue实现点亮灯光

<template>
  <canvas ref="lightCanvas" width="200" height="200"></canvas>
</template>

<script>
export default {
  mounted() {
    this.drawLight()
  },
  methods: {
    drawLight() {
      const canvas = this.$refs.lightCanvas
      const ctx = canvas.getContext('2d')

      let radius = 0
      const animate = () => {
        ctx.clearRect(0, 0, canvas.width, canvas.height)

        // 绘制发光效果
        const gradient = ctx.createRadialGradient(
          100, 100, 0,
          100, 100, radius
        )
        gradient.addColorStop(0, 'rgba(255, 255, 0, 1)')
        gradient.addColorStop(1, 'rgba(255, 255, 0, 0)')

        ctx.fillStyle = gradient
        ctx.beginPath()
        ctx.arc(100, 100, radius, 0, Math.PI * 2)
        ctx.fill()

        radius = (radius + 1) % 100
        requestAnimationFrame(animate)
      }
      animate()
    }
  }
}
</script>

使用第三方动画库

安装GSAP等动画库可以实现更专业的灯光效果。通过npm安装后,在组件中使用。

npm install gsap
<template>
  <div ref="lightElement" class="light"></div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  mounted() {
    this.animateLight()
  },
  methods: {
    animateLight() {
      gsap.to(this.$refs.lightElement, {
        duration: 1,
        boxShadow: '0 0 30px yellow',
        backgroundColor: 'yellow',
        repeat: -1,
        yoyo: true
      })
    }
  }
}
</script>

<style>
.light {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #ccc;
}
</style>

SVG实现动态灯光

使用SVG结合Vue的动态绑定,创建可交互的灯光效果。

<template>
  <svg width="200" height="200">
    <circle 
      cx="100" 
      cy="100" 
      :r="radius" 
      :fill="lightColor"
      @click="toggleLight"
    />
    <filter id="glow" x="-50%" y="-50%" width="200%" height="200%">
      <feGaussianBlur stdDeviation="5" result="blur"/>
      <feComposite in="SourceGraphic" in2="blur" operator="over"/>
    </filter>
  </svg>
</template>

<script>
export default {
  data() {
    return {
      radius: 50,
      lightColor: '#ccc'
    }
  },
  methods: {
    toggleLight() {
      this.lightColor = this.lightColor === '#ccc' ? 'yellow' : '#ccc'
      this.radius = this.radius === 50 ? 60 : 50
    }
  }
}
</script>

标签: 灯光vue
分享给朋友:

相关文章

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…