Vue3实现一个拾色器功能

javascript 复制代码
​
<template>
  <div class="color">
    <button v-if="hasEyeDrop" @click="nativePick">点击取色</button>
    <input v-else type="color" @input="nativePick" v-model="selectedColor" />
    <p>所选颜色: {{ selectedColor }}</p>
    <p>RGB颜色: {{ rgbColor }}</p>
  </div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';

const hasEyeDrop: boolean = 'EyeDropper' in window;
const selectedColor = ref<string>('#ffffff'); // 使用ref来创建响应式数据
let result: Record<string, any> = {}; // 用于存储选择的颜色信息

const rgbColor = computed(() => {
  const hex = selectedColor.value.replace('#', '');
  const r = parseInt(hex.substring(0, 2), 16);
  const g = parseInt(hex.substring(2, 4), 16);
  const b = parseInt(hex.substring(4, 6), 16);
  return `rgb(${r}, ${g}, ${b})`;
});

async function nativePick(e: Event): Promise<void> {
  const val = e ? (e.target as HTMLInputElement).value : null;
  if (val) {
    selectedColor.value = val; // 更新选中的颜色
    console.log('获得颜色: ' + val);
  } else {
    const eyeDropper = new (window as any).EyeDropper(); // 初始化一个EyeDropper对象
    console.log('按Esc可退出');
    try {
      const colorResult = await eyeDropper.open(); // 开始拾取颜色
      selectedColor.value = colorResult.sRGBHex; // 更新选中的颜色
      result = colorResult; // 存储颜色信息
      console.log('获得颜色: ' + colorResult.sRGBHex);

      // 使用result变量
      console.log(result);
    } catch (e) {
      console.log('用户取消了取色');
    }
  }
}
</script>


<!-- // import { ref } from 'vue';
// const hasEyeDrop: boolean = 'EyeDropper' in window;
// const selectedColor = ref<string>('#ffffff'); // 使用ref来创建响应式数据
// let result: Record<string, any> = {}; // 用于存储选择的颜色信息

// async function nativePick(e: Event): Promise<void> {
//   const val = e ? (e.target as HTMLInputElement).value : null;
//   if (val) {
//     selectedColor.value = val; // 更新选中的颜色
//     console.log('获得颜色: ' + val);
//   } else {
//     const eyeDropper = new (window as any).EyeDropper(); // 初始化一个EyeDropper对象
//     console.log('按Esc可退出');
//     try {
//       const colorResult = await eyeDropper.open(); // 开始拾取颜色
//       selectedColor.value = colorResult.sRGBHex; // 更新选中的颜色
//       result = colorResult; // 存储颜色信息
//       console.log('获得颜色: ' + colorResult.sRGBHex);
      
//       // 使用result变量
//       console.log(result);
//     } catch (e) {
//       console.log('用户取消了取色');
//     }
//   }
// }
<style>
.color {
  width: 100%;
  height: 100%;
  background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
}
</style> -->

效果图

相关推荐
LeeAt几秒前
真的!真的就一句话就能明白this指向问题
前端·javascript
阳火锅2 分钟前
都2025年了,来看看前端如何给刘亦菲加个水印吧!
前端·vue.js·面试
hahala233319 分钟前
ESLint 提交前校验技术方案
前端
夕水41 分钟前
ew-vue-component:Vue 3 动态组件渲染解决方案的使用介绍
前端·vue.js
我麻烦大了44 分钟前
实现一个简单的Vue响应式
前端·vue.js
独立开阀者_FwtCoder1 小时前
你用 Cursor 写公司的代码安全吗?
前端·javascript·github
Cacciatore->1 小时前
React 基本介绍与项目创建
前端·react.js·arcgis
摸鱼仙人~1 小时前
React Ref 指南:原理、实现与实践
前端·javascript·react.js
teeeeeeemo1 小时前
回调函数 vs Promise vs async/await区别
开发语言·前端·javascript·笔记
贵沫末1 小时前
React——基础
前端·react.js·前端框架