-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated MapLibre version and adjusted MaptilerGeolocateControl * removed the zoomAdjust unnecesary value in minimap * Adding sky examples * Now passing the options to the control * Updated xmldom dep * Add screenshot helper * adding screenshot helper and readme * Adding whole page screenshot * removed an old async keyword * removed an old async keyword * removed an old async keyword * linting * adapt UMD bundle name to prod or dev mode * removing whole page screenshot
- Loading branch information
1 parent
faf96b1
commit e42ebb3
Showing
6 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { Map as MapSDK } from "../Map"; | ||
|
||
/** | ||
* Takes a screenshot (PNG file) of the curent map view. | ||
* Depending on the options, this function can automatically trigger a download of te file. | ||
*/ | ||
export async function takeScreenshot( | ||
map: MapSDK, | ||
options: { | ||
/** | ||
* If `true`, this function will trigger a download in addition to returning a blob. | ||
* Default: `false` | ||
*/ | ||
download?: boolean; | ||
|
||
/** | ||
* Only if `options.download` is `true`. Indicates the filename under which | ||
* the file will be downloaded. | ||
* Default: `"maptiler_screenshot.png"` | ||
*/ | ||
filename?: string; | ||
} = {}, | ||
): Promise<Blob> { | ||
const download = options.download ?? false; | ||
const blob = await getMapScreenshotBlob(map); | ||
|
||
if (download) { | ||
const filename = options.filename ?? "maptiler_screenshot.png"; | ||
|
||
const link = document.createElement("a"); | ||
link.style.display = "none"; | ||
document.body.appendChild(link); | ||
link.href = URL.createObjectURL(blob); | ||
link.download = filename; | ||
link.click(); | ||
|
||
// Cleaning after event loop | ||
setTimeout(() => { | ||
document.body.removeChild(link); | ||
URL.revokeObjectURL(link.href); | ||
}, 0); | ||
} | ||
|
||
return blob; | ||
} | ||
|
||
function getMapScreenshotBlob(map: MapSDK): Promise<Blob> { | ||
return new Promise((resolve, reject) => { | ||
map.redraw(); | ||
|
||
map.once("idle", () => { | ||
map.getCanvas().toBlob((blob) => { | ||
if (!blob) { | ||
return reject(Error("Screenshot could not be created.")); | ||
} | ||
|
||
resolve(blob); | ||
}, "image/png"); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters