UNPKG

4.57 kBJavaScriptView Raw
1goog.require('ol.Feature');
2goog.require('ol.Map');
3goog.require('ol.View');
4goog.require('ol.geom.Point');
5goog.require('ol.layer.Vector');
6goog.require('ol.source.Vector');
7goog.require('ol.style.Circle');
8goog.require('ol.style.Fill');
9goog.require('ol.style.Stroke');
10goog.require('ol.style.Style');
11
12
13// Create separate layers for red, green an blue circles.
14//
15// Every layer has one feature that is styled with a circle, together the
16// features form the corners of an equilateral triangle and their styles overlap
17var redLayer = new ol.layer.Vector({
18 source: new ol.source.Vector({
19 features: [new ol.Feature(new ol.geom.Point([0, 0]))]
20 }),
21 style: new ol.style.Style({
22 image: new ol.style.Circle({
23 fill: new ol.style.Fill({
24 color: 'rgba(255,0,0,0.8)'
25 }),
26 stroke: new ol.style.Stroke({
27 color: 'rgb(255,0,0)',
28 width: 15
29 }),
30 radius: 120
31 })
32 })
33});
34var greenLayer = new ol.layer.Vector({
35 source: new ol.source.Vector({
36 // 433.013 is roughly 250 * Math.sqrt(3)
37 features: [new ol.Feature(new ol.geom.Point([250, 433.013]))]
38 }),
39 style: new ol.style.Style({
40 image: new ol.style.Circle({
41 fill: new ol.style.Fill({
42 color: 'rgba(0,255,0,0.8)'
43 }),
44 stroke: new ol.style.Stroke({
45 color: 'rgb(0,255,0)',
46 width: 15
47 }),
48 radius: 120
49 })
50 })
51});
52var blueLayer = new ol.layer.Vector({
53 source: new ol.source.Vector({
54 features: [new ol.Feature(new ol.geom.Point([500, 0]))]
55 }),
56 style: new ol.style.Style({
57 image: new ol.style.Circle({
58 fill: new ol.style.Fill({
59 color: 'rgba(0,0,255,0.8)'
60 }),
61 stroke: new ol.style.Stroke({
62 color: 'rgb(0,0,255)',
63 width: 15
64 }),
65 radius: 120
66 })
67 })
68});
69
70// Create the map, the view is centered on the triangle. Zooming and panning is
71// restricted to a sane area
72var map = new ol.Map({
73 layers: [
74 redLayer,
75 greenLayer,
76 blueLayer
77 ],
78 target: 'map',
79 view: new ol.View({
80 center: [250, 220],
81 extent: [0, 0, 500, 500],
82 resolution: 4,
83 minResolution: 2,
84 maxResolution: 32
85 })
86});
87
88// Get the form elements and bind the listeners
89var select = document.getElementById('blend-mode');
90var affectRed = document.getElementById('affect-red');
91var affectGreen = document.getElementById('affect-green');
92var affectBlue = document.getElementById('affect-blue');
93
94
95/**
96 * This method sets the globalCompositeOperation to the value of the select
97 * field and it is bound to the precompose event of the layers.
98 *
99 * @param {ol.render.Event} evt The render event.
100 */
101var setBlendModeFromSelect = function(evt) {
102 evt.context.globalCompositeOperation = select.value;
103};
104
105
106/**
107 * This method resets the globalCompositeOperation to the default value of
108 * 'source-over' and it is bound to the postcompose event of the layers.
109 *
110 * @param {ol.render.Event} evt The render event.
111 */
112var resetBlendModeFromSelect = function(evt) {
113 evt.context.globalCompositeOperation = 'source-over';
114};
115
116
117/**
118 * Bind the pre- and postcompose handlers to the passed layer.
119 *
120 * @param {ol.layer.Vector} layer The layer to bind the handlers to.
121 */
122var bindLayerListeners = function(layer) {
123 layer.on('precompose', setBlendModeFromSelect);
124 layer.on('postcompose', resetBlendModeFromSelect);
125};
126
127
128/**
129 * Unind the pre- and postcompose handlers to the passed layers.
130 *
131 * @param {ol.layer.Vector} layer The layer to unbind the handlers from.
132 */
133var unbindLayerListeners = function(layer) {
134 layer.un('precompose', setBlendModeFromSelect);
135 layer.un('postcompose', resetBlendModeFromSelect);
136};
137
138
139/**
140 * Handler for the click event of the 'affect-XXX' checkboxes.
141 *
142 * @this {HTMLInputElement}
143 */
144var affectLayerClicked = function() {
145 var layer;
146 if (this.id == 'affect-red') {
147 layer = redLayer;
148 } else if (this.id == 'affect-green') {
149 layer = greenLayer;
150 } else {
151 layer = blueLayer;
152 }
153 if (this.checked) {
154 bindLayerListeners(layer);
155 } else {
156 unbindLayerListeners(layer);
157 }
158 map.render();
159};
160
161
162// Rerender map when blend mode changes
163select.addEventListener('change', function() {
164 map.render();
165});
166
167// Unbind / bind listeners depending on the checked state when the checkboxes
168// are clicked
169affectRed.addEventListener('click', affectLayerClicked);
170affectGreen.addEventListener('click', affectLayerClicked);
171affectBlue.addEventListener('click', affectLayerClicked);
172
173// Initially bind listeners
174bindLayerListeners(redLayer);
175bindLayerListeners(greenLayer);
176bindLayerListeners(blueLayer);