uni-app低成本封装一个取色器组件

news/2024/7/24 14:32:30 标签: 生活

在uni-ui中找不到对应的工具

后面想想也是 移动端取色干什么?
没办法 也挂不住特殊需求

因为去应用市场下载 这总东西 又不是很有必要

那么 下面这个组件或许能解决您的烦恼

<template>
	<view class="content">
		<view class="dialog">
			<view id="colorBg" class="colorBg" @touchstart="startTouch" @touchmove="moveIng"
				@touchend="endTouch">
				<view class="roundBuff" :catchtouchmove="true" @c.stop="()=>{}" :style="'transform:rotate(' +degrees +'deg)'"></view>
				<view class="colorPan" :style="'color:'+getColorByDeg(this.degrees)">拖转轮播取色</view>
			</view>
			<view class="flex" style="margin-top: 100rpx;">
				<button class="lee_btn" @click.stop = "close" type="default">取消</button>
				<button class="lee_btn" @click.stop = "readColor" type="default">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pointerShow: true,
				colorPanWidth: 20,
				colorPanRadius: 0,
				pointerBox: {},
				degrees: 0
			}
		},
		mounted() {
			uni.getSystemInfo({
				success: (res) => {
					uni.createSelectorQuery().select('#colorBg').boundingClientRect((rect) => {
						this.pointerBox = rect
					}).exec()
					this.colorPanRadius = res.screenWidth * 0.4
				}
			})
		},
		methods: {
			close(){
                this.$emit('close');
			},
			readColor(){
				let colro = this.getColorByDeg(this.degrees);
				this.$emit('change',colro);
			},
            rbg2Hex(r, g, b) {
                return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
            },
            calculationScheme(deg) {
              deg = 360- deg + 120
		      const r = Math.round(Math.sin((deg * 2 * Math.PI) / 360) * 255)
		      const g = Math.round(Math.sin(((deg + 120) * 2 * Math.PI) / 360) * 255)
		      const b = Math.round(Math.sin(((deg + 240) * 2 * Math.PI) / 360) * 255)
              return this.colorRgbToHex(`rgb(${r},${g},${b})`);
            },
			startTouch(e) {
				const {
					pageX,
					pageY
				} = e.touches[0]
				this.rotatePointer(pageX, pageY)
			},
			endTouch(e) {
				const {
					pageX,
					pageY
				} = e.changedTouches[0]
				this.rotatePointer(pageX, pageY)
			},
			moveIng(e) {
				const {
					pageX,
					pageY
				} = e.touches[0]
				this.rotatePointer(pageX, pageY)
			},
			rotatePointer(pageX = 0, pageY = 0) {
				const {
					pointerBox,
					colorPanWidth
				} = this
				const mouseX = pageX - colorPanWidth
				const mouseY = pageY - colorPanWidth
				var centerY = pointerBox.top + (pointerBox.height / 2) - 0,
					centerX = pointerBox.left + (pointerBox.height / 2) - 0,
					radians = Math.atan2(mouseX - centerX, mouseY - centerY)
				this.degrees = (radians * (180 / Math.PI) * -1) + 180;
			},
		     getColorByDeg(deg) {
		      deg = 360- deg + 120
		      const r = Math.round(Math.sin((deg * 2 * Math.PI) / 360) * 255)
		      const g = Math.round(Math.sin(((deg + 120) * 2 * Math.PI) / 360) * 255)
		      const b = Math.round(Math.sin(((deg + 240) * 2 * Math.PI) / 360) * 255)
		      return `rgb(${r},${g},${b})`
		    },
			colorRgbToHex(rgbStr) {
				const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6}|[0-9a-fA-f]{8}|[0-9a-fA-f]{6}[0-9]{2})$/;
				if (reg.test(rgbStr)) {
					return rgbStr
				} else {
					const rgbArray = rgbStr.replace(/(?:\(|\)|rgba|rgb|RGBA|RGB)*/g, "").split(",");
					let strHex = "#";
					for (let i = 0; i < rgbArray.length; i++) {
					if (i !== 3) {
						if (rgbArray[i] == "0") {
						    strHex += "00"
						} else {
						    let newItem =Number(rgbArray[i]).toString(16)
						if (newItem.length < 2){
							newItem = "0" + newItem
						}
						    strHex += newItem
						}
							} else {
						strHex += rgbArray[i] == "0" ? "" : Number(rgbArray[i]) * 100
					}
					}
					return strHex;
				}
			}
		}
	}
</script>

<style>


	.dialog {
		display: block;
		border-radius: 30rpx;
		background-color: #303030;
		margin: 20rpx;
		padding: 30rpx;
	}

	.flex {
		display: flex;
		justify-content: space-between;
	}

	.colorBg {
		width: 80vw;
		height: 80vw;
		margin: 5vw;
		background: conic-gradient(red,
				yellow,
				lime,
				aqua,
				blue,
				fuchsia,
				red);
		border-radius: 50%;
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.roundBuff {
		width: 55vw;
		height: 55vw;
		-webkit-transform-origin: center 50%;
		transform-origin: center 50%;
		background: #303030;
		border-radius: 50%;
	}
	.roundBuff::before {
      content: "";
      width: 15px;
      height: 15px;
	  background: #303030;
      border: solid #303030;
      border-width: 10px 10px 0 0;
      transform: translate(-50%, -50%) rotate(-45deg);
      position: absolute;
      left: 50%;
      top: 2%;
}

	.lee_btn {
		background: #00000000;
		color: #FFFFFF;
		width: 36%;
		height: 80rpx;
		line-height: 70rpx;
		text-align: center;
		justify-content: center;
		font-size: 30rpx;
		border-radius: 50rpx;
		border: 5rpx #FFFFFF solid;
		font-weight: bold;
		padding: 1px 20px;
	}

	.colorPan {
		position: absolute;
		color: #FFFFFF;
	}
</style>

直接将整个组件复制过去 接口使用
在这里插入图片描述
组件有两个方法
change 当你点击确定时触发 返回 RGB 色码
close 当你点击取消时触发
在这里插入图片描述


http://www.niftyadmin.cn/n/50010.html

相关文章

ReentrantReadWriteLock、StampedLock

ReentrantLock、ReentrantReadWriteLock、StampedLock 读写锁 一个资源可以被多个读线程访问&#xff0c;或者被一个写线程访问&#xff0c;但是不能同时存在读写线程。 小口诀&#xff1a;读写互斥&#xff0c;读读共享 锁的演变 无锁-----> 独占锁----->读写锁---…

YSP的UI界面设计

文章目录一、准备工作二、UI设计1.QPushButton&#xff1a;三、遇到的bug一、准备工作 1.MSVC和MinGW上编译的项目&#xff0c;不能用另一个编译器进行编译 2.若要使用MSVC编译器&#xff0c;需要下载对应版本的VS 见此篇&#xff1a;https://blog.csdn.net/Copperxcx/article…

补天平台安全问题收录情况分享记录

声明 本文是学习2017年上半年 补天平台漏洞收录分析报告. 而整理的学习笔记,分享出来希望更多人受益,如果存在侵权请及时联系我们 补天平台及补天白帽大会介绍 “补天平台”&#xff0c;成立于2013年3月&#xff0c;全称为“补天漏洞响应平台”。平台旨在收集散落在民间的安…

互联网摸鱼日报(2023-02-09)

互联网摸鱼日报&#xff08;2023-02-09&#xff09; InfoQ 热门话题 ChatGPT进军B端&#xff1f;消息称微软将允许企业创建定制版ChatGPT Meta被曝向管理人员下“最后通牒”&#xff1a;要么加大产出&#xff0c;要么离职 2023年前端十大Web发展趋势 2023年构建前端应用时应…

你可能还不知道的 console.log 替代品

通过使用 javascript 对象的破坏能力&#xff0c;您可以这样做&#xff1a;const{ log } console; log("hi"); log("testing");你可以将log函数更改为您想要的任何其他名称&#xff0c;如下所示&#xff1a;const{log: myLog } console; myLog("hi&qu…

基于 NeRF 的 App 上架苹果商店!照片转 3D 只需一部手机,网友们玩疯了

前言 只用一部手机&#xff0c;现实中的 2D 照片就能渲染出 3D 模型&#xff1f; 没错&#xff0c;无需再手动上传电脑或安装激光雷达&#xff0c;苹果手机自带 App 就能生成 3D 模型。 这个名叫 Luma AI 的“NeRF APP”&#xff0c;正式上架 App Store 后爆火&#xff1a; 小…

Web(十五)

JSP: 1. 指令 * 作用&#xff1a;用于配置JSP页面&#xff0c;导入资源文件 * 格式&#xff1a; <% 指令名称 属性名1属性值1 属性名2属性值2 ... %> * 分类&#xff1a; 1. page &#xff1a; 配置JSP页面的 …

json与mat格式转换--python版

前言 本文分享json与mat格式之间相互转换&#xff0c;使用python实现json转为mat&#xff0c;实现mat转为json。 一、json转为mat 思路&#xff1a; xx.mat文件通常是matlab工具产生的&#xff1b;这里可以使用 scipy.io库中的savemat&#xff0c;来保存mat文件。xx.json文件…