An issue with the image filter function in JavaScript

I am currently working on a simple application that applies image filters to images. Below is the code I have written for this purpose.

class ImageUtil {

static getCanvas(width, height) {
    var canvas = document.querySelector("canvas");
    canvas.width = width;
    canvas.height = height;
    return canvas;
}

static getPixels(image) {
    var canvas = ImageUtil.getCanvas(image.width, image.height);
    var context = canvas.getContext('2d');
    context.drawImage(image, 0, 0);
    return context.getImageData(0, 0, canvas.width, canvas.height);
}

static putPixels(imageData, width, height) {
    var canvas = ImageUtil.getCanvas(width, height);
    var context = canvas.getContext('2d');
    context.putImageData(imageData, 0, 0);
}

}

function getRandomNumber(minimum, maximum) {
    return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

...

function applyRedFilter(image, adjustment){

 var pixels = ImageUtil.getPixels(image);
 var length = pixels.data.length;
 var data = pixels.data;

 for(var index = 0; index < length; index += 4) {
  data[index] += adjustment;
 }
 ImageUtil.putPixels(pixels, image.width, image.height);
}

...

$(document).ready(function() {
 var catImage = new Image();
 catImage.src = "img/cat.jpg";
 applyRedFilter(catImage, 50);
});

Upon launching the webpage, Chrome displays an error message which can be viewed at this link.

This error seems puzzling considering there is only a semicolon at the end of the function.

If you have any solutions to resolve this issue, please let me know. The top image does not show any visible change after applying the filter as shown in these two cat pictures.

Answer №1

The reason for the error is a common mix-up of using . instead of ,:

ImageUtils.putPixels(pixels, img.width. img.height);

Correct form:

ImageUtils.putPixels(pixels, img.width, img.height);

It is advised not to solely rely on StackOverflow for syntax checks, feel free to seek help here for genuine issues.

Answer №2

There seems to be a small error in your code, you have written:

ImageUtils.putPixels(pixels, img.width. img.height);

The correct syntax should include a comma after 'width' instead of a period.

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

Learn the process of adding asynchronous middleware modules to your express.js application

I'm currently developing an express application that utilizes the node_acl module along with a MongoDB database. I have created a custom module that sets up and configures node_acl asynchronously. This middleware needs to be called as the second piece ...

Activate jQuery function upon clicking a bootstrap tab

When I click on a Bootstrap tab, I want to trigger an AJAX function. Here is the HTML code: <li><a href="#upotrebljeni" data-toggle="tab">Upotrebljeni resursi</a></li> The jQuery AJAX function looks like this: $('a #upotreb ...

Adjusting or cropping strokes in a JavaScript canvas

I am working with a transparent canvas size 200x200. The object is being drawn line by line on this canvas using the solid stroke method (lineTo()). I'm in need of making this object full-width either before or after ctx.stroke();. ...

Having an issue while running the command "npm start" in Visual Studio with React.js

PS C:\Users\PC1\Downloads\portfolio_website-STARTER> npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0d120f091b12111412220a181f0e140918500e091c0f09180f3d4c534d534d">[email protect ...

How to display a div in Angular when hovering with ElementRef and Query List

I am having trouble implementing a ngFor loop in my project where I want to display a div on mouse hover using the @Input notation. This is how my HTML code looks: <div class="col s12 m6" style="position: relative" *ngFor="let res of hostInfo.resident ...

How can I automatically direct my NodeJS application to the login screen?

Here is the setup of my project structure: There is a simple folder called static, within which I have: index.html (the homepage for user registration) login.html (the login page) In the parent folder, there is a file named server.js: // NodeJS code for ...

Calculate the total number of blank input boxes within a specific row of the table

What is the method to count the number of input boxes without a value in a table row using jquery? For instance: <table id="table1"> <tr class="data" id="row5"> <td><input type="text" value="20%" /></td> <td><input ...

I'm finding it difficult to grasp the purpose of $inject within controllers

I'm feeling completely lost when it comes to understanding inject in Angular. I can't seem to grasp where it should be utilized and its purpose. Is it specifically tied to factory methods, as outlined here? myController.$inject = ['$scope&a ...

Gulp is failing to convert SCSS files into CSS

I'm having trouble getting gulp to compile my SASS code into CSS automatically. What could be the issue? file structure: public -css --styles.css -index.html sass -styles.scss gulpfile.js package.json Gulpfile: var gulp = require('gulp') ...

The custom tab component in React is currently not accepting the "disabledTabs" prop

I have designed a tab component as shown below: tab/index.jsx import React from 'react'; import TabHeader from './header'; import TabBody from './body'; import TabHeaderList from './header/list'; import TabBodyList ...

Can we address certain data before the $stateChangeStart event is triggered?

I have been working on creating a custom Role-Permissions system that I want to set up during the initial root state resolve: $stateProvider .state('common', { resolve:{ user: function(AclService, UserService) { UserService. ...

What is a reliable method to retrieve the text from the current LI if all LI elements in the list share the

I'm encountering an issue with retrieving the text from LI elements because I have 10 list items and they all have the same class name. When I attempt to fetch the text using the class name or id, I only get the text of the last item. Here is my code ...

Transmitting a pair of data points to PHP using ajax POST for querying the SQL database

I am attempting to send two values from a form to another PHP file using the ajax post method. One value is the current input box value, while the other is the value being typed into a different input box which functions as a search box. When I execute the ...

Confusion arises from conflicting Vue component script indentation guidelines

As I work on setting ESLint rules for my new Vue project, I am extending both eslint-plugin-vue and airbnb. All is well except for one issue - the indentation of the tag inside Vue components. The usual accepted format looks like this: <script> ex ...

Analyzing JSON parsing efficiency

I am currently working on a client application using jquery and html5 For my project, I have a data file that is 70 MB in size The file named data.json contains the following: var myData = [ { "id":"000000001", "title":"title1", "c ...

After retrieving a value from attr(), the object does not have the 'split' method available

I need to implement the split method on a variable fetched using attr. This is the code snippet I am attempting: $(document).ready(function() { $('.some_divs').each(function() { var id = $(this).attr('id'); var ida = ...

What is the most straightforward method to convert a current Express node app into a static site?

After primarily working with React and create-react-app, I've grown accustomed to the convenience of having a build utility for npm that simplifies deploying projects to static web hosting platforms like github pages or surge. This process ultimately ...

Tips for increasing the number of pixels in the current position of an element?

I need to shift an image to the right by adding pixels to its current left position. The challenge arises when the image, positioned absolutely, goes back to its nearest relative parent (the container) and creates a strange visual effect. Within my flex c ...

Vuejs method to showcase input fields based on form names

I am working on a Vue.js form component with multiple input fields, but now I need to split it into two separate forms that will collect different user input. Form 1 includes: Name Surname Email with a form name attribute value of form_1 Form 2 i ...

Conceal rows in the table until the search (filterBy) is completed

Currently, I am utilizing Vue.js version 1.x and plan to update soon. My requirement is to have a table of rows containing some data. To filter the table rows using filterBy, I am using a Vue.js component. However, I want the table rows to remain hidden un ...