Combining two image data objects concurrently

My goal in my canvas application is to create a high-pass filter. The idea is to subtract the regular image pixels from the blurred image pixels with a radius of x. It sounds straightforward, doesn't it?

var d = pixel.data;
var blurdata = blur(amnt, pixel);
var bd = blurdata.data;
for (var i=0; i<d.length; i+=4) {
    d[i] = 128+(d[i]-bd[i]);
    d[i+1] = 128+(d[i+1]-bd[i+1]);
    d[i+2] = 128+(d[i+2]-bd[i+2]);
}
return pixel;

This code requires two parameters: amnt and pixel. Pixel refers to the ctx.getImageData object, while amnt represents the blur radius. Although the blur function works perfectly as intended, the issue lies in the similarity between the d and bd variables. I'm puzzled about why this is happening. Subtracting bd[i] from d[i] results in 0, leading to a completely gray image when the data is reinserted into the canvas. Additionally, the dimensions of blurdata and pixel data are identical.

I would greatly appreciate any assistance.

Here is the link to the Blur function:

Answer №1

It is essential to note that the data remains consistent simply because it stays unchanged. The process of blurring pixel.data within the blur function alters the values in the array d. To address this issue, consider creating two canvases with identical images. Let d represent the pixel data for the unblurred image and then apply the blur effect to the other image:

let composite = canvas1.getContext("2d").getImageData();
let d = composite.data;
let blurdata = blur(amount, pixel);
let bd = blurdata.data;

for (let i=0; i<d.length; i+=4) {
    d[i] = 128+(d[i]-bd[i]);
    d[i+1] = 128+(d[i+1]-bd[i+1]);
    d[i+2] = 128+(d[i+2]-bd[i+2]);
}

return composite;

This adjustment should effectively resolve the issue at hand.

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

The body of the POST request appears to be void of any

Whenever I make a request using curl or hurl, an issue arises. Despite req.headers['content-length'] showing the correct length and req.headers['content-type'] being accurate, req.body returns as {}. Below is the Hurl test: POST http:/ ...

What is the best way to create CSS rules that can be dynamically applied as classes using JavaScript?

I have been attempting to use the addClass function in JavaScript to add a class, but I am struggling to make it work. Is there a specific way to define a class that will be added to an element when clicked on? Here is what I have attempted: var input = ...

Avoiding conflicts between banners, text, and images for HTML/CSS design

I'm having an issue with the banner I created for my project. It seems to be overlapping with text and images, hiding behind them. How can I fix this? Unfortunately, I can't post the link to my project here due to other files present. The specif ...

Create a shape using a series of points without allowing any overlap between the lines

JS fiddle There is an array of coordinates that gets populated by mouse clicks on a canvas. var pointsArray = []; Each mouse click pushes x and y values into this array using a click event. pointsArray.push({x: xVal, y: yVal}); The script iterates thr ...

The functionality of selecting all items in v-select does not result in saving all of them

I'm currently working on integrating a select all button into the v-select component of Vuetify. The issue I am facing is that even though I can use the button to select all options, when I attempt to save, not all items are saved. However, if I manua ...

Using Material UI's ProgressBar to create a countdown feature in a React

I am working on a feature where pressing a lock button changes the isReserved status of an item to true. This action also creates two new states in Firebase Firestore, namely startDate and expiryDate. The expiryDate is set to be 72 hours after the startDat ...

The JavaScript counterpart to jQuery's click event handler

I'm trying to figure out how to achieve the same effect as this jQuery code. var divQuery = $('.html5gallery-thumbs-0').children(); divQuery.on('click', function () {...} I attempted it like this: var divQuery = document.g ...

Providing Node-server with Vue.js Server Side Rendering

I've been attempting to deploy the Hackernews 2.0 demo on my Digital Ocean droplet but have hit a roadblock. npm run start starts the server on port :8080. npm run build is used for production builds. The specific build tasks can be found below: ...

Dropdown Selection Index not functioning properly post jQuery implementation

Upon pageload, I populated a dropdown with options using the following code: DropDownList1.Items.Insert(0, new ListItem("Add New1", "1")); DropDownList1.Items.Insert(1, new ListItem("Add New2", "2")); DropDo ...

Guide on implementing text/ng-template script section in jsfiddle

My current challenge involves creating a jsfiddle that utilizes AngularJS to display two calendar controls. While my code functions properly when run locally, I've encountered an issue with including the template code via a script tag on jsfiddle: ht ...

Communicate with the server by uploading a text file containing HTML5 code and utilizing Jquery/Javascript

Is it feasible to utilize Javascript/JQuery and HTML5 to write data to a server and save it as a text file? I am currently working on a game using JavaScript and JQuery, and I need to store the scores without relying on any server-side language like PHP ...

Employing jQuery and/or CSS to increase the size of an image upon hovering while keeping it enlarged when selected

I am looking to enhance the functionality of my website using jQuery or css. My goal is to be able to select one of 3 images and: enlarge the image when hovered over keep the selected image enlarged Below is the code that I currently have. Unfortunately ...

Generate a nested object dynamically, assign a specific value, and continue adding more objects using pairs of keys and values

Struggling as a newcomer to JavaScript with nesting values of key-value pairs in an object? Take the example below with Basis, where you need to add nested keys and allocate related values from Basis. Each new object should be wrapped in an array within th ...

"JavaScript: An In-Depth Guide on Iterating Over Objects Using

I am trying to iterate through the req.body payload object in a nodejs application and extract the array object with SplitType = 'FLAT'. However, when I attempt to loop through the array object, I encounter an error stating that SplitType is unde ...

What is the recommended way to select a checkbox using JQuery?

I have exhausted all options and nothing seems to be working. This is the checkbox in question: <input type="checkbox" onchange="WriteFormSelections('SearchForm', 'AccommodationType', 'AccommodationTypeSelections', 'A ...

Easily integrate an HTML form as an input for your Python script

Despite my thorough searches, I haven't been able to find a satisfactory answer. I am creating a local HTML page with Python and would like to extract data from a form on that page, use it as input for a Python script, and then properly display the r ...

Encountering Uncaught Promise Rejection Warning in Node.js

I can't figure out why I am receiving this error or warning when my code appears to be correct. Below is a snippet of the UserModel that I have been working on: const fs = require('fs'); class UserModel { constructor(filename) { ...

Ways to trigger the React component to refresh when it receives an update via a web socket

Looking to enhance a React JS page featuring a dynamic table. Here are the key requirements: Utilize React Table to load and populate the table data by calling the Data API Establish a web socket connection on the client side to receive real-time updates ...

Issues with AngularJS Scope Variables Stagnating

Weird issue at hand. Check out this form: <form name="newaccount" id="newaccount" ng-submit="doSubmit()"> <label>username</label> <input name="username" type="text" required ng-model="username"> <span ui-toggle="username.length ...

Updating a table cell triggers a change in every cell

I have a table with columns and I need to calculate values in other cells when there is a change event on the table. I want to use ng-change so that the changes are seen immediately. However, I am unsure of how to properly utilize ng-model - if it is used ...