Conic Gradient CSS
<div></div>
<style>
div {
width: 200px;
height: 200px;
background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
}
</style>
CSS conic gradient. I got this snippet from CSS Tricks article… đ
<div></div>
<style>
div {
width: 200px;
height: 200px;
background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
}
</style>
CSS conic gradient. I got this snippet from CSS Tricks article… đ
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;
}
Click anywhere on the spectrum to add a color… color boxes can be dragged, color swatches can be deleted by clicking on them…
Just something that popped into my head awhile back so figured I’d do a speed-coded prototype. I’d like to revisit this and add more to it.
d = document
b = d.body
b.style.margin = 0
b.style.background = 'black'
r = (v = 1) => Math.random() * v
with(
b.appendChild(
d.createElement`canvas`
).getContext`2d`) {
onresize = () => {
canvas.width = innerWidth
canvas.height = innerHeight
}
onresize()
fillStyle = 'red'
dot = (
x = r(innerWidth),
y = r(innerHeight),
mass = 10, sr = r(8) + 4,
R = sr,
dx, dy,
dist, vx = .2, vy = 0) => {
return () => {
fillStyle = '#005eb0'
R = sr
if (r() < .005) {
vx = r() * 4 - 2
R = sr * 2
fillStyle = 'white'
}
dx = innerWidth / 2 - x
dy = innerHeight / 2 - y
dist = Math.sqrt(dx * dx + dy * dy)
dist *= dist
vx += dx / dist * mass
vy += dy / dist * mass
x += vx
y += vy
beginPath()
arc(x, y, R, 0, 6.29)
fill()
}
}
const dots = []
for (let i = 0; i < 10; i++) dots.push(dot())
loop = () => {
fillStyle = 'rgba(0, 0, 0, 0.2)'
fillRect(0, 0, canvas.width, canvas.height)
dots.map(d => d())
}
setInterval(loop, 16)
}
Some speed coded dots that gravitate to the center of the screen and occasionally change direction.
const canvas = document.body.appendChild(
document.createElement('canvas')
);
const c = canvas.getContext('2d');
document.body.style.margin = 0;
function resize() {
canvas.width = innerWidth * 2;
canvas.height = innerHeight * 2;
canvas.style.width = innerWidth + 'px';
canvas.style.height = innerHeight + 'px';
}
addEventListener('resize', resize);
resize();
const PAD = 50;
const RAD = 2;
const SPEED = 200;
const TWO_PI = Math.PI * 2;
let mode = 'draw';
let t = Math.random() * TWO_PI,
x = canvas.width / 2,
y = canvas.height / 2,
vx = 0, vy = 0, ta = 0;
let solid = false;
let dotMod = 3;
function loop() {
if (Math.random() < .01) solid = !solid;
if (Math.random() < .01) dotMod = [2, 3, 6][Math.floor(Math.random() * 3)]
for (var i = 0; i < SPEED; i++) {
t = Math.sin(ta) * TWO_PI;
vx = RAD * Math.cos(t);
vy = RAD * Math.sin(t);
ta += Math.random() * 0.1 - 0.05;
x += vx;
y += vy;
if (Math.random() < 0.005) {
mode = 'no draw';
} else if (Math.random() < 0.005) {
mode = 'draw';
}
if (mode === 'draw' && (solid || i % dotMod === 0)) {
c.fillStyle = 'black';
c.fillRect(x, y, 2, 2);
}
if (x < -PAD) {
x = canvas.width + PAD;
} else if (x > canvas.width + PAD) {
x = -PAD;
}
if (y < -PAD) {
y = canvas.height + PAD;
} else if (y > canvas.height + PAD) {
y = -PAD;
}
}
requestAnimationFrame(loop);
}
loop();
This is a variation on a post from awhile back. I was posting it over on dev.to and realized I wanted it to look a bit different.
const canvas = document.createElement('canvas'),
c = canvas.getContext('2d');
canvas.width = 500;
canvas.height = 500;
document.body.appendChild(canvas);
c.fillStyle = 'black';
c.fillRect(0, 0, canvas.width, canvas.height);
let a = 0.29, b = 0.22;
function f(x, y) {
if (Math.random() < 0.001) b = Math.random();
return Math.cos((x + Math.sin(x) * 0.01 + Math.cos(x * a)) * b);
}
let x = 1, y = 0;
setInterval(() => {
if (Math.random() < 0.03) {
x = 1;
y = 0;
}
if (Math.random() < 0.001) a = Math.random();
for (let i = 0; i < 1e3; i++) {
x = x + f(y);
y = y + f(x);
c.save();
c.translate(150, 250);
c.scale(0.5, 0.5);
c.fillStyle = 'rgba(255, 255, 255, 0.01)';
c.fillRect(x, y, 5, 5);
c.restore();
}
}, 20);
A single particle moves around and leaves a trail