UNPKG

3.19 kBJavaScriptView Raw
1goog.require('ol.Map');
2goog.require('ol.View');
3goog.require('ol.extent');
4goog.require('ol.format.GeoJSON');
5goog.require('ol.has');
6goog.require('ol.layer.Vector');
7goog.require('ol.proj');
8goog.require('ol.source.Vector');
9goog.require('ol.style.Fill');
10goog.require('ol.style.Stroke');
11goog.require('ol.style.Style');
12
13var canvas = document.createElement('canvas');
14var context = canvas.getContext('2d');
15
16// Gradient and pattern are in canvas pixel space, so we adjust for the
17// renderer's pixel ratio
18var pixelRatio = ol.has.DEVICE_PIXEL_RATIO;
19
20// Generate a rainbow gradient
21function gradient(feature, resolution) {
22 var extent = feature.getGeometry().getExtent();
23 // Gradient starts on the left edge of each feature, and ends on the right.
24 // Coordinate origin is the top-left corner of the extent of the geometry, so
25 // we just divide the geometry's extent width by resolution and multiply with
26 // pixelRatio to match the renderer's pixel coordinate system.
27 var grad = context.createLinearGradient(0, 0,
28 ol.extent.getWidth(extent) / resolution * pixelRatio, 0);
29 grad.addColorStop(0, 'red');
30 grad.addColorStop(1 / 6, 'orange');
31 grad.addColorStop(2 / 6, 'yellow');
32 grad.addColorStop(3 / 6, 'green');
33 grad.addColorStop(4 / 6, 'aqua');
34 grad.addColorStop(5 / 6, 'blue');
35 grad.addColorStop(1, 'purple');
36 return grad;
37}
38
39// Generate a canvasPattern with two circles on white background
40var pattern = (function() {
41 canvas.width = 11 * pixelRatio;
42 canvas.height = 11 * pixelRatio;
43 // white background
44 context.fillStyle = 'white';
45 context.fillRect(0, 0, canvas.width, canvas.height);
46 // outer circle
47 context.fillStyle = 'rgba(102, 0, 102, 0.5)';
48 context.beginPath();
49 context.arc(5 * pixelRatio, 5 * pixelRatio, 4 * pixelRatio, 0, 2 * Math.PI);
50 context.fill();
51 // inner circle
52 context.fillStyle = 'rgb(55, 0, 170)';
53 context.beginPath();
54 context.arc(5 * pixelRatio, 5 * pixelRatio, 2 * pixelRatio, 0, 2 * Math.PI);
55 context.fill();
56 return context.createPattern(canvas, 'repeat');
57}());
58
59// Generate style for gradient or pattern fill
60var fill = new ol.style.Fill();
61var style = new ol.style.Style({
62 fill: fill,
63 stroke: new ol.style.Stroke({
64 color: '#333',
65 width: 2
66 })
67});
68
69/**
70 * The styling function for the vector layer, will return an array of styles
71 * which either contains the aboove gradient or pattern.
72 *
73 * @param {ol.Feature} feature The feature to style.
74 * @param {number} resolution Resolution.
75 * @return {ol.style.Style} The style to use for the feature.
76 */
77var getStackedStyle = function(feature, resolution) {
78 var id = feature.getId();
79 fill.setColor(id > 'J' ? gradient(feature, resolution) : pattern);
80 return style;
81};
82
83// Create a vector layer that makes use of the style function above…
84var vectorLayer = new ol.layer.Vector({
85 source: new ol.source.Vector({
86 url: 'data/geojson/countries.geojson',
87 format: new ol.format.GeoJSON()
88 }),
89 style: getStackedStyle
90});
91
92// … finally create a map with that layer.
93var map = new ol.Map({
94 layers: [
95 vectorLayer
96 ],
97 target: 'map',
98 view: new ol.View({
99 center: ol.proj.fromLonLat([7, 52]),
100 zoom: 3
101 })
102});