also see http://bl.ocks.org/dwtkns/7319558
Created
November 10, 2013 23:47
Patterns & animation test 2
This file contains 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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.patterned-stripey { fill: url(#stripey); } | |
.state-filled-with-a-tiny-pattern-of-texas { fill: url(#tiny-map-of-texas); } | |
#stripey rect { stroke: none; fill: #eee; } | |
#stripey path { stroke: #ddd; } | |
#tiny-map-of-texas rect { stroke: none; fill: none; } | |
#tiny-map-of-texas path { fill: #f2f; opacity:.2; } | |
.state-boundary { | |
fill: none; | |
stroke: #f2f; | |
stroke-width:2; | |
stroke-linejoin:round; | |
opacity:.5; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var patternSize = 20; | |
var projection = d3.geo.albersUsa() | |
.scale(1000) | |
.translate([width / 2, height / 2]); | |
var tinyprojection = d3.geo.albersUsa().scale(150).translate([patternSize,0]) | |
var tinypath = d3.geo.path().projection(tinyprojection) | |
var path = d3.geo.path().projection(projection); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
var map = svg.append('g'), | |
defs = svg.append('defs'), | |
stripey = newPattern('stripey',patternSize,defs), | |
tinytexas = newPattern('tiny-map-of-texas',patternSize*2,defs); | |
stripey.append('rect').attr({ | |
'x': -patternSize, | |
'y': -patternSize, | |
'height': patternSize*3, | |
'width': patternSize*3 | |
}) | |
stripey.append('path').attr({ | |
'd': 'M-10,10 l30,-30 M0,'+patternSize+' l'+patternSize+',-'+patternSize+' M'+(patternSize-10)+','+(patternSize+10)+' l30,-30' | |
}) | |
d3.json("/mbostock/raw/4090846/us.json", function(error, us) { | |
var texas = topojson.feature(us, us.objects.states).features.filter(function(f){return f.id === 48})[0] | |
tinytexas.append('path').datum(texas).attr('id','texas').attr('d',tinypath) | |
map.selectAll('path') | |
.data(topojson.feature(us, us.objects.states).features) | |
.enter().append("path") | |
.attr('id',function(d) { return d.id}) | |
.attr("class", 'patterned-stripey') | |
.attr("d", path); | |
map.selectAll('.state-filled-with-a-tiny-pattern-of-texas') | |
.data(topojson.feature(us, us.objects.states).features) | |
.enter().append("path") | |
.attr('id',function(d) { return d.id}) | |
.attr("class", 'state-filled-with-a-tiny-pattern-of-texas') | |
.attr("d", path); | |
map.append("path") | |
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) | |
.attr("class", "state-boundary") | |
.style('stroke-dasharray','0,1000') | |
.attr("d", path) | |
d3.select('#stripey path').call(pulse,1000) | |
d3.select('#stripey').call(patterndrift,5000,100,0) | |
d3.select('#texas').call(spin,2600,45) | |
d3.select('#tiny-map-of-texas').call(patternspin,3000,5) | |
d3.select('.state-boundary').call(trace,2000) | |
}); | |
function newPattern(name,size,defs) { | |
return defs.append('pattern').attr({ | |
'id': name, | |
'patternUnits':'userSpaceOnUse', | |
'patternTransform':'rotate(0,0,0)', | |
'width': size, | |
'height':size | |
}).append('g'); | |
} | |
function pulse(path,duration){ | |
path.transition() | |
.duration(duration) | |
.attr('stroke-width',12) | |
.transition() | |
.attr('stroke-width',2) | |
setTimeout(function() { pulse(path, duration); }, duration*2); | |
} | |
function spin(path,duration,rotation) { | |
path.transition().duration(duration) | |
.attr('transform','rotate('+rotation+',20,20)') | |
setTimeout(function() { spin(path, duration, -rotation); }, duration); | |
} | |
function trace(path,duration){ | |
path.transition() | |
.duration(duration) | |
.style('stroke-dasharray','0,1000') | |
.transition() | |
.style('stroke-dasharray','300,1000') | |
setTimeout(function() { trace(path, duration); }, duration*2); | |
} | |
function patternspin(pattern,duration,rotation) { | |
pattern.transition().duration(duration) | |
.attr('patternTransform','rotate('+rotation+','+width/2+','+height/2+')') | |
setTimeout(function() { patternspin(pattern, duration, -rotation); }, duration); | |
} | |
function patterndrift(pattern,duration,driftx,drifty) { | |
pattern.transition().duration(duration) | |
.attr('patternTransform','translate('+driftx+','+drifty+')') | |
setTimeout(function() { patterndrift(pattern, duration, -driftx,-drifty); }, duration); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment