Skip to content

Instantly share code, notes, and snippets.

@JonathanZWhite
Last active February 13, 2018 21:17
Show Gist options
  • Save JonathanZWhite/85a44d1ad1fa02fb0435c2542e444ec0 to your computer and use it in GitHub Desktop.
Save JonathanZWhite/85a44d1ad1fa02fb0435c2542e444ec0 to your computer and use it in GitHub Desktop.
function dots(width, height, density) {
for (let i = 0; i < density; i += 1) {
createDot(
Math.floor(Math.random() * width),
Math.floor(Math.random() * height),
);
}
}
function createDot(x, y) {
const elem = document.createElement('div');
const colors = ['#BBC8FA', '#BBE6FA', '#FADCBB'];
const color = colors[Math.floor(Math.random() * colors.length)];
const dotSize = Math.floor(Math.random() * 6) + 4;
const styles = [
`left: ${x}px;`,
`top: ${y}px;`,
`background: ${color};`,
`height: ${dotSize};`,
`width: ${dotSize};`,
];
elem.setAttribute('class', 'dot');
elem.setAttribute('style', styles.join(''));
document.getElementsByTagName('body')[0].appendChild(elem);
return elem;
}
export default dots;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment