for(u of document.querySelectorAll('.hnuser'))for(u.prepend(c=document.createElement('canvas')),x=c.getContext('2d'),c.width=18,c.height=14,s=u.innerText,r=1,i=28+s.length;i--;i>>29>X*X/3+Y/2&&x.fillRect(6+2*X,2*Y,2,2)&x.fillRect(6-2*X,2*Y,2,2):r+=s.charCodeAt(i-28,x.fillStyle='#'+(r>>8&0xFFFFFF).toString(16)))r^=r>>17,r^=r>2Show HN: HN Avatars in 357 bytes
1–10 of 536 posts
Re: Show HN: HN Avatars in 357 bytes
#2The concept is to use HN usernames as the seed into a deterministic avatar generator. This generator is built from the famously simple xorshift32 PRNG, which both provides a random variable for the image generator steps, and "pseudo-hashes" the seed string to provide the initial PRNG state using a non-linear step (adding each codepoint - which is likely not very robust against collisions compared to proper hashing algorithms, but is simple and good enough).
The image generation part is a probability distribution with mirrored pixels... specifically: r>>>29 > X*X/3+Y/2 where the left side is 3 of the upper bits of the PRNG state (providing a random integer 0-7), and the right side is the squared distance from the centre for X + the linear distance from the top for Y. i.e the further from the top centre the pixel is, the less likely it will be filled, but linearly for Y and squared for X.
Un-golfed version:
for (const u of document.querySelectorAll('.hnuser')) {
const p=2;
const c=document.createElement('canvas');
const x=c.getContext('2d');
c.width=p*7, c.height=p*7;
u.parentElement.prepend(c);
for (let s=u.innerText, r=1, i=28+s.length; i--;) {
// xorshift32
r^=r>>17, r^=r>2;
if (i >= 28) {
// seed state
r+=s.charCodeAt(i-28);
x.fillStyle='#'+(r>>8&0xFFFFFF)
.toString(16).padStart(0, 6);
} else {
// draw pixel
if (r>>>29 > X*X/3+Y/2)
x.fillRect(p*3+p*X, p*Y, p, p),
x.fillRect(p*3-p*X, p*Y, p, p);
}
}
}
Was fun to play with, but also surprisingly helpful in following discussions.It's worth mentioning these were heavily inspired by three particular dweets by @KilledByAPixel and @FireFly:
[1] https://www.dwitter.net/d/3078
[2] https://www.dwitter.net/d/19786
Re: Show HN: HN Avatars in 357 bytes
#3This was inspired by @frncsdrk's submission from earlier today [0]. The generative concepts used were inspired by and derived from dweets from @KilledByAPixel and @FireFly [1] [2] [3] The concept is to use HN usernames as the seed into a deterministic avatar generator. This generator is built from the famously simple xorshift32 PRNG, which both provides a random variable for the image generator steps, and "pseudo-has…
Re: Show HN: HN Avatars in 357 bytes
#4This was inspired by @frncsdrk's submission from earlier today [0]. The generative concepts used were inspired by and derived from dweets from @KilledByAPixel and @FireFly [1] [2] [3] The concept is to use HN usernames as the seed into a deterministic avatar generator. This generator is built from the famously simple xorshift32 PRNG, which both provides a random variable for the image generator steps, and "pseudo-has…
I've been following Frank Force (KilledByAPixel) for quite some time now, he's on the forefront of generative design via HTML canvas.
Re: Show HN: HN Avatars in 357 bytes
#5Re: Show HN: HN Avatars in 357 bytes
#6Re: Show HN: HN Avatars in 357 bytes
#7It would be neat if the code also applied to the username in the top right so I didn't have to make a spammy comment to see what it gives me. ;)
Re: Show HN: HN Avatars in 357 bytes
#8This was inspired by @frncsdrk's submission from earlier today [0]. The generative concepts used were inspired by and derived from dweets from @KilledByAPixel and @FireFly [1] [2] [3] The concept is to use HN usernames as the seed into a deterministic avatar generator. This generator is built from the famously simple xorshift32 PRNG, which both provides a random variable for the image generator steps, and "pseudo-has…
To anyone interested, I've added links to how the un-golfed version looks with Dark Reader enabled[1] and how it looks without Dark Reader[2]. To clarify, I've set my zoom level for Hacker News to 150% as I find the default to not be conducive to reading.
---
Re: Show HN: HN Avatars in 357 bytes
#9It would be neat if the code also applied to the username in the top right so I didn't have to make a spammy comment to see what it gives me. ;)
Just F12 and manually edit some username, after that re-paste the code and you will see the avatar.
Re: Show HN: HN Avatars in 357 bytes
#10This was inspired by @frncsdrk's submission from earlier today [0]. The generative concepts used were inspired by and derived from dweets from @KilledByAPixel and @FireFly [1] [2] [3] The concept is to use HN usernames as the seed into a deterministic avatar generator. This generator is built from the famously simple xorshift32 PRNG, which both provides a random variable for the image generator steps, and "pseudo-has…
Dwitter.net is a challenge to see what awesomeness you can create when limited to only 140 characters of javascript and a canvas. Give it a go!