DDR爱好者之家 Design By 杰米
本文实例为大家分享了QML实现圆环颜色选择器的具体代码,供大家参考,具体内容如下
话不多说,先上效果图:
ColorSelector组件代码如下,有问题可以留言:
import QtQuick 2.0
import QtQuick.Controls 2.2
Item {
id:baseItem
width: 350
height: width
signal colorChanged(string newColor);
property int circleWidth: 40;//圆环宽度
property var curColor: undefined;
Rectangle {
id: control
width: baseItem.width
height: width
color: "transparent"
border.width: 1
border.color: "black"
anchors.margins: 10
// 根据角度获取颜色值
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;
}
// 获取旋转角度
function getRotateAngle (mouseX, mouseY) {
var yPosOffset = mouseY - control.width/2; // 计算角度 : tan(x) = (y2-y1)/(x2-x1);
var xPosOffset = mouseX - control.height/2;
// 旋转的弧度 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;
}
// 通过鼠标所在点更新Canvas画图信息
function updateCanvasByMousePos(x, y){
var currentAngle = control.getRotateAngle(x, y);
console.log(x, y, currentAngle);
updateCanvasByAngle(currentAngle);
}
//通过角度更新Canvas画图信息位置
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);//发信号
}
// 鼠标选择圆环按钮
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
}
// 圆环画布
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);
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
