Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(globe): add screenshot export #399

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/scripts/components/globe-navigation/globe-navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {DownloadIcon} from '../icons/download-icon';
import setGlobeProjectionAction from '../../actions/set-globe-projection';
import {projectionSelector} from '../../selectors/globe/projection';
import setFlyToAction from '../../actions/set-fly-to';
import {downloadScreenshot} from '../../libs/download-screenshot';

import {GlobeProjection} from '../../types/globe-projection';

Expand Down Expand Up @@ -40,7 +41,7 @@ const GlobeNavigation: FunctionComponent = () => {
<Button
className={styles.downloadIcon}
icon={DownloadIcon}
onClick={() => console.log('placeholder')}
onClick={() => downloadScreenshot()}
/>
</div>
);
Expand Down
7 changes: 6 additions & 1 deletion src/scripts/components/globe/globe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ const cesiumOptions = {
navigationHelpButton: false,
animation: false,
timeline: false,
baseLayerPicker: false
baseLayerPicker: false,
contextOptions: {
webgl: {
preserveDrawingBuffer: true
}
}
};

interface Props {
Expand Down
42 changes: 42 additions & 0 deletions src/scripts/libs/download-screenshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Downloads one or both Cesium canvases as an image file
export function downloadScreenshot() {
const canvases = Array.from(
document.querySelectorAll('.cesium-viewer canvas') as NodeListOf<
HTMLCanvasElement
>
);

const finalCanvas = combineCanvases(canvases);
download(finalCanvas.toDataURL());
}

function combineCanvases(canvases: HTMLCanvasElement[]) {
if (canvases.length < 2) {
return canvases[0];
}

const canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext('2d');

canvases.forEach((tmpCanvas, index) =>
ctx?.drawImage(tmpCanvas, (window.innerWidth / 2) * index, 0)
);

return canvas;
}

function download(url: string) {
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', 'globe.png');
link.style.display = 'none';

document.body.appendChild(link);

setTimeout(() => {
link.click();
document.body.removeChild(link);
}, 1);
}