当前位置:首页 > VUE

vue实现点亮灯光

2026-03-28 14:54:58VUE

Vue 实现点亮灯光效果

在 Vue 中实现点亮灯光效果可以通过多种方式完成,以下是几种常见的方法:

使用 CSS 动画和 Vue 数据绑定

通过 Vue 的数据绑定控制 CSS 类名,实现灯光的点亮和熄灭效果。

vue实现点亮灯光

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

<script>
export default {
  data() {
    return {
      isLightOn: false
    }
  },
  methods: {
    toggleLight() {
      this.isLightOn = !this.isLightOn
    }
  }
}
</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>

使用 SVG 和动态样式

通过 SVG 实现更复杂的灯光效果,利用 Vue 控制 SVG 元素的属性。

vue实现点亮灯光

<template>
  <div>
    <svg width="200" height="200">
      <circle 
        cx="100" 
        cy="100" 
        r="50" 
        :fill="isLightOn ? 'yellow' : 'gray'"
        :filter="isLightOn ? 'url(#glow)' : 'none'"
        @click="toggleLight"
      />
      <defs>
        <filter id="glow" x="-30%" y="-30%" width="160%" height="160%">
          <feGaussianBlur stdDeviation="5" result="blur" />
          <feComposite in="SourceGraphic" in2="blur" operator="over" />
        </filter>
      </defs>
    </svg>
  </div>
</template>

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

使用第三方动画库

结合动画库如 GSAP 或 Anime.js 实现更丰富的灯光动画效果。

<template>
  <div>
    <div ref="lightBulb" class="light-bulb" @click="toggleLight"></div>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      isLightOn: false
    }
  },
  methods: {
    toggleLight() {
      this.isLightOn = !this.isLightOn
      if (this.isLightOn) {
        gsap.to(this.$refs.lightBulb, {
          backgroundColor: 'yellow',
          boxShadow: '0 0 20px yellow',
          duration: 0.5
        })
      } else {
        gsap.to(this.$refs.lightBulb, {
          backgroundColor: '#ccc',
          boxShadow: 'none',
          duration: 0.5
        })
      }
    }
  }
}
</script>

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

结合 Canvas 实现动态灯光

使用 Canvas 绘制更复杂的灯光效果,通过 Vue 控制绘制逻辑。

<template>
  <div>
    <canvas ref="lightCanvas" width="200" height="200" @click="toggleLight"></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLightOn: false,
      ctx: null
    }
  },
  mounted() {
    this.ctx = this.$refs.lightCanvas.getContext('2d')
    this.drawLight()
  },
  methods: {
    toggleLight() {
      this.isLightOn = !this.isLightOn
      this.drawLight()
    },
    drawLight() {
      this.ctx.clearRect(0, 0, 200, 200)

      if (this.isLightOn) {
        // 绘制发光效果
        const gradient = this.ctx.createRadialGradient(100, 100, 0, 100, 100, 50)
        gradient.addColorStop(0, 'yellow')
        gradient.addColorStop(1, 'transparent')

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

        this.ctx.beginPath()
        this.ctx.arc(100, 100, 30, 0, Math.PI * 2)
        this.ctx.fillStyle = 'yellow'
        this.ctx.fill()
      } else {
        // 绘制关闭状态
        this.ctx.beginPath()
        this.ctx.arc(100, 100, 30, 0, Math.PI * 2)
        this.ctx.fillStyle = '#ccc'
        this.ctx.fill()
      }
    }
  }
}
</script>

以上方法可以根据项目需求选择适合的实现方式,从简单的 CSS 动画到复杂的 Canvas 绘制都能实现点亮灯光的效果。

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

相关文章

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在 V…