当前位置:首页 > 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.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> &l…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…

vue滑块实现

vue滑块实现

Vue滑块实现方法 使用原生HTML5 range input Vue中可以绑定原生HTML5的range类型input元素实现基础滑块功能: <template> <div&…