Golfed Min/Max
Math.min(a,b) // 13 chars
a<b?a:b // 7 chars
Math.max(a,b)
a>b?a:b
Another small golfing gem from codegolf stackexchange. This isn’t immediately obvious, but cool to note when golfing.
It’s also worth mentioning that if your code is long enough, aliasing Math.min
and/or Math.max
may be shorter in the long run:
m = Math.min
Math.min(a,b) // 13 chars
a<b?a:b // 7 chars
m(a,b) // 6 chars
Complementary HSL
const a = document.body.appendChild(document.createElement('div')),
b = document.body.appendChild(document.createElement('div'));
let degA = degB = 0;
const size = {
width: '100px',
height: '100px'
};
Object.assign(a.style, size);
Object.assign(b.style, size);
function loop() {
degA += 1;
degB = degA + 180;
a.style.background = `hsl(${degA}deg, 100%, 50%)`;
b.style.background = `hsl(${degB}deg, 100%, 50%)`;
requestAnimationFrame(loop);
}
loop();
In HSL a hue difference of 180 degrees between two values will create a set of complimentary colors.
Wiggly Line Canvas
const canvas = document.body.appendChild(
document.createElement('canvas')
);
const c = canvas.getContext('2d');
document.body.style.margin = 0;
function resize() {
canvas.width = innerWidth;
canvas.height = innerHeight;
}
addEventListener('resize', resize);
resize();
const PAD = 50;
const RAD = 2;
const SPEED = 20;
const TWO_PI = Math.PI * 2;
let mode = 'draw';
let t = Math.random() * TWO_PI,
x = innerWidth / 2,
y = innerHeight / 2,
vx = 0, vy = 0, ta = 0;
function loop() {
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') {
c.fillStyle = 'red';
c.fillRect(x, y, 2, 2);
}
if (x < -PAD) {
x = innerWidth + PAD;
} else if (x > innerWidth + PAD) {
x = -PAD;
}
if (y < -PAD) {
y = innerHeight + PAD;
} else if (y > innerHeight + PAD) {
y = -PAD;
}
}
requestAnimationFrame(loop);
}
loop();
Recently saw this in some very old code – cool trick for moving things in a wiggly way – or in this case, drawing a wiggly line.
Make a Grid
const cellSize = 25;
const cols = 10;
const rows = 20;
function makeDot(x, y) {
const dot = document.body.appendChild(
document.createElement('div')
);
dot.classList.add('cell');
Object.assign(dot.style, {
position: 'absolute',
left: `${x}px`,
top: `${y}px`,
width: `${cellSize}px`,
height: `${cellSize}px`,
outline: '1px solid black',
cursor: 'pointer',
background: 'gray'
});
return dot;
}
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
makeDot(x * cellSize, y * cellSize);
}
}
// make a cell red when it is rolled over
document.addEventListener('mouseover', e => {
if (e.target.classList.contains('cell')) {
e.target.style.background = 'red';
}
});
Here is a simple example for arranging divs in a grid.
toString Hack Obfuscated
x=''+self
j=''
'd1d7a1712345691a7512d427b1da7d9ab7519a4b721a961721d694'
.split``
.map(_=>j+=`x[0x${_}]+`)
console.log(eval(j+'""'))
Yesterday’s snippet saying something else…
It’s simpler than it looks:
x=''+self
// becomes "[object Window]"
j=''
// initialize `j` which will be javascript to pass to `eval`
'd1d7a17123456...'
// this is a list of index values to
// look up in `x` in hexidecimal so that each
// index is a single character
.split``
// split the index values into an array `[0xe, 0x2 ...`
.map(_=>j+=`x[0x${_}]+`)
// map over the index values and write a string like
// this `x[0xe]+x[0x2]+...` into `j`
console.log(eval(j+'""'))
// evaluate `j` with an empty string at the end
// `x[0xe]+x[0x2]+""` and log it out
`