Table of Contents for
Mastering OpenLayers 3

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering OpenLayers 3 by Gábor Farkas Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Mastering OpenLayers 3
  4. Mastering OpenLayers 3
  5. Credits
  6. About the Author
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Creating Simple Maps with OpenLayers 3
  16. Structure of OpenLayers 3
  17. Building the layout
  18. Using the API documentation
  19. Debugging the code
  20. Summary
  21. 2. Applying Custom Styles
  22. Customizing the default appearance
  23. Styling vector layers
  24. Customizing the appearance with JavaScript
  25. Creating a WebGIS client layout
  26. Summary
  27. 3. Working with Layers
  28. Building a layer tree
  29. Adding layers dynamically
  30. Adding vector layers with the File API
  31. Adding vector layers with a library
  32. Removing layers dynamically
  33. Changing layer attributes
  34. Changing the layer order with the Drag and Drop API
  35. Clearing the message bar
  36. Summary
  37. 4. Using Vector Data
  38. Accessing attributes
  39. Setting attributes
  40. Validating attributes
  41. Creating thematic layers
  42. Saving vector data
  43. Saving with WFS-T
  44. Modifying the geometry
  45. Summary
  46. 5. Creating Responsive Applications with Interactions and Controls
  47. Building the toolbar
  48. Mapping interactions to controls
  49. Building a set of feature selection controls
  50. Adding new vector layers
  51. Building a set of drawing tools
  52. Modifying and snapping to features
  53. Creating new interactions
  54. Building a measuring control
  55. Summary
  56. 6. Controlling the Map – View and Projection
  57. Customizing a view
  58. Constraining a view
  59. Creating a navigation history
  60. Working with extents
  61. Rotating a view
  62. Changing the map's projection
  63. Creating custom animations
  64. Summary
  65. 7. Mastering Renderers
  66. Using different renderers
  67. Creating a WebGL map
  68. Drawing lines and polygons with WebGL
  69. Blending layers
  70. Clipping layers
  71. Exporting a map
  72. Creating a raster calculator
  73. Creating a convolution matrix
  74. Clipping a layer with WebGL
  75. Summary
  76. 8. OpenLayers 3 for Mobile
  77. Responsive styling with CSS
  78. Generating geocaches
  79. Adding device-dependent controls
  80. Vectorizing the mobile version
  81. Making the mobile application interactive
  82. Summary
  83. 9. Tools of the Trade – Integrating Third-Party Applications
  84. Exporting a QGIS project
  85. Importing shapefiles
  86. Spatial analysis with Turf
  87. Spatial analysis with JSTS
  88. 3D rendering with Cesium
  89. Summary
  90. 10. Compiling Custom Builds with Closure
  91. Configuring Node JS
  92. Compiling OpenLayers 3
  93. Bundling an application with OpenLayers 3
  94. Extending OpenLayers 3
  95. Creating rich documentation with JSDoc
  96. Summary
  97. Index

Blending layers

In the next three examples, we will fall back on the Canvas renderer. It is the most developed and stable one, providing a lot of perks by allowing canvas manipulation methods. The map and every layer is rendered on a different canvas, while in the end, they are aggregated into a single composition. This pattern, the existence of precompose, and the postcompose events (rendering hooks) enable us to manipulate the context of any layer or the map as a whole. We can basically use any canvas manipulation method as long as we can get the original context of the layers or map with these events.

In this example, called ch07_blend, we will discuss one of the most useful canvas manipulation methods, the globalCompositionContext. For this example, we modify our layer tree's createRegistry method, and add some blending options to every registered layer:

var layerTree = function (options) {
    […]
        this.createRegistry = function (layer, buffer) {
            […]
            layerControls.appendChild(document.createElement('br'));
            var blendMode = document.createElement('select');
            blendMode.appendChild(this.createOption('source-over'));
            blendMode.appendChild(this.createOption('lighten'));
            blendMode.appendChild(this.createOption('darken'));
            blendMode.appendChild(this.createOption('multiply'));
            blendMode.appendChild(this.createOption('difference'));
            blendMode.addEventListener('change', function () {
                if (layer.get('blendMode')) {
                    ol.Observable.unByKey(layer.get('blendMode'));
                    layer.unset('blendMode');
                }
                layer.set('blendMode', layer.on('precompose', function (evt) {
                    evt.context.globalCompositeOperation = blendMode.value;
                }));
                _this.map.render();
            });
            layerControls.appendChild(this.stopPropagationOnEvent(blendMode, 'click'));
            […]

We create a select element for a limited number of blending options. When we change the element's value, we remove the current event listener, if we have one, and then register a new listener on the layer's precompose event. In the listener, we simply change the canvas element's context globalCompositeOperation property.

Tip

The default composite operation is source-over. You can find a description for every valid operation on the MDN site at http://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation.

Remember: only one composite operation can be used on a single canvas. Because of the rendering pattern of OpenLayers 3, we can apply a different operation on every layer. However, the library keeps the last operation for every layer from there. Due to this phenomenon, we should explicitly define an operation for every layer. This can be done by registering a listener with the default value (source-over) in every layer that's added to the map or leaving the whole process to the users for the purpose of experimenting.

If you load the example, you can experiment with blending methods too. For a better experience, you can change or extend the blending options with the help of more legal operations, load more layers, and try to create complex compositions:

Blending layers