creating visualizations on the canvas with raw UInt16Array data in JavaScript

I'm attempting to display an image from a Uint16Array using Javascript. Despite not encountering any errors, the canvas remains blank.


const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const bufferMemoryAllocation = cellCount*2;
const bitArray = new SharedArrayBuffer(bufferMemoryAllocation);
const pixelData = new SharedArrayBuffer(bufferMemoryAllocation);
const pixelDataCanvas =  new Uint16Array(pixelData);

const videoResolutions = [{name: "test", height: 1000, width:1000},{name:"1080", height: 1080, width:1920},{name:"4k", height: 2160, width:3840}];
canvas.height = videoResolutions[2].height;
canvas.width =  videoResolutions[2].width;

const cellScale = 2;
const drawHeight = Math.floor(canvas.height/cellScale);

const drawWidth = Math.floor(canvas.width/cellScale);
const cellCount=drawHeight*drawWidth;




function renderToCanvas()
    {
    ctx.drawImage(renderToImage(pixelDataCanvas),0,0,drawWidth,drawHeight); 
}
function renderToImage(pixelDataReffernce)
    {let imageObject = new Image(canvas.height,canvas.width);
    let imageString = 'data:image/bmp;base64,'+btoa(pixelDataReffernce);
    imageObject.src = imageString;
    // console.log(imageObject);
    return imageObject;
}

