SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIOBrowserPopup.cpp
Go to the documentation of this file.
1 /**
2  * \file SLIOBrowserDisplay.cpp
3  * \date October 2022
4  * \authors Marino von Wattenwyl
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <SLIOBrowserPopup.h>
11 #include <Utils.h>
12 #include <cassert>
13 
14 #ifdef SL_STORAGE_WEB
15 //-----------------------------------------------------------------------------
17  : SLIOWriterMemory(path)
18 {
19  assert(Utils::endsWithString(path, ".png") && "SLIOWriteBrowserDisplay only supports PNG files");
20 }
21 //-----------------------------------------------------------------------------
23 {
25 }
26 //-----------------------------------------------------------------------------
28 {
29  std::vector<char>& buffer = SLIOMemory::get(_path);
30  const char* data = buffer.data();
31  size_t length = buffer.size();
32 
33  std::string filename = Utils::getFileName(_path);
34 
35  // clang-format off
36  MAIN_THREAD_EM_ASM({
37  let path = UTF8ToString($0);
38  let section = HEAPU8.subarray($1, $1 + $2);
39  let array = new Uint8Array(section);
40  let blob = new Blob([array], {"type": "image/png"});
41  globalThis.snapshotURL = URL.createObjectURL(blob);
42 
43  let link = document.querySelector("#snapshot-download");
44  link.href = snapshotURL;
45  link.download = path;
46 
47  document.querySelector("#snapshot-image").src = snapshotURL;
48  document.querySelector("#snapshot-overlay").classList.add("visible");
49  }, filename.c_str(), data, length);
50  // clang-format on
51 }
52 //-----------------------------------------------------------------------------
53 #endif
SLIOWriterBrowserPopup(std::string path)
SLIOStream implementation for reading to memory.
Definition: SLIOMemory.h:43
std::string _path
Definition: SLIOMemory.h:52
std::vector< char > & get(std::string path)
Definition: SLIOMemory.cpp:24
void clear(std::string path)
Definition: SLIOMemory.cpp:34
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
Definition: Utils.cpp:580
bool endsWithString(const string &container, const string &endStr)
Return true if the container string ends with the endStr.
Definition: Utils.cpp:357