Skip to content

Instantly share code, notes, and snippets.

@roobie
Created May 22, 2014 12:23
Show Gist options
  • Save roobie/6b70fe47d6045ef0a622 to your computer and use it in GitHub Desktop.
Save roobie/6b70fe47d6045ef0a622 to your computer and use it in GitHub Desktop.
Random color throbber
/******************************************************************************
ORIGINAL:
http://upload.wikimedia.org/wikipedia/commons/3/30/Chromiumthrobber.svg
-------------------------------------------------------------------------------
<svg width="16" height="16" viewBox="0 0 300 300"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<path d="M 150,0
a 150,150 0 0,1 106.066,256.066
l -35.355,-35.355
a -100,-100 0 0,0 -70.711,-170.711 z"
fill="#14C964">
<animateTransform attributeName="transform" attributeType="XML"
type="rotate" from="0 150 150" to="360 150 150"
begin="0s" dur="1s" fill="freeze" repeatCount="indefinite" />
</path>
</svg>
-------------------------------------------------------------------------------
******************************************************************************/
var getThrobber = function (color, secondsPerRevolution) {
var throbberSvg = '' +
'<svg width="16" height="16" viewBox="0 0 300 300"' +
'xmlns="http://www.w3.org/2000/svg" version="1.1">' +
'<path d="M 150,0' +
'a 150,150 0 0,1 106.066,256.066' +
'l -35.355,-35.355' +
'a -100,-100 0 0,0 -70.711,-170.711 z"' +
'fill="%color">' +
'<animateTransform attributeName="transform" attributeType="XML"' +
'type="rotate" from="0 150 150" to="360 150 150"' +
'begin="0s" dur="%secondsPerRevolution" fill="freeze" repeatCount="indefinite" />' +
'</path>' +
'</svg>';
return throbberSvg
.replace(/%color/, "#" + (color || "000000"))
.replace(/%secondsPerRevolution/, (secondsPerRevolution || 1) + "s");
};
var randomByte = function () {
return Math.floor(Math.random() * 0xff);
};
var randomColor = function () {
var triplet = [randomByte(), randomByte(), randomByte()];
return triplet.map(function (b) {
var s = b.toString(16);
return s.length < 2 ?
"0" + s : s;
}).join("");
};
var getRandomThrobber = function () {
return getThrobber(randomColor());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment