当前位置:首页 > 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 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transi…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templ…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…