The best way to understand these animation functions is to try them out. Start from the previous example:
<button onclick="doBounce(london);">Bounce To London</button> <button onclick="doBounce(rome);">Bounce To Rome</button <button onclick="doPan(london);">Pan To London</button> <button onclick="doPan(rome);">Pan To Rome</button> <button onclick="doRotate();">Rotate</button> <button onclick="doZoom(2);">Zoom Out</button> <button onclick="doZoom(0.5);">Zoom In</button>
These are regular HTML buttons that call a function when clicked. We'll add the functions in a moment.
Rome, next to the line where we defined the location of London:var rome = ol.proj.transform([12.5, 41.9], 'EPSG:4326', 'EPSG:3857');
<script> tag to handle the button clicks. Start with the doBounce() function:function doBounce(location) {
var bounce = ol.animation.bounce({
resolution: map.getView().getResolution() * 2
});
var pan = ol.animation.pan({
source: map.getView().getCenter()
});
map.beforeRender(bounce);
map.beforeRender(pan);
map.getView().setCenter(location);
}doPan() function:function doPan(location) {
var pan = ol.animation.pan({
source: map.getView().getCenter()
});
map.beforeRender(pan);
map.getView().setCenter(location);
}doRotate() function:function doRotate() {
var rotate = ol.animation.rotate({
rotation : Math.PI * 2
});
map.beforeRender(rotate);
}doZoom() function:function doZoom(factor) {
var resolution = map.getView().getResolution();
var zoom = ol.animation.zoom({
resolution: resolution
});
map.beforeRender(zoom);
map.getView().setResolution(resolution * factor);
}London and Rome.Not too bad! In a few lines of code, we managed to create some pretty impressive animation effects using ol.animation functions and the Map's beforeRender() method.
In step 3, we created the doBounce() function. This function takes a single parameter, location, which is the location that we want to bounce to. The effect we want to achieve is to smoothly zoom out from our current location and then into the new location. We called ol.animation.bounce() to create a function that implements the bounce animation effect. For this example, we provided only the resolution property and let the other properties (start, duration, and easing) take their default values. We set the resolution property to two times the current resolution of the map's view. This is equivalent to clicking the ZoomOut control (the button in the top-left corner of the map with the minus sign) once. What this does is smoothly zoom out from the current resolution to the next zoom level, and then back into the current resolution.
Next, we created a function that implements the pan animation effect. Again, we used the defaults for start, duration, and easing by not specifying them and set the source property to the current center of the map's view. We added both bounce and pan functions to the map by calling beforeRender(); then, we changed the map's view to go to the location we passed into this function.
In step 4, the doPan() function takes a single parameter, location, and smoothly pans the map to it. To do this, we called ol.animation.pan() with the map's current center for the source as we did in the doBounce() function, then we told the map's view to go to the new location.
In step 5, the doRotate() function doesn't take any parameters; we just wanted to spin the map 360 degrees. We used the ol.animation.rotate() function for this and set the rotation option to specify how much to rotate. When we add this to the map using beforeRender(), the effect happens immediately.
In OpenLayers, rotation is always specified in radians. When we think about rotation, we normally think in degrees. To convert from degrees to radians, it is useful to remember that 180 degrees = PI * radians. We don't need to know the value of PI, JavaScript provides us with the useful constant, Math.PI. The formula to convert degrees to radians is var radians = degrees * Math.PI / 180;. The inverse is var degrees = radians * 180 / Math.PI;. In our example, we wanted to rotate the map 360 degrees. Using the formula above, this becomes 2 * Math.PI, which is the value we used.
Step 6 adds the doZoom() function. It takes a single parameter, factor, which is the amount to zoom. We used ol.animation.zoom() to create our animation function using the view's current resolution as our starting point. Then, we added our animation function and told the map's view to zoom to a new resolution by multiplying the current resolution by the factor parameter. A factor of two will cause the map to zoom out to the next zoom level and a factor of 0.5 will cause the map to zoom in to the next zoom level.
Now that we've seen how the basic animations work, try modifying the animation properties in each function to see how to change its parameters and overriding the default duration, start time and easing functions. You can also try combining the animations in different ways.
When we click on a web page, the browser generates a MouseEvent that contains, among other things, the position that the click happened at. This position is in pixels and is relative to the browser window. In an OpenLayers application, we will often want to respond to the user interacting with the map and it is important to understand these events specify the position in pixels. It is common to need to determine the geographic coordinate that corresponds to this position. OpenLayers provides several methods that allow us to convert between the browser's pixel space and geographic coordinates:
The map object contains a few other methods that don't neatly fit into the previous groups; so, we've included them here for completeness: