Exponential Notation JavaScript
console.log(1e1); // 10
console.log(1e2); // 100
console.log(1e3); // 1000
console.log(1e4); // 10000
console.log(12e3); // 12000
This can be helpful when you need big numbers like 100000
.
console.log(1e1); // 10
console.log(1e2); // 100
console.log(1e3); // 1000
console.log(1e4); // 10000
console.log(12e3); // 12000
This can be helpful when you need big numbers like 100000
.
function rect(x1, y1, x2, y2, col, blur = 0.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 blends = 'multiply,screen,overlay,darken,lighten,color-dodge,color-burn,hard-light,soft-light,difference,exclusion,hue'.split(
','
);
const cols = ['black', 'white'];
function gen() {
[...document.querySelectorAll('div')].forEach(d =>
d.parentNode.removeChild(d)
);
for (let j = 0; j < 4; j++) {
const rects = [];
for (let i = 0; i < 10; i++) {
const x = Math.random() * 100;
const y = Math.random() * 100;
const s = Math.random() * Math.random() * Math.random() * 30 + 3;
const col = cols[~~(Math.random() * cols.length)];
rects.push(rect(x, y, x + s, y + s, col, Math.random() * 10));
}
const el = document.body.appendChild(document.createElement('div'));
el.style.backgroundImage = rects.join(', ');
el.style.mixBlendMode = blends[~~(Math.random() * blends.length)];
}
}
gen();
document.body.innerHTML += `
<button id="regen">Regenerate</button>
<style>
body, html {
height: 100%;
background: #ccc;
margin: 0;
background-repeat: no-repeat;
width: 100%;
}
div {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
}
#regen {
position: absolute;
width: 100px;
height: 30px;
background: #ccc;
border: 1px solid white;
margin: 1em;
cursor: pointer;
transition: all 250ms ease-out;
z-index: 999;
}
#regen:hover { background: #333; color: white; }
</style>
`;
regen.onclick = gen;
Playing around some more with lots of linear gradients.
conicBtn.onclick = () => {
document.body.innerHTML += `
<style>
div {
background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
}
</style>
`
}
get ready….
When I created the gradient conic snippet from a few days ago – I accidentally applied the rule to the entire page…. thought it was funny enough to be worth making a snippet out of đ
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…
function rect(x1, y1, x2, y2, col) {
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 +
0.1}%)`;
}
const NUM = 90;
const MAX_SIZE = 20;
const rects = [];
const colors = ['rgba(0, 0, 0, 0.2)', 'white', 'black'];
for (let i = 0; i < NUM; i++) {
const x1 = Math.random() * 100; // %
const y1 = Math.random() * 100;
const size = Math.random() * Math.random() * MAX_SIZE;
const idx = Math.random() < 0.3 ? 1 + Math.round(Math.random()) : 0;
col = colors[idx];
rects.push(rect(x1, y1, x1 + size, y1 + size, col));
}
document.body.style.backgroundImage = rects.join(', ');
document.body.innerHTML += `
<style>
body, html {
height: 100%;
background-repeat: no-repeat;
}
</style>
`;
Many circles with no divs or canvas, just radial-gradients…