Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active August 14, 2017 08:56
Show Gist options
  • Save ramiroaznar/ce04a80433053d2feeed60b57acbde90 to your computer and use it in GitHub Desktop.
Save ramiroaznar/ce04a80433053d2feeed60b57acbde90 to your computer and use it in GitHub Desktop.
How to style lines as lightsabers in CARTO with CartoCSS
<!DOCTYPE html>
<html>
<head>
<title>How to style lines as lightsabers in CARTO with CartoCSS</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<style type="text/css">
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<script type="text/sql" id="query">
SELECT * FROM world_borders_hd
</script>
<script type="text/cartocss" id="style">
#layer {
line-color: #005CE6;
line-width: 12;
line-opacity: .4;
}
#layer::first {
line-color: #00C5FF;
line-width: 8;
line-opacity: .6;
}
#layer::second {
line-color: #00FFC5;
line-width: 6;
line-opacity: .8;
}
#layer::third {
line-color: white;
line-width: 4;
line-opacity: 1;
}
</script>
<div id="map"></div>
<script>
function main() {
// Create map, get style and query
var map = new L.Map('map', {
zoomControl: true,
center: [40, -1],
zoom: 2
}),
style = $("#style").text(), query = $("#query").text()
// Add basemap
L.tileLayer('http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png', {
attribution: '<a href="http://carto.com">CARTO</a> © 2014',
maxZoom: 16
}).addTo(map);
// Add CARTO layer
cartodb.createLayer(map, {
user_name: 'ramirocartodb',
type: 'cartodb',
sublayers: [{
sql: query,
cartocss: style
}]
}).addTo(map);
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment