原文链接及内容

使用GIF动画作为图标的例子,动画是使用Gifler库实现的。参考文档:https://themadcreator.github.io/gifler/docs.html 。
main.js
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import Feature from 'ol/Feature.js'; import Map from 'ol/Map.js'; import Point from 'ol/geom/Point.js'; import View from 'ol/View.js'; import {Icon, Style} from 'ol/style.js'; import {Stamen, Vector as VectorSource} from 'ol/source.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
const iconFeature = new Feature({ geometry: new Point([0, 0]), });
const vectorSource = new VectorSource({ features: [iconFeature], });
const vectorLayer = new VectorLayer({ source: vectorSource, });
const rasterLayer = new TileLayer({ source: new Stamen({ layer: 'toner', }), });
const map = new Map({ layers: [rasterLayer, vectorLayer], target: document.getElementById('map'), view: new View({ center: [0, 0], zoom: 2, }), });
const gifUrl = 'data/globe.gif'; const gif = gifler(gifUrl); gif.frames( document.createElement('canvas'), function (ctx, frame) { if (!iconFeature.getStyle()) { iconFeature.setStyle( new Style({ image: new Icon({ img: ctx.canvas, imgSize: [frame.width, frame.height], opacity: 0.8, }), }) ); } ctx.clearRect(0, 0, frame.width, frame.height); ctx.drawImage(frame.buffer, frame.x, frame.y); map.render(); }, true );
map.on('pointermove', function (e) { const pixel = map.getEventPixel(e.originalEvent); const hit = map.hasFeatureAtPixel(pixel); map.getTarget().style.cursor = hit ? 'pointer' : ''; });
|
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Animated GIF</title> <link rel="stylesheet" href="node_modules/ol/ol.css"> <style> .map { width: 100%; height: 400px; } </style> </head> <body> <div id="map" class="map"></div> <script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script> <script src="https://cdn.jsdelivr.net/npm/gifler@0.1.0/gifler.min.js"></script> <script type="module" src="main.js"></script> </body> </html>
|
Vue版本代码:以下代码没有打包测试,由于gifler库不支持es6,所以需要做兼容处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <template> <div class="oneMap"> <TdtMap @mapCreated="mapCreated" /> </div> </template>
<script setup> import Feature from "ol/Feature.js"; import Point from "ol/geom/Point.js"; import VectorLayer from "ol/layer/Vector.js"; import VectorSource from "ol/source/Vector.js"; import Icon from "ol/style/Icon.js"; import Style from "ol/style/Style.js";
let map, gifler;
const iconFeature = new Feature({ geometry: new Point([0, 0]), });
const vectorSource = new VectorSource({ features: [iconFeature], });
const vectorLayer = new VectorLayer({ source: vectorSource, });
async function mapCreated(m) { map = m; map.addLayer(vectorLayer); gifler = (await import("gifler")).default || window.gifler; const gif = gifler("data/globe.gif"); gif.frames( document.createElement("canvas"), function (ctx, frame) { if (!iconFeature.getStyle()) { iconFeature.setStyle( new Style({ image: new Icon({ img: ctx.canvas, opacity: 0.8, }), }) ); } ctx.clearRect(0, 0, frame.width, frame.height); ctx.drawImage(frame.buffer, frame.x, frame.y); map.render(); }, true );
map.on("pointermove", function (e) { const hit = map.hasFeatureAtPixel(e.pixel); map.getTargetElement().style.cursor = hit ? "pointer" : ""; }); } </script>
<style lang="scss" scoped> .oneMap { position: relative; height: 100%; overflow: hidden; } </style>
|