原文链接及内容

此示例演示如何使用view.animate()方法来运行一个或多个动画。

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import {easeIn, easeOut} from 'ol/easing.js';
import {fromLonLat} from 'ol/proj.js';

const london = fromLonLat([-0.12755, 51.507222]);
const moscow = fromLonLat([37.6178, 55.7517]);
const istanbul = fromLonLat([28.9744, 41.0128]);
const rome = fromLonLat([12.5, 41.9]);
const bern = fromLonLat([7.4458, 46.95]);

const view = new View({
center: istanbul,
zoom: 6,
});

const map = new Map({
target: 'map',
layers: [
new TileLayer({
preload: 4,
source: new OSM(),
}),
],
view: view,
});

// A bounce easing method (from https://github.com/DmitryBaranovskiy/raphael).
function bounce(t) {
const s = 7.5625;
const p = 2.75;
let l;
if (t < 1 / p) {
l = s * t * t;
} else {
if (t < 2 / p) {
t -= 1.5 / p;
l = s * t * t + 0.75;
} else {
if (t < 2.5 / p) {
t -= 2.25 / p;
l = s * t * t + 0.9375;
} else {
t -= 2.625 / p;
l = s * t * t + 0.984375;
}
}
}
return l;
}

// An elastic easing method (from https://github.com/DmitryBaranovskiy/raphael).
function elastic(t) {
return (
Math.pow(2, -10 * t) * Math.sin(((t - 0.075) * (2 * Math.PI)) / 0.3) + 1
);
}

function onClick(id, callback) {
document.getElementById(id).addEventListener('click', callback);
}

onClick('rotate-left', function () {
view.animate({
rotation: view.getRotation() + Math.PI / 2,
});
});

onClick('rotate-right', function () {
view.animate({
rotation: view.getRotation() - Math.PI / 2,
});
});

onClick('rotate-around-rome', function () {
// Rotation animation takes the shortest arc, so animate in two parts
const rotation = view.getRotation();
view.animate(
{
rotation: rotation + Math.PI,
anchor: rome,
easing: easeIn,
},
{
rotation: rotation + 2 * Math.PI,
anchor: rome,
easing: easeOut,
}
);
});

onClick('pan-to-london', function () {
view.animate({
center: london,
duration: 2000,
});
});

onClick('elastic-to-moscow', function () {
view.animate({
center: moscow,
duration: 2000,
easing: elastic,
});
});

onClick('bounce-to-istanbul', function () {
view.animate({
center: istanbul,
duration: 2000,
easing: bounce,
});
});

onClick('spin-to-rome', function () {
// Rotation animation takes the shortest arc, so animate in two parts
const center = view.getCenter();
view.animate(
{
center: [
center[0] + (rome[0] - center[0]) / 2,
center[1] + (rome[1] - center[1]) / 2,
],
rotation: Math.PI,
easing: easeIn,
},
{
center: rome,
rotation: 2 * Math.PI,
easing: easeOut,
}
);
});

function flyTo(location, done) {
const duration = 2000;
const zoom = view.getZoom();
let parts = 2;
let called = false;
function callback(complete) {
--parts;
if (called) {
return;
}
if (parts === 0 || !complete) {
called = true;
done(complete);
}
}
view.animate(
{
center: location,
duration: duration,
},
callback
);
view.animate(
{
zoom: zoom - 1,
duration: duration / 2,
},
{
zoom: zoom,
duration: duration / 2,
},
callback
);
}

onClick('fly-to-bern', function () {
flyTo(bern, function () {});
});

function tour() {
const locations = [london, bern, rome, moscow, istanbul];
let index = -1;
function next(more) {
if (more) {
++index;
if (index < locations.length) {
const delay = index === 0 ? 0 : 750;
setTimeout(function () {
flyTo(locations[index], next);
}, delay);
} else {
alert('Tour complete');
}
} else {
alert('Tour cancelled');
}
}
next(true);
}

onClick('tour', tour);

界面布局文件index.html代码如下:

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Animation</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>
<button id="rotate-left" title="Rotate clockwise"></button>
<button id="rotate-right" title="Rotate counterclockwise"></button>
<button id="pan-to-london">Pan to London</button>
<button id="elastic-to-moscow">Elastic to Moscow</button>
<button id="bounce-to-istanbul">Bounce to Istanbul</button>
<button id="spin-to-rome">Spin to Rome</button>
<button id="fly-to-bern">Fly to Bern</button>
<button id="rotate-around-rome">Rotate around Rome</button>
<button id="tour">Take a tour</button>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>