Emscripten: Showcasing an image within an HTML document

I have successfully compiled a JS program from C++ files using emscripten, and it generates a PNG file.

The program is now being loaded and run in an HTML file.

However, I am facing a challenge in displaying the generated PNG file within this HTML file. Emscripten utilizes an emulated sandboxed filesystem, and my PNG file ends up being stored in this filesystem.

What is the best way to retrieve this file and display it in the HTML file?

Answer №1

To access the file, you can utilize the emscripten's File System API.

Assuming you have a C++ function that provides the path to your PNG file, it may look something like this:

std::string processImage()
{
    std::string filename = "my.png";
    // Do something with the file
    return filename;
}

In your HTML file, you can use JavaScript to read the file, convert it to a blob, and then create a URL for it:

const filename = Module.processImage(); // Call your C++ function
const blob = new Blob([FS.readFile(filename)], {'type': 'image/png'});
const url = URL.createObjectURL(blob);
// You can set this URL as the src attribute of an img tag

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Arrange DIV elements sequentially with HTML, JQuery, and CSS

Live Demo: Live Demo HTML: <div class="target"> <img src="bg-clock.png" alt="jQuery" /> </div> <div class="target2"> <img src="bg-clock.png" alt="jQuery" /> </div> CSS: .target, .target2 { ...

Incorporating OpenCV into an iOS application

After following the Hello World tutorial for OpenCV from the link provided below, I encountered an O Linker error. Is there something I am missing? When I try to add OpenCV to my project, I get the following error messages for architecture armv7: - "s ...

Steady Navigation Bar in Javascript without Bouncing

While experimenting with a fixed navigation bar, I've encountered an issue where the content below jumps up on the page when the navigation bar "fixes" itself to the top of the screen. You can check out the JSFiddle I've been working on for refer ...

Node.js is receiving an empty body from Postman when using form-data in the request

Even though I've searched extensively, I still have not found a solution to my specific issue despite it being asked before. const express = require("express"); require("dotenv").config({ path: ".env", }); const PORT = ...

Re-rendering components using arrow functions

Using an arrow function, I have implemented a feature to toggle a div and show/hide a button based on the div's visibility. toggleDeliveryDiv = () => { document.getElementById('btn_collapse_delivery').click(); this.s ...

Prop type failure: The `actions` prop is specified as mandatory in the `Testing` component, however, its value is currently undefined

I am working on a project that involves creating a login form using React and Redux. Here's a snippet of my app.js: import React from 'react'; import { render } from 'react-dom'; import Input from 'react-toolbox/lib/input&apo ...

How to return a variable to Express when clicking a button?

Imagine having a list of 10 elements, each with a unique id as their name, and the user has the ability to add more items to the list at any time. If I were to click on an element, my goal is to have Express retrieve the id of that specific element. If it ...

The index on ref in MongoDB does not seem to be yielding any improvements

I've encountered a performance issue with my model: const PostSchema = new mongoose.Schema( { _id: mongoose.Schema.Types.ObjectId, workSpace: { type: mongoose.Schema.Types.ObjectId, ref: 'Workspace&apos ...

Encountering a problem with populating data in postgresql

After executing the npm run seed command to populate data into PostgreSQL, even though the seeding process seemed to be successful, I couldn't locate the seeded data in the PostgreSQL database. Can anyone advise on what might have gone wrong or sugges ...

Real-time functionality is not supported by Firebase functions

I've set up a firebase query within a method in VueJS: data: {this.todaysEvents}, methods : { getTodaysEvents (day) { this.todaysEvents = [] const events = db.ref('calendar') const query = events.orderByChild('da ...

``It seems like there was an error with WebComponents and NextJS - the hydration failed due to a mismatch between the initial UI and what was rendered on

I'm running into an issue with the following error message: Error: The initial UI doesn't match what was rendered on the server, leading to hydration failure. This problem occurs when I have a NextJS webpage that includes StencilJS web compone ...

Tips for executing a callback function when triggering a "click" event in jQuery?

Having trouble triggering a custom event in the callback of a trigger call. Attempted solutions: var $input = $( ".ui-popup-container" ).find( "input" ).eq(2); function runtests () { console.log("clicked the input"); }; $input.trigger('click&ap ...

An issue has occurred while trying to execute the npm start command within PhpStorm

When I try to run npm start on Windows' command prompt, it works fine. However, I encounter errors when running it in the PhpStorm Terminal. You can see the errors here. Is this a result of them being different platforms or could it be related to som ...

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

Can we consider this function(operator overloading) to be safe for use with multiple threads

Take a look at this snippet of code: ElementType& customOperator[] (int key) { // Using a custom type of lock, similar to boost::mutex::scoped_lock CustomLockType lock(); if(key < 0 || key > m_bound) throw std::range_error ...

In React js, I wanted to display the animation specifically on the "add to bag" button for the added item

When I click the "add to bag" button, all other buttons also display the animation. How can I make sure that only the clicked button shows the animation? Any suggestions? <Table responsive> <thead> <tr> ...

Before beginning my selenium scripts, I need to figure out how to set an item using Ruby in the browser's localStorage

Before running my selenium scripts, I am attempting to store an item in the browser's localStorage. I attempted to clear the local storage using this command: driver.get('javascript:localStorage.clear();') This successfully cleared the lo ...

Ways to generate data following the integration of Firebase Firestore in Vue.JS

How can I display the orders data retrieved from Firebase in my browser console? See the image link below for reference. https://i.sstatic.net/HPlaC.png This is the code snippet for fetching data from Firebase and displaying it in the console: orders(){ ...

JQuery code causing issue with properly closing toggle menu

I am currently working on implementing a closing toggle menu for mobile devices and facing a minor issue. What I aim for is to allow users to close the toggle menu by tapping anywhere on the screen of their device while it is active. I have almost achieved ...

Is there a way to use Javascript to detect if the value of a control on an ASP.Net page has been modified?

Within my web page, I have integrated a FormView that transitions to Edit mode when the user clicks the edit button. To enhance user experience, I have implemented a JavaScript onbeforeunload function that triggers a confirmation dialog if the user tries t ...