What is Clicked?
document.addEventListener('click', e => console.log(e.target))
Try that one in your console and then click on stuff. I use that frequently when debugging…
document.addEventListener('click', e => console.log(e.target))
Try that one in your console and then click on stuff. I use that frequently when debugging…
// "Being clever is not clever"
// -- Bjarne Stroustrup
D = document
ang = {
'|': 180,
'-': 90,
'\\': 135,
'/': 225
}
box = def => {
def = def.split(/\s+/)
form = def.length
I = i => parseInt(def[i], 16)
;[,,, _=>{x = y = I(0); w = h = I(1); c = def[2]},
_=>{x = I(0), y = I(1); w = h = I(2);c = def[3]},
_=>{x = I(0); y = I(1); w = I(2); h = I(3); c = def[4]}
][form]()
c = c.split``
ca = c[0]
ca = ca+ca+ca
cD = ang[c[1]]
cb = c[2]
cb = cb+cb+cb
D.body.appendChild(
D.createElement`div`
).style = `
position: absolute; left: ${x}px; top: ${y}px;
width: ${w}px; height: ${h}px;
background: linear-gradient(${cD}deg, #${ca}, #${cb})
`
}
const parse = prog => prog.trim()
.split(/\n+/m)
.map(line => box(line.trim()))
parse(`
0 64 0/f
64 64 0\\f
a0 f0 30 54 f\\0
0 6f 20 60 0-c
f 7f 20 60 0|c
1f 8f 30 30 c/0
`)
Just playing around… odd micro-gradient notation:
'0 64 0/f'
// x=0 y=0 width=0x64 height=0x64
// 0/f = gradient black to white top right to bottom left
'64 64 0\\f'
// x=0 y=0 width=0x64 height=0x64
// 0\\f = black to to white gradient top left to bottom right
'0 6f 20 60 0-c'
// x=0 y=0x6f width=0x20 height=0x60
// 0-c = gradient black to grey (#ccc) left to right
// etc... ( | ) is top to bottom grad
<pre id=p style=background:#000><svg onload='setInterval(f=n=>
{for(t++,o=i=1;i++<476;o+=i%30?([(f+"")[i%195],"o"][c=0|(h=v=>
(M=Math).hypot(i/30-8+3*M.sin(t/8/v),i%30/2-7+4*M.cos(t/9/v)))
(7)*h(9)*h(6)/52]||".").fontcolor(c?c>2:n):"\n");p.innerHTML=o},t=1)'>
Great golfed snippet from Martin Kleppe – can’t wait to fork it… 😀
This was updated a bit later to be even smaller:
<body onload='setInterval(f=n=>{for(t++,o=i=1;i++<476;o+=i%30?([(f+f)[i],"o"][c=0|(h=v=>(M=Math).hypot(i/30-7+3*M.sin(t/8/v),i%30/2-7+4*M.cos(t/9/v)))(7)*h(9)*h(6)/52]||".").fontcolor(c?c>2:n):"\n");p.innerHTML=o},t=1)'bgcolor=X><pre id=p>
myEl.parentNode.appendChild(myEl);
This will make an element the top most of all of its siblings…
document.body.innerHTML += `
<div>
<button>one</button>
<button>two</button>
<button>three</button>
<button>four</button>
</div>
<style>
button { display: block; cursor: pointer; }
</style>
`;
document.addEventListener('click', e => {
if (e.target.tagName === 'BUTTON') {
e.target.parentNode.appendChild(e.target);
}
});
Clicking on any button will bring it to the top most position within its parent.
function rect(x1, y1, x2, y2, col, blur=.1) {
const dx = x1 - x2;
const dy = y1 - y2;
const dist = Math.sqrt(dx * dx + dy * dy);
return `radial-gradient(circle at ${x1}% ${y1}%, ${col} 0, ${col} ${dist}%, transparent ${dist +
blur}%)`;
}
const NUM = 8;
let rects = [];
const colors = ['rgba(0, 0, 0, 0.2)', 'white', 'black'];
let x = 0;
let y = 0;
let t = 8;
function loop() {
rects = [];
for (let i = 0; i < NUM; i++) {
x = 50 + 30 * Math.sin(t + i / 2);
y = 50 + 30 * Math.cos(t * 1.5 * i / 10);
rects.push(rect(x, y, x + 5, y + 5, 'rgba(255, 255, 255, 1)', 1));
rects.push(rect(x, y, x + 5, y + 5, 'rgba(0, 0, 0, 0.4)',
8 + 6 * Math.cos(y / 10)));
}
t += .04;
document.body.style.backgroundImage = rects.join(', ');
window.requestAnimationFrame(loop);
}
loop()
document.body.innerHTML += `
<style>
body, html {
height: 100%;
background: #ccc;
margin: 0;
background-repeat: no-repeat;
width: 100%;
}
</style>
`;
Animated variation on yesterdays post – many animating circles with no divs or canvas, just radial-gradients…