Skip to content

Commit

Permalink
Add frame capture to web viewer (AcademySoftwareFoundation#1636)
Browse files Browse the repository at this point in the history
Add frame capture code to trigger on 'f' key. This is the same key as used for the desktop viewer.
  • Loading branch information
kwokcb authored Jan 17, 2024
1 parent 0375249 commit c0ccd34
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions javascript/MaterialXView/source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ let turntableEnabled = false;
let turntableSteps = 360;
let turntableStep = 0;

let captureRequested = false;

// Get URL options. Fallback to defaults if not specified.
let materialFilename = new URLSearchParams(document.location.search).get("file");
if (!materialFilename) {
Expand All @@ -31,6 +33,18 @@ let viewer = Viewer.create();
init();
viewer.getEditor().updateProperties(0.9);

// Capture the current frame and save an image file.
function captureFrame()
{
let canvas = document.getElementById('webglcanvas');
var url = canvas.toDataURL();
var link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('target', '_blank');
link.setAttribute('download', 'screenshot.png');
link.click();
}

function init()
{
let canvas = document.getElementById('webglcanvas');
Expand Down Expand Up @@ -82,9 +96,15 @@ function init()
orbitControls = new OrbitControls(scene.getCamera(), renderer.domElement);
orbitControls.addEventListener('change', () => {
viewer.getScene().setUpdateTransforms();
})
})

// Load model and shaders
// Add hotkey 'f' to capture the current frame and save an image file.
// See check inside the render loop when a capture can be performed.
document.addEventListener('keydown', (event) => {
if (event.key === 'f') {
captureRequested = true;
}
});

// Initialize editor
viewer.getEditor().initialize();
Expand Down Expand Up @@ -160,6 +180,12 @@ function animate()

composer.render();
viewer.getScene().updateTransforms();

if (captureRequested)
{
captureFrame();
captureRequested = false;
}
}

function handleKeyEvents(event)
Expand Down

0 comments on commit c0ccd34

Please sign in to comment.