-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.js
162 lines (139 loc) · 5.07 KB
/
lambda.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
const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');
//Zip nodejs folder and upload to s3 , create lambda layer with src from s3 (file limitations)
//Needs to be nodejs folder in root with node_modules inside for the lambda to recognise the packages
//Debug timer
// let times = [];
// const timer = () => {
// var currentdate = new Date();
// var datetime =
// "Now: " +
// currentdate.getDate() +
// "/" +
// (currentdate.getMonth() + 1) +
// "/" +
// currentdate.getFullYear() +
// " @ " +
// currentdate.getHours() +
// ":" +
// currentdate.getMinutes() +
// ":" +
// currentdate.getSeconds();
// times.push(datetime);
// console.log(datetime);
// };
exports.handler = async (event, context) => {
//timer();
let browser = null;
let result = null;
// Get URL parameters or default to London, UK
// 51.12 N, 0.13° W
// Get URL parameters
const queryParams = event.queryStringParameters || {};
const latitude = parseFloat(queryParams.latitude) || 51.12;
const longitude = parseFloat(queryParams.longitude) || 0.13;
const zoom = parseFloat(queryParams.zoom) || 5;
try {
//Launch new browser
browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
// Set vp
await page.setViewport({ width: 1920, height: 1080 });
// Load external scripts
const deckScript = 'https://unpkg.com/deck.gl@latest/dist.min.js';
await page.addScriptTag({ url: deckScript });
// Add HTML content
await page.setContent(`
<!DOCTYPE html>
<html>
<head>
<style>
body { margin: 0;
width: 100vw;
height: 100vh;
overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="deck-canvas"></canvas>
</body>
</html>
`);
// await page.exposeFunction("timer", timer);
// Define the Deck.gl Globe
await page.evaluate(({ latitude, longitude, zoom }) => {
const { DeckGL, _GlobeView, TileLayer, BitmapLayer, COORDINATE_SYSTEM } = deck;
new DeckGL({
views: new _GlobeView({
resolution: 10
}),
initialViewState: {
longitude: longitude,
latitude: latitude,
zoom: zoom,
minZoom: 0,
maxZoom: 20
},
controller: true,
// onBeforeRender: async () => {
// await timer();
// },
layers: [
new TileLayer({
data: 'https://c.tile.openstreetmap.org/{z}/{x}/{y}.png',
minZoom: 0,
maxZoom: 15,
tileSize: 256,
renderSubLayers: props => {
const {
bbox: { west, south, east, north }
} = props.tile;
return new BitmapLayer(props, {
data: null,
image: props.data,
_imageCoordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
bounds: [west, south, east, north]
});
}
})
]
});
}, { latitude, longitude, zoom });
// Wait for the scene to render 12sec seem enough, any lower will have missing tiles from the render
await page.waitForTimeout(2000);
// Take a screenshot and save it as a png or add to buffer as response / change to png but takes more resources
const screenshot = await page.screenshot({type: 'png', encoding: 'base64'});
// Take screenshot of page and convert to png buffer
const screenshotBuffer = screenshot;
///save the screenshot locally or upload to s3
// fs.writeFileSync('deckgl-globe.png', screenshot);
await browser.close();
// await timer();
// let responseBody = {
// image: screenshotBuffer.toString("base64"),
// times: times,
// };
// Return png buffer as response
const response = {
statusCode: 200,
headers: {
'Content-Type': 'image/png',
},
isBase64Encoded: true,
body: screenshotBuffer.toString("base64")
};
return response
} catch (error) {
console.error(error);
} finally {
if (browser) {
await browser.close();
}
}
};