-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·182 lines (155 loc) · 3.96 KB
/
script.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
require([
"esri/Camera",
"esri/Map",
"esri/views/SceneView",
"esri/layers/Layer",
"esri/layers/GeoJSONLayer",
"esri/geometry/geometryEngine",
"esri/core/watchUtils",
"animations/layers/LineLayerAnimation",
"animations/support/interpolate"
], function(
Camera,
Map,
SceneView,
Layer,
GeoJSONLayer,
geometryEngine,
watchUtils,
LineLayerAnimation,
interpolate
) {
// Overview camera
var cameraA = new Camera({
position: {
spatialReference: { latestWkid: 3857, wkid: 102100 },
x: -7909937.703683352,
y: 5214411.41498922,
z: 5662.71134893503
},
heading: 0,
tilt: 0
});
var datetimeA = new Date("Fri Jun 21 2019 19:00:00 GMT+0200");
var cameraB = new Camera({
position: {
spatialReference: { latestWkid: 3857, wkid: 102100 },
x: -7912108.151415734,
y: 5213899.50879747,
z: 725.303618597798
},
heading: 74.23167582579066,
tilt: 43.404465157723436
});
var datetimeB = new Date("Sat Jun 22 2019 00:45:00 GMT+0200");
var map = new Map({
basemap: "topo",
ground: "world-elevation"
});
// Add Boston route
var route = new GeoJSONLayer({
url: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2969649/boston-route.geojson",
renderer: {
type: "simple",
symbol: {
type: "simple-line",
width: 5,
color: "orange"
}
}
});
var animation = new LineLayerAnimation({
sourceLayer: route
});
animation.whenAnimatedLayer().then(function(animatedLayer) {
map.add(animatedLayer);
});
var routeObjectId = null;
route.queryObjectIds().then(function(objectIds) {
routeObjectId = objectIds[0];
});
// Add 3D buildings and trees of Boston
Layer.fromPortalItem({
portalItem: {
id: "2d913b3b8caf4f3d87be84ff19d77ac7"
}
}).then(function(layer) {
map.add(layer);
});
Layer.fromPortalItem({
portalItem: {
id: "e1c77e6d3809412f8755faaf810520df"
}
}).then(function(layer) {
map.add(layer);
});
var view = new SceneView({
container: "viewDiv",
map: map,
ui: {
components: ["attribution"]
},
camera: cameraA,
environment: {
lighting: {
date: datetimeA,
directShadowsEnabled: true
}
}
});
// SCROLL ANIMATION
var scrollTopOffset = 0;
function getScrollProgress(element) {
var elemRect = element.getBoundingClientRect();
// return window.pageYOffset || document.documentElement.scrollTop;
var top = elemRect.top + scrollTopOffset;
var windowHeight =
window.innerHeight || document.documentElement.clientHeight;
var progress = Math.min(Math.max(windowHeight - top, 0), elemRect.height);
return progress / elemRect.height;
}
function render() {
var camera = interpolate(
cameraA,
cameraB,
getScrollProgress(cameraSection)
);
view.goTo(camera, { animate: false });
if (routeObjectId) {
animation.seek(getScrollProgress(pathSection), routeObjectId);
}
view.environment.lighting.date = interpolate(
datetimeA,
datetimeB,
getScrollProgress(daylightSection)
);
}
var apparentScrollTop = 0;
var animating = false;
function animationLoop() {
var previousScrollTop = apparentScrollTop;
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
apparentScrollTop =
Math.round(interpolate(apparentScrollTop, scrollTop, 0.2) * 100) / 100;
// Make sure animation loop does not run forever
if (previousScrollTop === apparentScrollTop) {
apparentScrollTop = scrollTop;
}
scrollTopOffset = Math.round(scrollTop - apparentScrollTop);
animating = true;
// udpate view
render();
if (scrollTopOffset) {
requestAnimationFrame(animationLoop);
} else {
console.log("Pause animation");
animating = false;
}
}
window.onscroll = function(e) {
if (!animating) {
console.log("Start animation");
animationLoop();
}
};
});