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> -->

效果图

相关推荐
小小小小宇14 分钟前
React 的 DOM diff笔记
前端
小小小小宇21 分钟前
react和vue DOM diff 简单对比
前端
我在北京coding23 分钟前
6套bootstrap后台管理界面源码
前端·bootstrap·html
Carlos_sam27 分钟前
Opnelayers:封装Popup
前端·javascript
前端小白从0开始1 小时前
Vue3项目实现WPS文件预览和内容回填功能
前端·javascript·vue.js·html5·wps·文档回填·文档在线预览
難釋懷2 小时前
Vue解决开发环境 Ajax 跨域问题
前端·vue.js·ajax
特立独行的猫a2 小时前
Nuxt.js 中的路由配置详解
开发语言·前端·javascript·路由·nuxt·nuxtjs
咸虾米2 小时前
在uniCloud云对象中定义dbJQL的便捷方法
前端·javascript
梨子同志2 小时前
JavaScript Proxy 和 Reflect
前端·javascript