当前位置:首页 > VUE

vue实现点亮灯光

2026-01-08 14:56:34VUE

Vue 实现点亮灯光效果

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

使用 CSS 动画和 Vue 数据绑定

通过 Vue 的数据绑定控制 CSS 类名,实现灯光开关效果。

<template>
  <div>
    <div class="light" :class="{ 'light-on': isLightOn }"></div>
    <button @click="toggleLight">Toggle Light</button>
  </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 10px rgba(255, 255, 0, 0.5);
}
</style>

使用 Canvas 绘制动态灯光效果

对于更复杂的灯光效果,可以使用 Canvas 结合 Vue 实现。

<template>
  <div>
    <canvas ref="lightCanvas" width="200" height="200"></canvas>
    <button @click="toggleLight">Toggle Light</button>
  </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, 10, 100, 100, 50)
        gradient.addColorStop(0, 'yellow')
        gradient.addColorStop(1, 'transparent')

        this.ctx.fillStyle = gradient
        this.ctx.fillRect(0, 0, 200, 200)

        // 绘制光源中心
        this.ctx.fillStyle = 'yellow'
        this.ctx.beginPath()
        this.ctx.arc(100, 100, 20, 0, Math.PI * 2)
        this.ctx.fill()
      }
    }
  }
}
</script>

使用第三方动画库

对于更专业的灯光效果,可以结合 GSAP 或 Anime.js 等动画库。

<template>
  <div>
    <div ref="light" class="light"></div>
    <button @click="toggleLight">Toggle Light</button>
  </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.light, {
          backgroundColor: '#ffff00',
          boxShadow: '0 0 30px 15px rgba(255, 255, 0, 0.7)',
          duration: 0.5
        })
      } else {
        gsap.to(this.$refs.light, {
          backgroundColor: '#cccccc',
          boxShadow: 'none',
          duration: 0.5
        })
      }
    }
  }
}
</script>

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

响应式灯光颜色控制

通过 Vue 的响应式特性,实现灯光颜色和强度的动态控制。

vue实现点亮灯光

<template>
  <div>
    <div 
      class="light" 
      :style="{
        backgroundColor: lightColor,
        boxShadow: `0 0 ${lightIntensity}px ${lightIntensity/2}px rgba(255, 255, 0, 0.5)`
      }"
    ></div>
    <div>
      <label>Color: <input type="color" v-model="lightColor"></label>
      <label>Intensity: <input type="range" v-model="lightIntensity" min="0" max="50"></label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      lightColor: '#ffff00',
      lightIntensity: 20
    }
  }
}
</script>

<style>
.light {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin: 20px;
  transition: all 0.3s ease;
}
</style>

这些方法展示了在 Vue 中实现点亮灯光效果的不同技术路径,从简单的 CSS 过渡到复杂的 Canvas 绘制,开发者可以根据项目需求选择合适的方式。

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

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue实现列表显示

vue实现列表显示

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

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要跳…

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript 图表…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…