Skip to content

Instantly share code, notes, and snippets.

@crismanNoble
Forked from mbostock/.block
Created July 16, 2012 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crismanNoble/3125623 to your computer and use it in GitHub Desktop.
Save crismanNoble/3125623 to your computer and use it in GitHub Desktop.
SVG Linear Gradient (D3) Rainbow
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.27.1"></script>
</head>
<body>
<script type="text/javascript">
var w = 400,
h = 200;
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var gradient = svg.append("svg:defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "0%")
.attr("spreadMethod", "pad");
gradient.append("svg:stop")
.attr("offset", "0%")
.attr("stop-color", "rgb(255,0,0)")
.attr("stop-opacity", 1);
gradient.append("svg:stop")
.attr("offset", "100%")
.attr("stop-color", "rgb(255,255,0)")
.attr("stop-opacity", 1);
svg.append("svg:rect")
.attr("width", w)
.attr("height", h)
.style("fill", "url(#gradient)");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment