Sometimes it's useful to apply translation (that is, pixel offset) to a layer (and not other layers).
For example, two line-based layers can be visually compared by translating (that is, offsetting) a layer.
For vector layers, it can be done by translating vector features. But for the tiled image layers, (for example, road traffic information tiles generated by GeoServer) how can it be done?
This is also a nice example for
precompose
andpostcompose
render event handlers.precompose
is triggered before a layer is rendered, andpostcompose
afterwards. Inside the event handlers you have direct access to the canvas context, so you can use CanvasRenderingContext2D.translate() to offset the rendering for a layer.http://jsfiddle.net/m1abjrkm/3/
Note: This example does not take into account rotated maps!
Update: As @zeodtr points out in his comment, there is a problem at the tile borders with this approach. The following screenshot created by him illustrates the problem: