Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Last active March 5, 2018 04:29
Show Gist options
  • Save cesarmiquel/a498a8e11716a04fcf2f2f0a27ab364f to your computer and use it in GitHub Desktop.
Save cesarmiquel/a498a8e11716a04fcf2f2f0a27ab364f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
<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>
@cesarmiquel
Copy link
Author

cesarmiquel commented Mar 5, 2018

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.

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