Last active
March 5, 2018 04:29
-
-
Save cesarmiquel/a498a8e11716a04fcf2f2f0a27ab364f to your computer and use it in GitHub Desktop.
Dynamically created SVG image. Inspired by https://twitter.com/newsycombinator/status/967806990092242944 and http://blog.excites.co.uk/post/161030046411/rainbow.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<svg onload="init()" xmlns="http://www.w3.org/2000/svg" | |
aria-label="Calendar" role="img" | |
viewBox="0 0 512 512"> | |
<script type="text/ecmascript"><![CDATA[ | |
// | |
// https://stackoverflow.com/questions/5736398/how-to-calculate-the-svg-path-for-an-arc-of-a-circle | |
// | |
function polarToCartesian(centerX, centerY, radius, angleInDegrees) { | |
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0; | |
return { | |
x: centerX + (radius * Math.cos(angleInRadians)), | |
y: centerY + (radius * Math.sin(angleInRadians)) | |
}; | |
} | |
function describeArc(x, y, radius, startAngle, endAngle){ | |
var start = polarToCartesian(x, y, radius, endAngle); | |
var end = polarToCartesian(x, y, radius, startAngle); | |
var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1"; | |
var d = [ | |
"M", start.x, start.y, | |
"A", radius, radius, 0, largeArcFlag, 0, end.x, end.y | |
].join(" "); | |
return d; | |
} | |
function drawArc(x, y, radius, start, end, color) { | |
var arc = document.createElementNS("http://www.w3.org/2000/svg", "path"); | |
arc.setAttribute("fill", "none"); | |
arc.setAttribute("stroke-width", "15"); | |
arc.setAttribute("stroke", color); | |
arc.setAttribute("stroke-linecap", "round"); | |
arc.setAttribute("d", describeArc(x, y, radius, start, end)); | |
document.getElementsByTagName("svg")[0].append(arc); | |
} | |
function drawSpiral(startAngle, x) { | |
var color; | |
var startHue = Math.round(Math.random() * 256) % 255; | |
for(var r = 20; r < 254; r += 20) { | |
color = "hsl(" + Math.round((startHue + r) % 200) + "," + x + "%, 50%)"; | |
startAngle += 20; | |
drawArc(250,250,r,startAngle % 360,(startAngle + 90) % 360,color); | |
} | |
} | |
function init() { | |
// remove warning since JS works | |
var warnings = document.getElementsByTagName("text"); | |
for(var i = 0; i < warnings.length; i++) { | |
warnings[i].setAttribute("font-size", "0"); | |
} | |
// Do the magic | |
var initialAngle = Math.round(Math.random() * 360); | |
drawSpiral(initialAngle, 50); | |
drawSpiral(initialAngle + 90, 70); | |
drawSpiral(initialAngle + 180, 90); | |
drawSpiral(initialAngle + 270, 100); | |
} | |
]]></script> | |
<text x="40" y="30" font-family="Verdana" font-size="20">Need JavaScript support for this to display.</text> | |
<text x="50" y="50" font-family="Verdana" font-size="20">Open file in Chrome / Firefox / Safari.</text> | |
</svg> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To see the image you need to download the SVG file and open it on a browser (Chrome, Firefox or Safari will do). Every time you open it the colors will vary.