pixelDataReffernce consoles out to::

  Uint16Array(2073600) [0, 0, 0, 0, 0, 0, 0, 0, 1024, 1024, 1024, 0, 0, 0, 0, 1024, 0, 0, 0, 1024, 1024, 1024, 0, 0, 0, 0, 0, 1024, 0, 0, 0, 0, 1024, 0, 1024, 1024, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1024, 0, 0, 0...

EDIT_

The issue was that I was utilizing a binary channel for color instead of a 4-channel color array. The fix involved extending the size of the recorded color to 4 bytes while viewing it as 1 byte on the main thread. However, trying to access the SharedMemory with
new Uint8ClampedArray(data.buffer) resulted in the following console warning.

Uncaught TypeError: Failed to construct 'ImageData': The provided ArrayBufferView value must not be shared.

This led me to create a temporary array on the main thread;

    const pixelTransViewer = new Uint8Array(pixelData);
    const tA = new Uint8ClampedArray(data.buffer);
    for(let i=0;i<tA.length;i++)
        {   
        for(let j=0;j < 8;j++)
            {
            tA[i]=setBitState(tA[i],j,checkBit(pixelTransViewer[i],j));
        }
    }
    const img = new ImageData(tA, span);

Unfortunately, this process essentially duplicates the information from sharedMemory to a new memory slot for every rendering cycle. Is there a more efficient way for me to extract the pixelData from sharedMemory and transfer it to the canvas?

Answer №1

If you have raw RGBA pixel data, a simple solution is to create an ImageData instance from your Uint16Array and then place it on your canvas.

const width = 50;
const height = 50;
const pixels = new Uint16Array((width * height) * 2);
// add some randomness
crypto.getRandomValues(pixels);

const canvasElement = document.getElementById('canvas');
canvasElement.width = width;
canvasElement.height = height;
const context = canvasElement.getContext('2d');

const image = new ImageData(
  new Uint8ClampedArray(pixels.buffer),
  width,
  height
);
context.putImageData(image, 0, 0);
<canvas id="canvas"></canvas>

Your code might not have shown any errors because you weren't properly handling them. By attaching a listener to the error event of your imageObject, you would have noticed that it failed to load the content due to missing headers specifying the image size.

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

What could be the reason jqGrid is not making multiple Ajax calls when the OnSelectRow Event is triggered repeatedly?

Encountering an issue with my two jqGrid grids. The first grid operates smoothly, with data and a loadComplete event that automatically selects the first row. This selection triggers the population of the second jqGrid based on the selected row (id) from t ...

Improving the Efficiency of JavaScript/jQuery Code

Currently, I am delving into JavaScript and jQuery. Here is the code snippet that I am working on: $("#hrefBlur0").hover(function() { $("#imgBlur0").toggleClass("blur frame"); }); $("#hrefBlur1").hover(function() { $("#imgBlur1").toggleClass("blur fra ...

Connect with Friends - Using Express, MongoDB, and EJS for a Seamless Friend Connection

I have been working on creating a social network that allows users to send and interact with friend requests. Currently, I have completed the registration, log-in, and "search for other users" functions. Once I find and select another user, I am able to d ...

What is the best method for removing a class with JavaScript?

I have a situation where I need to remove the "inactive" class from a div when it is clicked. I have tried various solutions, but none seem to work. Below is an example of my HTML code with multiple divs: <ul class="job-tile"> <li><div ...

How can React and Redux ensure that response data is accessible to every component?

When using react and redux, how can data written in the useDispatch function be made available in other components? Additionally, how can the customerId be accessed in all components? I have created a code that calls an API and returns data in response. I ...

Tips on positioning content beneath a fixed header or navigation bar when viewed in a web browser

Hi, I'm having an issue with creating a fixed header using HTML and CSS. When I set my header to be in a fixed position, it covers up the content below it. I want the content to be positioned under the header when the page is loaded. Additionally, I&a ...

Executing a javascript function numerous times

Here are a few examples: <div class="statement">text goes here</div> <div class="response"><input type="text" id="amount1" style="border: 0; color: #f6931f; font-weight: bold;" /></div> <div id="sli ...

Exploring date comparison in AngularJS

I've encountered an issue while using ng-show in a page that I'm currently designing: <td ng-show="week.EndDate > controller.currentDate"> The week object has a property called EndDate, and the value of currentDate is being set in my c ...

Identify the quantity of dynamically added <li> elements within the <ul> using jQuery

I'm facing an issue where I need to dynamically add a list of LI items to a UL using jQuery. However, when I try to alert the number of LI elements in this list, it only shows 0. I suspect that it's because the code is trying to count the origina ...

I'm unable to resolve the issue regarding the message "Property or method is not defined on the instance but referenced during render."

I have a good grasp on what the issue is (the inputs I'm trying to v-model aren't declared), but for some reason, I can't resolve it (or figure out how to) even after studying other posts with the same problem. After comparing my code with ...

Encountered a connection error while attempting to establish a connection in Node.js

Every time I try to execute the code, I encounter this error message: throw new Error('Most middleware (like ' + name + ') is no longer bundled with express and must be installed separately ^ Error: Most middleware(like BodyParser) is ...

importing files from Uploadcare using ngCordova MediaFile

I am facing an issue while attempting to upload a sound file from ngCordova's $cordovaCapture service to UploadCare. The `uploadcare.fileFrom('object')` function is failing with an 'upload' error even though I have set the public k ...

Implement a time interval in a recurring jQuery / Ajax operation

I'm attempting to introduce a delay into a repeating query. After some research, I've discovered that .delay isn't the right approach. Instead, it's recommended to use either setInterval or setTimeout. However, my attempts with both me ...

Utilizing a switch case for typing

I am working on a React component that takes in a list and a type as props. The list is an array of objects, while the type is an optional enum string. Inside this component, there is a function that uses a switch case statement to enforce a specific type ...

Why Promise.all() isn't working as expected - where am I going wrong?

Currently, I am in the process of developing a script that utilizes the GitHub API. The script includes a function that takes a list of usernames as input. Each username triggers an API request to fetch that user's starred repositories. For each starr ...

Is the website failing to load on certain IOS devices? Could it be experiencing the dreaded "white screen

Issue Description I have developed an application that is hosted at . Unfortunately, the app does not render correctly on certain IOS devices. I have tested it on my iPhone 5 running IOS 10.3.3 and a friend's iPad with unknown iOS version. The page s ...

Can you help me understand how to ensure the CSS translate feature lands in a specific div, no matter its initial position?

I'm facing a roadblock in my journey to create a card game. The issue arises at the final stage of the Translate function implementation. In this game, the player is dealt 30 cards (I've simplified it to four for ease of programming), and upon cl ...

Is there a way to ensure the req.body retrieved from a form is not undefined?

Every time I submit a form with information, the response comes back as undefined. I have provided the code below for reference. If I include the (enctype="multipart/form-data") in the form, I do not receive any data in the body (req.body). However, if I e ...

Enhanced memory allocation for JavaScript clients

Creating a JavaScript script that demands significant amounts of RAM, possibly around 100MB. I need to generate a large array on the client-side. Is there an HTML tag available to allocate more memory for the script? Code: <html> <head> ...

Tips for accessing the current state/value in a third-party event handler?

Consider the following scenario: function MapControl() { const [countries, setCountries] = useContext(CountriesContext) useEffect( () => { ThirdPartyApi.OnSelectCountry((country) => { setCountries([...countries, country]) }) }) ...