Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "offset" support to CSS styles #117

Merged
merged 3 commits into from
Nov 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ turbo.js: \
libs/jquery/jquery-1.11.1.js \
libs/lodash/lodash-2.4.1.js \
libs/leaflet/leaflet-src.js \
libs/polylineOffset/leaflet.polylineoffset.js \
libs/jqueryui/jquery-ui.js \
libs/CodeMirror/lib/codemirror.js \
libs/CodeMirror/mode/javascript/javascript.js \
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<link rel="stylesheet" href="libs/locationfilter/src/locationfilter.css"/>
<script src="libs/locationfilter/src/locationfilter.js"></script>
<script src="libs/mapbbcode/PopupIcon.js"></script>
<script src="libs/polylineOffset/leaflet.polylineoffset.js"></script>
<script src="js/GeoJsonNoVanish.js"></script>
<script src="js/OSM4Leaflet.js"></script>
<!--misc libraries-->
Expand Down Expand Up @@ -190,7 +191,7 @@ <h4 class="t" data-t="help.attr.software"></h4>
<ul class="plain">
<li><span class="t" data-t="help.attr.leaflet"></span> <a href="http://leaflet.cloudmade.com">Leaflet</a></li>
<li><span class="t" data-t="help.attr.codemirror"></span> <a href="http://codemirror.net/">CodeMirror</a></li>
<li><span class="t" data-t="help.attr.other_libs"></span> <a href="https://github.com/tyrasd/osmtogeojson">osmtogeojson</a>, <a href="https://github.com/tyrasd/togpx">togpx</a>, <a href="https://github.com/mapbox/tokml">tokml</a>, <a href="http://lodash.com">lodash</a>, <a href="http://jquery.com/">jQuery</a>, <a href="http://jqueryui.com/">jQuery UI</a>, <a href="http://html2canvas.hertzen.com/">html2canvas</a>, <a href="http://code.google.com/p/canvg/">canvg</a>, <a href="https://github.com/kajic/leaflet-locationfilter">leaflet-locationfilter</a>, <a href="http://mapbox.com/maki/">maki</a>, <a href="http://www.sjjb.co.uk/mapicons/">SJJB map icons</a>, <a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a>, <a href="https://github.com/MapBBCode/mapbbcode">MapBBCode</a>, <a href="http://momentjs.com/">Moment.js</a></li>
<li><span class="t" data-t="help.attr.other_libs"></span> <a href="https://github.com/tyrasd/osmtogeojson">osmtogeojson</a>, <a href="https://github.com/tyrasd/togpx">togpx</a>, <a href="https://github.com/mapbox/tokml">tokml</a>, <a href="http://lodash.com">lodash</a>, <a href="http://jquery.com/">jQuery</a>, <a href="http://jqueryui.com/">jQuery UI</a>, <a href="http://html2canvas.hertzen.com/">html2canvas</a>, <a href="http://code.google.com/p/canvg/">canvg</a>, <a href="https://github.com/kajic/leaflet-locationfilter">leaflet-locationfilter</a>, <a href="https://github.com/bbecquet/Leaflet.PolylineOffset">leaflet.PolylineOffset</a>, <a href="http://mapbox.com/maki/">maki</a>, <a href="http://www.sjjb.co.uk/mapicons/">SJJB map icons</a>, <a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a>, <a href="https://github.com/MapBBCode/mapbbcode">MapBBCode</a>, <a href="http://momentjs.com/">Moment.js</a></li>
</ul>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion js/jsmapcss/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ styleparser.inherit_from_Style(styleparser.PointStyle.prototype);
styleparser.ShapeStyle = function() {this.__init__()};

styleparser.ShapeStyle.prototype = {
properties: ['width','color','opacity','dashes','linecap','linejoin','line_style',
properties: ['width','offset','color','opacity','dashes','linecap','linejoin','line_style',
'fill_image','fill_color','fill_opacity','casing_width','casing_color','casing_opacity','casing_dashes','layer'],

width:0, color:null, opacity:NaN, dashes:[],
Expand Down
2 changes: 2 additions & 0 deletions js/overpass.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ setTimeout(function() {
if (p !== undefined) stl.opacity = p;
var p = get_property(styles, ["width"]);
if (p !== undefined) stl.weight = p;
var p = get_property(styles, ["offset"]);
if (p !== undefined) stl.offset = -p; // MapCSS and PolylineOffset definitions use different signs
var p = get_property(styles, ["dashes"]);
if (p !== undefined) stl.dashArray = p.join(",");
break;
Expand Down
22 changes: 22 additions & 0 deletions libs/polylineOffset/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2014 Benjamin Becquet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

39 changes: 39 additions & 0 deletions libs/polylineOffset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Leaflet Polyline Offset
===
Works with Leaflet 0.7.x and 0.8-dev. Should work with the upcoming 1.0.

This plugin adds to Leaflet `Polyline`s the ability to be drawn with a relative pixel offset, without modifying their actual `LatLng`s. The offset value can be either negative or positive, for left- or right-side offset, and remains constant across zoom levels.

## Use cases and demos

Line offsetting is the process of drawing a line parallel to an existant one, at a fixed distance. It's not a simple (x,y) translation of the whole shape, as it shouldn't overlap. It can be used to visually emphasize different properties of the same linear feature, or achieve complex composite styling.

This plugin brings this feature to Leaflet, to apply to client-side vectors.

Demos are clearer than words:
* [Basic demo](http://bbecquet.github.io/Leaflet.PolylineOffset/examples/example.html). The dashed line is the "model", with no offset applied. Red is with a -10px offset, green is with a 5px offset. The three are distinct `Polyline` objects but uses the same coordinate array.
* [Cycle lanes](http://bbecquet.github.io/Leaflet.PolylineOffset/examples/example_cycle.html). Drawing a road with two directions of cycle lanes, a main one and one shared.
* [Bus lines](http://bbecquet.github.io/Leaflet.PolylineOffset/examples/example_bus.html). A more complex demo. Offsets are computed automatically depending on the number of bus lines using the same segment. Other non-offset polylines are used to achieve the white and black outline effect.

## Usage

The plugin adds offset capabilities directly to the `L.Polyline` class.
```javascript
// Instantiate a normal Polyline with an 'offset' options, in pixels.
var pl = L.polyline([[48.8508, 2.3455], [48.8497, 2.3504], [48.8494, 2.35654]], {
offset: 5
});

// Setting the 'offset' property through the 'setStyle' method won't work.
// If you want to set the offset afterwards, use 'setOffset'.
pl.setOffset(-10);

// To cancel the offset, simply set it to 0
pl.setOffset(0);
```

## License
MIT.

## Authors
[Benjamin Becquet](//github.com/bbecquet)
59 changes: 59 additions & 0 deletions libs/polylineOffset/examples/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<title>Leaflet Polyline Offset example</title>

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<style>
* { margin: 0; padding: 0; }
html, body { height: 100%; }
#map { width:100%; height:100%; }
</style>

<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet-src.js"></script>
<script src="../leaflet.polylineoffset.js"></script>
<script>
window.onload = function() {
var map = new L.Map('map', {
center: [58.0, -11.0],
zoom: 4,
layers: [
new L.TileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png', {
attribution: 'Tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
maxZoom: 18,
subdomains: '1234'
})
]
});

var ll = [[58.44773, -28.65234], [53, -23.33496], [53, -14.32617], [58.1707, -10.37109], [59, -13], [57, -15], [57, -18], [60, -18], [63, -5], [59, -7], [58, -3], [56, -3]];

var orig = L.polyline(ll, {
weight: 2,
dashArray: '5,10',
color: 'black',
opacity: 0.3
}).addTo(map);

var leftPolyline = L.polyline(ll, {
color: '#f00',
opacity: 1,
offset: -5
}).addTo(map);

var rightPolyline = L.polyline(ll, {
color: '#080',
opacity: 1,
offset: 10
}).addTo(map);
};
</script>
</head>

<body>
<div id="map"></div>
</body>
</html>
Loading