QML實(shí)現(xiàn)圓環(huán)顏色選擇器
更新時(shí)間:2019年09月25日 09:34:20 作者:lesliefish
這篇文章主要為大家詳細(xì)介紹了QML實(shí)現(xiàn)圓環(huán)顏色選擇器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了QML實(shí)現(xiàn)圓環(huán)顏色選擇器的具體代碼,供大家參考,具體內(nèi)容如下
話不多說(shuō),先上效果圖:

ColorSelector組件代碼如下,有問(wèn)題可以留言:
import QtQuick 2.0
import QtQuick.Controls 2.2
Item {
id:baseItem
width: 350
height: width
signal colorChanged(string newColor);
property int circleWidth: 40;//圓環(huán)寬度
property var curColor: undefined;
Rectangle {
id: control
width: baseItem.width
height: width
color: "transparent"
border.width: 1
border.color: "black"
anchors.margins: 10
// 根據(jù)角度獲取顏色值
function getAngleColor(angle) {
var color, d;
if (angle < Math.PI * 2 / 5) { // angle: 0-72
d = 255 / (Math.PI * 2 / 5) * angle;
color = '255,' + Math.round(d) + ',0'; // color: 255,0,0 - 255,255,0
} else if (angle < Math.PI * 4 / 5) { // angle: 72-144
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 2 / 5);
color = (255 - Math.round(d)) + ',255,0'; // color: 255,255,0 - 0,255,0
} else if (angle < Math.PI * 6 / 5) { // angle: 144-216
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 4 / 5);
color = '0,255,' + Math.round(d); // color: 0,255,0 - 0,255,255
} else if (angle < Math.PI * 8 / 5) { // angle: 216-288
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 6 / 5);
color = '0,'+(255 - Math.round(d)) + ',255'; // color: 0,255,255 - 0,0,255
} else { // angle: 288-360
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 8 / 5);
color = Math.round(d) + ',0,' + (255 - Math.round(d)) ; // color: 0,0,255 - 255,0,0
}
return color;
}
// 獲取旋轉(zhuǎn)角度
function getRotateAngle (mouseX, mouseY) {
var yPosOffset = mouseY - control.width/2; // 計(jì)算角度 : tan(x) = (y2-y1)/(x2-x1);
var xPosOffset = mouseX - control.height/2;
// 旋轉(zhuǎn)的弧度 hudu, 角度angle
var hudu = 0, angle = 0;
if (xPosOffset != 0 && yPosOffset != 0) {
hudu = Math.atan(Math.abs(yPosOffset / xPosOffset));
}
if (xPosOffset === 0 && yPosOffset === 0) {
return angle;
} else if (xPosOffset < 0 && yPosOffset < 0) {
angle = hudu * 180 / Math.PI; // 左上
} else if (xPosOffset === 0 && yPosOffset < 0) {
angle = 90; // 上 中間
} else if (xPosOffset > 0 && yPosOffset < 0) {
angle = 180 - hudu * 180 / Math.PI; // 右上
} else if (xPosOffset > 0 && yPosOffset === 0) {
angle = 180; // 上 下 中間
} else if (xPosOffset > 0 && yPosOffset > 0) {
angle = 180 + hudu * 180 / Math.PI; // 右下
} else if (xPosOffset === 0 && yPosOffset > 0) {
angle = 270; // 下 中間
} else if (xPosOffset < 0 && yPosOffset > 0) {
angle = 360 - hudu * 180 / Math.PI; // 左下
}
return angle;
}
// 通過(guò)鼠標(biāo)所在點(diǎn)更新Canvas畫(huà)圖信息
function updateCanvasByMousePos(x, y){
var currentAngle = control.getRotateAngle(x, y);
console.log(x, y, currentAngle);
updateCanvasByAngle(currentAngle);
}
//通過(guò)角度更新Canvas畫(huà)圖信息位置
function updateCanvasByAngle(angle){
var newX = control.width/2 + - Math.cos(angle*Math.PI/180) * (control.width/2-baseItem.circleWidth/2-2*control.anchors.margins);
var newY = control.height/2 - Math.sin(angle* Math.PI/180) * (control.height/2-baseItem.circleWidth/2-2*control.anchors.margins);
console.log("new : ", newX, newY,"\ncur color is :" + baseItem.curColor);
handle.xDrawPos = newX;
handle.yDrawPos = newY;
handle.requestPaint();
baseItem.curColor='rgb('+control.getAngleColor(((angle+180)%360)/180 * Math.PI)+')';
baseItem.colorChanged(baseItem.curColor);//發(fā)信號(hào)
}
// 鼠標(biāo)選擇圓環(huán)按鈕
Canvas {
id:handle
width : parent.width;
height : width
property int xDrawPos: 0
property int yDrawPos: 0
onPaint: {
var ctx = getContext("2d")
ctx.clearRect(0,0,width,height);
ctx.beginPath();
ctx.arc(xDrawPos, yDrawPos, baseItem.circleWidth/2 + 10, 0, 2 * Math.PI, false);
ctx.fillStyle = 'lightblue';
ctx.fill();
ctx.strokeStyle = 'transparent';
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.arc(xDrawPos, yDrawPos, baseItem.circleWidth/2 - 2, 0, 2 * Math.PI, false);
ctx.fillStyle = baseItem.curColor;
ctx.fill();
ctx.strokeStyle = 'transparent';
ctx.stroke();
ctx.closePath();
}
z:1000
}
// 圓環(huán)畫(huà)布
Canvas {
id: canvas
width: parent.width-4*control.anchors.margins;
height: parent.height
anchors.centerIn: parent
onPaint: {
var ctx = getContext("2d")
var iSectors = 360;
var iSectorAngle = (360/iSectors)/180 * Math.PI; // in radians
ctx.translate(width/2, height/2);
for (var i = 0; i< iSectors; i++) {
var startAngle = 0;
var endAngle = startAngle + iSectorAngle;
var radius = (width/2-1);
var color = control.getAngleColor(iSectorAngle * i);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.arc(0, 0, radius, startAngle, endAngle, false);
ctx.closePath();
ctx.strokeStyle = 'rgb('+color+')';
ctx.stroke();
ctx.fillStyle = 'rgb('+color+')';
ctx.fill();
ctx.rotate(iSectorAngle);
}
ctx.restore();
ctx.save();
ctx.translate(0,0);
ctx.beginPath();
ctx.arc(0, 0, width/2-baseItem.circleWidth, 0, 2 * Math.PI, false);
ctx.fillStyle = 'white';
ctx.fill();
ctx.strokeStyle = 'transparent';
ctx.stroke();
ctx.restore();
}
MouseArea {
id:colorSelectorMouseArea
anchors.fill: parent;
onMouseXChanged: {
control.updateCanvasByMousePos(mouseX, mouseY);
}
}
Component.onCompleted:{
control.updateCanvasByAngle(0);
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)的前端AES加密解密功能【基于CryptoJS】
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的前端AES加密解密功能,涉及javascript基于CryptoJS插件進(jìn)行AES加密解密操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08
微信小程序?qū)崿F(xiàn)頁(yè)面跳轉(zhuǎn)傳值以及獲取值的方法分析
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)頁(yè)面跳轉(zhuǎn)傳值以及獲取值的方法,結(jié)合實(shí)例形式總結(jié)分析了微信小程序頁(yè)面跳轉(zhuǎn)及傳值的常用操作技巧,需要的朋友可以參考下2017-12-12
使用TextRange獲取輸入框中光標(biāo)的位置的代碼
使用TextRange獲取輸入框中光標(biāo)的位置的代碼...2007-03-03
使用javascript實(shí)現(xiàn)監(jiān)控視頻播放并打印日志
這篇文章主要介紹了使用javascript實(shí)現(xiàn)監(jiān)控視頻播放并打印日志的方法及示例代碼分享,非常不錯(cuò),需要的朋友可以參考下2015-01-01

