From d1e734cb814e4821e24f74ce171f1ec4fea74549 Mon Sep 17 00:00:00 2001 From: Bernard Kwok Date: Thu, 4 Jan 2024 15:16:38 -0500 Subject: [PATCH 1/6] Add snapshot code to trigger on 's' key. --- javascript/MaterialXView/source/index.js | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/javascript/MaterialXView/source/index.js b/javascript/MaterialXView/source/index.js index ddd1ec8ffc..8db30959d3 100644 --- a/javascript/MaterialXView/source/index.js +++ b/javascript/MaterialXView/source/index.js @@ -21,6 +21,8 @@ let turntableEnabled = false; let turntableSteps = 360; let turntableStep = 0; +let snapshot = false; + // Get URL options. Fallback to defaults if not specified. let materialFilename = new URLSearchParams(document.location.search).get("file"); if (!materialFilename) { @@ -31,6 +33,17 @@ let viewer = Viewer.create(); init(); viewer.getEditor().updateProperties(0.9); +function performSnapshot() +{ + 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'); @@ -75,9 +88,14 @@ function init() orbitControls = new OrbitControls(scene.getCamera(), renderer.domElement); orbitControls.addEventListener('change', () => { viewer.getScene().setUpdateTransforms(); - }) + }) - // Load model and shaders + // Add hotkey S to save the contents of the contents of the thee.sjs renderer to a file + document.addEventListener('keydown', (event) => { + if (event.key === 's') { + snapshot = true; + } + }); // Initialize editor viewer.getEditor().initialize(); @@ -153,6 +171,12 @@ function animate() composer.render(); viewer.getScene().updateTransforms(); + + if (snapshot) + { + performSnapshot(); + snapshot = false; + } } function handleKeyEvents(event) From 55c21e314d31b0ebd1243c8d89b52c698b9f715a Mon Sep 17 00:00:00 2001 From: Bernard Kwok Date: Thu, 4 Jan 2024 15:23:20 -0500 Subject: [PATCH 2/6] Comment cleanup. --- javascript/MaterialXView/source/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/javascript/MaterialXView/source/index.js b/javascript/MaterialXView/source/index.js index 8db30959d3..2f94e0b2f3 100644 --- a/javascript/MaterialXView/source/index.js +++ b/javascript/MaterialXView/source/index.js @@ -33,6 +33,7 @@ let viewer = Viewer.create(); init(); viewer.getEditor().updateProperties(0.9); +// Save the contents of the rendered canvas to a file. function performSnapshot() { let canvas = document.getElementById('webglcanvas'); @@ -90,7 +91,8 @@ function init() viewer.getScene().setUpdateTransforms(); }) - // Add hotkey S to save the contents of the contents of the thee.sjs renderer to a file + // Add hotkey S to save the contents of the rendered canvas to a file. + // See check inside the render loop when a snapshot can be performed. document.addEventListener('keydown', (event) => { if (event.key === 's') { snapshot = true; From e02ef011155b80a28489f9958cd76d94acc7da07 Mon Sep 17 00:00:00 2001 From: Bernard Kwok Date: Fri, 5 Jan 2024 14:27:23 -0500 Subject: [PATCH 3/6] Change to 'f' key to be consistent with desktop viewer. --- javascript/MaterialXView/source/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/javascript/MaterialXView/source/index.js b/javascript/MaterialXView/source/index.js index 2f94e0b2f3..f5e0663491 100644 --- a/javascript/MaterialXView/source/index.js +++ b/javascript/MaterialXView/source/index.js @@ -91,10 +91,10 @@ function init() viewer.getScene().setUpdateTransforms(); }) - // Add hotkey S to save the contents of the rendered canvas to a file. + // Add hotkey 'f' to save the contents of the rendered canvas to a file. // See check inside the render loop when a snapshot can be performed. document.addEventListener('keydown', (event) => { - if (event.key === 's') { + if (event.key === 'f') { snapshot = true; } }); From fbd7f160c2e42dfe53ddadd65693851d8143baf2 Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Wed, 17 Jan 2024 13:17:09 -0800 Subject: [PATCH 4/6] Update function and variable names --- javascript/MaterialXView/source/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/javascript/MaterialXView/source/index.js b/javascript/MaterialXView/source/index.js index 6ab837468a..b0667ba86b 100644 --- a/javascript/MaterialXView/source/index.js +++ b/javascript/MaterialXView/source/index.js @@ -21,7 +21,7 @@ let turntableEnabled = false; let turntableSteps = 360; let turntableStep = 0; -let snapshot = false; +let captureRequested = false; // Get URL options. Fallback to defaults if not specified. let materialFilename = new URLSearchParams(document.location.search).get("file"); @@ -34,7 +34,7 @@ init(); viewer.getEditor().updateProperties(0.9); // Save the contents of the rendered canvas to a file. -function performSnapshot() +function captureFrame() { let canvas = document.getElementById('webglcanvas'); var url = canvas.toDataURL(); @@ -99,10 +99,10 @@ function init() }) // Add hotkey 'f' to save the contents of the rendered canvas to a file. - // See check inside the render loop when a snapshot can be performed. + // See check inside the render loop when a frame capture can be performed. document.addEventListener('keydown', (event) => { if (event.key === 'f') { - snapshot = true; + captureRequested = true; } }); @@ -181,10 +181,10 @@ function animate() composer.render(); viewer.getScene().updateTransforms(); - if (snapshot) + if (captureRequested) { - performSnapshot(); - snapshot = false; + captureFrame(); + captureRequested = false; } } From d766fb28b9ed2b574bd60fda3feec131749b4e11 Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Wed, 17 Jan 2024 13:19:50 -0800 Subject: [PATCH 5/6] Update comments --- javascript/MaterialXView/source/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/javascript/MaterialXView/source/index.js b/javascript/MaterialXView/source/index.js index b0667ba86b..c3d62551ec 100644 --- a/javascript/MaterialXView/source/index.js +++ b/javascript/MaterialXView/source/index.js @@ -33,7 +33,7 @@ let viewer = Viewer.create(); init(); viewer.getEditor().updateProperties(0.9); -// Save the contents of the rendered canvas to a file. +// Capture the current frame and save an image file. function captureFrame() { let canvas = document.getElementById('webglcanvas'); @@ -98,8 +98,8 @@ function init() viewer.getScene().setUpdateTransforms(); }) - // Add hotkey 'f' to save the contents of the rendered canvas to a file. - // See check inside the render loop when a frame capture can be performed. + // 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; From d0735d457b7e044b139ead95fdea6f68f93c2e41 Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Wed, 17 Jan 2024 13:26:26 -0800 Subject: [PATCH 6/6] Remove added whitespace --- javascript/MaterialXView/source/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/MaterialXView/source/index.js b/javascript/MaterialXView/source/index.js index c3d62551ec..d46bd2350f 100644 --- a/javascript/MaterialXView/source/index.js +++ b/javascript/MaterialXView/source/index.js @@ -96,7 +96,7 @@ function init() orbitControls = new OrbitControls(scene.getCamera(), renderer.domElement); orbitControls.addEventListener('change', () => { viewer.getScene().setUpdateTransforms(); - }) + }) // 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.