document.addEventListener('touchmove', e => e.preventDefault(), {
passive: false
});
document.body.innerHTML += `
<style>
* {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
body {
background: #333;
}
.select-box {
border: 1px solid white;
outline: 1px solid black;
}
.swatch {
border: none;
outline: none;
}
</style>
`;
const col = document.body.appendChild(document.createElement('div'));
Object.assign(col.style, {
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '200px',
background:
`linear-gradient(0, black 0%, transparent 50%, transparent 50%, white 100%),
linear-gradient(90deg, #ff0000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff, #ff0000)`
});
const swatches = document.body.appendChild(document.createElement('div'));
Object.assign(swatches.style, {
position: 'absolute',
top: '200px',
left: 0,
width: '100%'
});
function box(x, y, cls = 'select-box', parent = document.body) {
const box = parent.appendChild(document.createElement`div`);
box.classList.add(cls);
Object.assign(box.style, {
position: 'absolute',
left: `${x}%`,
top: `${y}px`,
width: '40px',
height: '40px',
background: 'none',
transform: 'translate(-50%, -50%)',
cursor: 'pointer',
color: 'white'
});
return box;
}
function touch(e) {
let x = e.clientX;
let y = e.clientY;
if (e.touches != null && e.touches.length > 0) {
x = e.touches[0].clientX;
y = e.touches[0].clientY;
}
return { x, y };
}
document.addEventListener('touchstart', onDown);
document.addEventListener('touchmove', onMove);
document.addEventListener('touchend', onUp);
document.addEventListener('mousedown', onDown);
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
let down = false;
let currBox;
let currSwatch;
let swatchHeight = 30;
let id = 0;
function toHSL(x, y) {
let deg = x * 360;
return `hsl(${deg}deg, 100%, ${100 - y / 2}%)`;
}
function onDown(e) {
let { x, y } = touch(e);
down = true;
let hPercent = x / innerWidth;
let color = toHSL(hPercent, y);
if (e.target.classList.contains('swatch')) {
e.target.style.outline = '2px solid red';
e.target.style.zIndex = 999;
down = false;
setTimeout(() => {
if (confirm('Would you like to remove this swatch?')) {
currBox = document.querySelector(
`.select-box[data-id="${e.target.dataset.id}"]`
);
if (currBox != null) {
currBox.parentNode.removeChild(currBox);
e.target.parentNode.removeChild(e.target);
}
} else {
e.target.style.outline = null;
e.target.style.zIndex = null;
}
}, 250);
} else if (e.target.classList.contains('select-box')) {
currBox = e.target;
c = document.querySelector(`.swatch[data-id="${currBox.dataset.id}"]`);
} else {
currBox = box(hPercent * 100, y);
currBox.dataset.id = id++;
currSwatch = box(0, 0, 'swatch', swatches);
currSwatch.dataset.id = currBox.dataset.id;
Object.assign(currSwatch.style, {
width: '100%',
position: 'relative',
height: `${swatchHeight}px`,
transform: 'none',
background: color
});
}
}
function onMove(e) {
if (down) {
let { x, y } = touch(e);
let hPercent = x / innerWidth;
let color = toHSL(hPercent, y);
Object.assign(currBox.style, {
left: `${hPercent * 100}%`,
top: `${y}px`
});
currSwatch.style.background = color;
}
}
function onUp(e) {
down = false;
currBox = null;
}