JSFiddle - React, Tailwind, and code Playground
by DSDmark
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>SketchBoard</title>
<link rel="stylesheet" href="assets/css/style.css" />
</head>
<body>
<!-- <div class="sketchColor">
<input type="text" id="sketchColor" placeholder="Enter a Color Name" />
<button type="button" id="changeColor" onclick="getInputValue()">
Change Color
</button>
</div>
<canvas id="cvs"></canvas> -->
<!-- =================================== -->
<!-- <canvas id="cvs"></canvas> -->
<div class="color-picker">
<div class="container">
<div class="color-spectrum">
<div class="color-handle"></div>
<canvas width="500" height="176"></canvas>
</div>
<div class="hue">
<div class="hue-handle"></div>
<div class="hue-bar"></div>
</div>
<div class="picked-color"></div>
</div>
</div>
<!-- =========================================== -->
<!-- <script defer src="/assets/js/index.js"></script> -->
<script defer src="test.js"></script>
</body>
</html>
CSS
/* html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
font-family: "Roboto", "Lucida Grande", sans-serif;
}
.main {
width: 500px;
height: 100vh;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
}
.container {
-webkit-box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
}
.color-handle,
.hue-handle {
width: 20px;
height: 20px;
border-radius: 10px;
background: red;
border: 2px solid #fff;
cursor: pointer;
}
.color-spectrum {
position: relative;
}
.color-handle {
position: absolute;
top: -10px;
left: -10px;
will-change: transform;
}
.hue {
position: relative;
margin: 0 auto;
padding: 1.5em 0;
width: 80%;
}
.hue-bar {
position: relative;
width: 100%;
height: 8px;
border-radius: 4px;
background: linear-gradient(
to right,
hsl(0, 100%, 50%),
hsl(60, 100%, 50%),
hsl(120, 100%, 50%),
hsl(180, 100%, 50%),
hsl(240, 100%, 50%),
hsl(300, 100%, 50%),
hsl(360, 100%, 50%)
);
z-index: 1;
}
.hue-handle {
position: absolute;
left: -10px;
top: 18px;
z-index: 2;
will-change: transform;
}
.hue-handle.hue-handle--animatable {
transition: transform 0.3s cubic-bezier(0, 0, 0.3, 1);
}
.color-picker canvas {
display: block;
}
.picked-color {
width: 100%;
font-family: 400;
height: 100px;
background: #ccc;
margin-bottom: 1.5em;
display: flex;
align-items: center;
justify-content: space-around;
text-align: center;
}
body {
display: grid;
place-items: center;
min-height: 100vh;
} */
JavaScript
/* const ColorPicker = {
mainFunction() {
this.canvas = document.getElementById("cvs");
this.CH = this.canvas.height = 400;
this.CW = this.canvas.width = 400;
this.canvas.style.background = "black";
this.ctx = this.canvas.getContext("2d");
// Add three color stops
this.gradient = this.ctx.createLinearGradient(0, 0, this.CW, 0);
this.gradient.addColorStop(0.2, "red");
this.gradient.addColorStop(0.5, "black");
this.gradient.addColorStop(1, "white");
this.ctx.fillStyle = this.gradient;
this.ctx.fillRect(0, 0, this.CW, this.CW / 2);
// Set the fill style and draw a rectangle
}
};
ColorPicker.mainFunction();
class ColorPicker{
constructor(){
this.canvas = document.getElementById("cvs");
this.CH = this.canvas.height = 400;
this.CW = this.canvas.width = 400;
this.canvas.style.background = "black";
this.ctx = this.canvas.getContext("2d");
// Add three color stops
this.gradient = this.ctx.createLinearGradient(0, 0, this.CW, 0);
this.gradient.addColorStop(0, `rgb(255, 153, 255)`);
this.gradient.addColorStop(1, `rgb(255, 0, 0)`);
this.ctx.fillStyle = this.gradient;
this.ctx.fillRect(0, 0, this.CW, this.CW / 2);
// Set the fill style and draw a rectangle
}
}
let picker = new ColorPicker();
==========================
class ColorPicker {
constructor() {
this.colorPickerEl = document.querySelector("canvas");
this.hueBarEl = document.querySelector(".hue");
this.hueHandleEl = document.querySelector(".hue-handle");
this.colorHandleEl = document.querySelector(".color-handle");
this.pickedColorEl = document.querySelector(".picked-color");
const colorPickerBCR = this.colorPickerEl.getBoundingClientRect();
const huePickerBCR = this.hueBarEl.getBoundingClientRect();
this.huePickerX = huePickerBCR.left;
this.canvasX = colorPickerBCR.left;
this.canvasY = colorPickerBCR.top;
this.canvasWidth = colorPickerBCR.width;
...