* Prepare all tiles to fit the baselayer
(data)
| 13 | * Prepare all tiles to fit the baselayer |
| 14 | */ |
| 15 | prepareTileParts(data) { |
| 16 | return new Promise((resolve) => { |
| 17 | const tile = sharp(data.body); |
| 18 | tile |
| 19 | .metadata() |
| 20 | .then((metadata) => { |
| 21 | const x = data.box[0]; |
| 22 | const y = data.box[1]; |
| 23 | const sx = x < 0 ? 0 : x; |
| 24 | const sy = y < 0 ? 0 : y; |
| 25 | const dx = x < 0 ? -x : 0; |
| 26 | const dy = y < 0 ? -y : 0; |
| 27 | const extraWidth = x + (metadata.width - this.width); |
| 28 | const extraHeight = y + (metadata.width - this.height); |
| 29 | const w = metadata.width + (x < 0 ? x : 0) - (extraWidth > 0 ? extraWidth : 0); |
| 30 | const h = metadata.height + (y < 0 ? y : 0) - (extraHeight > 0 ? extraHeight : 0); |
| 31 | |
| 32 | // Fixed #20 https://github.com/StephanGeorg/staticmaps/issues/20 |
| 33 | if (!w || !h) { |
| 34 | resolve({ success: false }); |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | return tile |
| 39 | .extract({ |
| 40 | left: dx, |
| 41 | top: dy, |
| 42 | width: w, |
| 43 | height: h, |
| 44 | }) |
| 45 | .toBuffer() |
| 46 | .then((part) => { |
| 47 | resolve({ |
| 48 | success: true, |
| 49 | position: { top: Math.round(sy), left: Math.round(sx) }, |
| 50 | data: part, |
| 51 | }); |
| 52 | }) |
| 53 | .catch(() => resolve({ success: false })); |
| 54 | }) |
| 55 | .catch(() => resolve({ success: false })); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | async draw(tiles) { |
| 60 | this.tempBuffer = null; |