Skip to content

Instantly share code, notes, and snippets.

@dontpaniclabsgists
Created September 5, 2017 13:27
Show Gist options
  • Save dontpaniclabsgists/6b669a61b17f00dba9dd1d1be0786ea3 to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/6b669a61b17f00dba9dd1d1be0786ea3 to your computer and use it in GitHub Desktop.
print_graphic_2
var stars = 600;sk
var sizeVariance = .9; var starSize = 2.5;
var opacityVariance = 1; var w = 2100;
var h = 650;
var g = document.getElementById("group");
for (var i = 0 ; i < stars ; i++){
var x = getRandomNumber(0,w);
var y = getRandomNumber(0,h);
var r = getRandomNumber((starSize - (starSize*sizeVariance)),(starSize + (starSize*sizeVariance)));
var o = getRandomNumber((1 - opacityVariance),1); g.append(makeStar(x,y,r,o));
}
function makeStar(x,y,r,o){
var s = document.createElementNS("http://www.w3.org/2000/svg", "circle"); s.setAttribute("cx",x);
s.setAttribute("cy",y);
s.setAttribute("r",r);
}
function makeStar(x,y,r,o){
var s = document.createElementNS("http://www.w3.org/2000/svg", "circle"); s.setAttribute("cx",x);
s.setAttribute("cy",y);
s.setAttribute("r",r);
s.setAttribute("fill","#FFFFFF"); s.setAttribute("fill-opacity",o); return s;
}
function getRandomNumber(min,max){
var dist = max - min;
return min + (Math.random()*dist);
}
@mattbrailsford
Copy link

mattbrailsford commented Apr 16, 2018

Thanks this came in pretty handy for a similar need I had.

As an FYI, on line one you have some rogue characters on the end "sk", and you also have two makeStar functions when only the second is required so you can get rid of the first one completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment