当前位置:首页 > VUE

vue实现点亮灯光

2026-01-08 14:56:34VUE

Vue 实现点亮灯光效果

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

使用 CSS 动画和 Vue 数据绑定

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

vue实现点亮灯光

<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 实现。

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 的响应式特性,实现灯光颜色和强度的动态控制。

<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实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install axi…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-ta…

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取…