Skip to content

Instantly share code, notes, and snippets.

@AdamWhiteHat
Created December 22, 2016 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamWhiteHat/6acc80eaf9c5ddae9c881a929bb33fc0 to your computer and use it in GitHub Desktop.
Save AdamWhiteHat/6acc80eaf9c5ddae9c881a929bb33fc0 to your computer and use it in GitHub Desktop.
Javascript in SVG example (prime circles)
Display the source blob
Display the rendered blob
Raw
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<script type="text/javascript"><![CDATA[
var width=8096;
var height=900;
var svg = document.getElementsByTagName('svg')[0];
svg.setAttribute("width", width);
svg.setAttribute("height", height);
var rect = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
rect.setAttribute("height", height);
rect.setAttribute("width", width);
rect.style.fill = "black";
svg.appendChild(rect);
for (var b = 2; b < width; b += 1)
{
for (var a = 1; a < width; a += b*2)
{
var cir = document.createElementNS("http://www.w3.org/2000/svg", 'circle');
cir.setAttribute("shape-rendering", "geometricPrecision");
cir.setAttribute("stroke-opacity", "0.07");
cir.setAttribute("r", b);
cir.setAttribute("cx", a+b);
cir.setAttribute("cy", height/2);
cir.style.stroke = "red";
cir.style.strokeWidth = "1";
cir.style.fill = "none";
svg.appendChild(cir);
}
}
]]></script>
</svg>
@AdamWhiteHat
Copy link
Author

Inspired by www.sievesofchaos.com, this SVG generates images such as these on the fly:

prime-circles

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