Skip to content

Instantly share code, notes, and snippets.

@mapsense-examples
Last active August 29, 2015 14:26
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 mapsense-examples/ed64ba382d6bc838bea6 to your computer and use it in GitHub Desktop.
Save mapsense-examples/ed64ba382d6bc838bea6 to your computer and use it in GitHub Desktop.
image fills
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js" charset="utf-8"></script>
<script src="https://developer.mapsense.co/mapsense.js" charset="utf-8"></script>
<link type="text/css" href="https://developer.mapsense.co/mapsense.css" rel="stylesheet"/>
<style>
html,
body,
#myMap {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.map {
background-color: white;
width: 100%;
height: 100%;
}
path {
vector-effect: non-scaling-stroke;
stroke: white;
fill: none;
}
.flaggable {
vector-effect: non-scaling-stroke;
stroke: white;
fill: #d3d4d5;
}
.painted {
vector-effect: non-scaling-stroke;
stroke: #777;
stroke-width: 1px;
fill: none;
}
.tile-background {
fill: white;
}
</style>
</head>
<body>
<div id="myMap"></div>
<script>
var G = {};
G.home = [ // we'll set the map extent to these bounds
{lon: -125, lat: -60},
{lon: 160, lat: 75}
];
var map = mapsense.map("#myMap");
map.extent(G.home);
map.zoom(4);
map.center({'lat': 36.2, 'lon': -95});
map.add(mapsense.hash());
var layer;
var images = {}/*
for (var i = 0; i < 600; i++){
images[i] = new Image();
images[i].src = moviePath + (i + 1) + '.jpg.t';
images[i].onload = function(){
var i = this.src.split(movieName + '/')[1].split(".")[0];
d3.select("#bar" + i).style("stroke", 'rgb(' + colors[i].rgb + ')');
}
}*/
//function paint(obj,id,attr) {
function paintAll() {
d3.selectAll('.flaggable').each(function(g){
//d3.selectAll('.flaggable').on("mouseover", function(g){
// this = path
// g = geojson data
console.log(this,d);
var d = d3.select(this);
var id = 'postal',
attr = 'mammal';
/*d = d3.select(this)
.attr('class','ms');*/
//console.log(g);
if (d && d.node() && d.node().parentNode && g && g.properties[id]) {
console.log(g);
var p = d.node().parentNode;
var a_id = 'hec_' + g.properties[id];
// FIX
var iso = g.properties[id];
var bbox = d.node().getBBox();
var w = bbox.width/2;
var h = bbox.height/2;
final_x = bbox.x;
final_y = bbox.y;
var geom_d = d3.select('#hec_' + g.properties[id]).attr('d');
d3.select(d.node().parentNode).append("clipPath")
//d3.select(d.node().parentNode).append("g")
.attr("class", "mask")
.attr("id", "mask_" + a_id)
.append('path')
//.attr('d',d.attr('d'))
.attr('d',geom_d)
;
var img = d3.select(d.node().parentNode)
//.attr('transform',"translate(" + (final_x + (bbox.width)/2) + "," + (final_y + (bbox.height)/2) + ") rotate(90," + (bbox.width)/2 + "," + (bbox.height)/2 + ")")
//.attr('transform'," rotate(90," + (bbox.width)/2 + "," + (bbox.height)/2 + ")")
//var ratio = bbox.width / bbox.height;
var search_term = g.properties[attr]
search_term = search_term.replace(/ /g,',');
img_url = 'http://loremflickr.com/' + parseInt(w) + '/' + parseInt(h) + '/' + search_term + '/all';
console.log(img_url);
d3.select(d.node().parentNode).append("g")
// position it where the country is
.attr("clip-path", function(d) {
return "url(#mask_" + a_id + ")";
})
.attr("x", final_x)
.attr("y", final_y)
.attr("width", w)
.attr("height", h)
.append("image")
.attr("xlink:href", img_url)
.attr("preserveAspectRatio", "none")
.attr("x", final_x)
.attr("y", final_y)
.attr("width", w)
.attr("height", h)
d3.select(d.node().parentNode).append("g")
//.attr("paint-order","fill")
.append('path')
.attr('d',geom_d)
//.attr("class", "painted")
;
}
});
}
d3.json("ne50us_states.geojson", function(data) {
layer = mapsense.geoJson()
.features(data.features)
.selection(function(d) {
var id = 'postal';
var attr = 'mammal';
//d.attr("class", "gj");
d.attr("class", function(g) {
var iso = g.properties[id];
/*images[iso] = new Image();
images[iso].src = 'https://cdn.rawgit.com/stevenrskelton/flag-icon/master/svg/country-squared/' + iso.toLowerCase() + '.svg';
images[iso+'4x3'] = new Image();
images[iso+'4x3'].src = 'https://cdn.rawgit.com/stevenrskelton/flag-icon/master/svg/country-4x3/' + iso.toLowerCase() + '.svg';*/
return 'flaggable ' + iso;
});
d.attr("paint-order","fill");
d.attr("id", function(g) {
if (g.properties[id]) {
return 'hec_' + g.properties[id];
}
});
/*d.each(function(dd){
console.log(d);
paint(d,id,attr);
});*/
paintAll();
//resetClipper();
})
map.add(layer);
});
var layer2;
var svg;
var one, one_path, bounds, bbox;
var final_x, final_y, ex, why;
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment