【发布时间】:2020-10-14 15:20:18
【问题描述】:
基本上大声笑不能在这里添加视频,但是使用默认的 html 颜色选择器,如果鼠标从颜色框边界向下移动并且它再次移动到边界的边缘,您可以移动颜色选择器点,但我的它只是停止当我快速移动鼠标时,移动并且效果不佳。
我正在尝试创建一个自定义颜色选择器,其中您有一个颜色滑块,然后您选择了颜色框,您可以在此处更改它的白度和暗度(如 adobe xdcc 颜色选择器): Adobe xdcc color picker example
[下面的代码在这里创建][2] 您可以拖动小颜色选择器圆圈,它会更改选定的颜色,但是在使用画布时,当鼠标离开我想要更改的画布时,我不必在边缘移动颜色选择器圆圈。
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="app.js" defer></script>
</head>
<body>
<h2>Let's Create a Color Picker</h2>
<div class="container">
<canvas id="color-picker"></canvas>
<div class="info">
<h3>Selected Color</h3>
<div class="selected"></div>
</div>
</div>
</body>
</html>
CSS:
html, body {
width: 100%;
height: 100%;
font-family: Oxygen, sans-serif;
text-align: center; }
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center; }
#color-picker {
border: .5px solid rgba(15, 15, 15, 0.2); }
.info {
width: 12em;
display: flex;
margin-left: 4em;
flex-direction: row;
justify-content: space-between; }
.selected {
width: 50px;
height: 50px;
border-radius: 100%;
border: 2px solid rgba(15, 15, 15, 0.2); }
JS:
class Picker {
constructor(target, width, height) {
this.target = target;
this.width = width;
this.height = height;
this.target.width = width;
this.target.height = height;
// Get context
this.context = this.target.getContext("2d");
// Circle
this.pickerCircle = { x: 10, y: 10, width: 10, height: 10 };
this.listenForEvents();
}
draw() {
}
build() {
let gradient = this.context.createLinearGradient(0, 0, this.width, 0);
//Color Stops
gradient.addColorStop(0, "rgb(255, 0, 0)");
gradient.addColorStop(0.15, "rgb(255, 0, 255)");
gradient.addColorStop(0.33, "rgb(0, 0, 255)");
gradient.addColorStop(0.49, "rgb(0, 255, 255)");
gradient.addColorStop(0.67, "rgb(0, 255, 0)");
gradient.addColorStop(0.84, "rgb(255, 255, 0)");
gradient.addColorStop(1, "rgb(255, 0, 0)");
//Fill it
this.context.fillStyle = gradient;
this.context.fillRect(0, 0, this.width, this.height);
//Apply black and white
gradient = this.context.createLinearGradient(0, 0, 0, this.height);
gradient.addColorStop(0, "rgba(255, 255, 255, 1)");
gradient.addColorStop(0.5, "rgba(255, 255, 255, 0)");
gradient.addColorStop(0.5, "rgba(0, 0, 0, 0)");
gradient.addColorStop(1, "rgba(0, 0, 0, 1)");
this.context.fillStyle = gradient;
this.context.fillRect(0, 0, this.width, this.height);
//Circle
this.context.beginPath();
this.context.arc(this.pickerCircle.x, this.pickerCircle.y, this.pickerCircle.width, 0, Math.PI * 2);
this.context.strokeStyle = "black";
this.context.stroke();
this.context.closePath();
}
listenForEvents() {
let isMouseDown = false;
const onMouseDown = (e) => {
this.build();
let currentX = e.clientX - this.target.offsetLeft;
let currentY = e.clientY - this.target.offsetTop;
this.pickerCircle.x = currentX;
this.pickerCircle.y = currentY;
isMouseDown = true;
}
const onMouseMove = (e) => {
if(isMouseDown) {
this.build();
let currentX = e.clientX - this.target.offsetLeft;
let currentY = e.clientY - this.target.offsetTop;
this.pickerCircle.x = currentX;
this.pickerCircle.y = currentY;
}
}
const onMouseUp = () => {
isMouseDown = false;
}
//Register
this.target.addEventListener("mousedown", onMouseDown);
this.target.addEventListener("mousemove", onMouseMove);
this.target.addEventListener("mousemove", () => this.onChangeCallback(this.getPickedColor()));
document.addEventListener("mouseup", onMouseUp);
}
getPickedColor() {
let imageData = this.context.getImageData(this.pickerCircle.x, this.pickerCircle.y, 1, 1);
return { r: imageData.data[0], g: imageData.data[1], b: imageData.data[2] };
}
onChange(callback) {
this.onChangeCallback = callback;
}
}
let picker = new Picker(document.getElementById("color-picker"), 250, 220); picker.build();
picker.onChange((color) => {
let selected = document.getElementsByClassName("selected")[0];
selected.style.backgroundColor = `rgb(${color.r}, ${color.g}, ${color.b})`; });
[1]: https://i.stack.imgur.com/MQsjs.png
[2]: https://i.stack.imgur.com/krEYa.png
提前致谢!
【问题讨论】:
-
嘿,欢迎来到 StackOverflow。请阅读stackoverflow.com/help/minimal-reproducible-example 和stackoverflow.com/help/how-to-ask :)。
标签: javascript html css