Kineticjs is not performing up to par

I apologize if this question has already been asked, but I haven't been able to locate it.

I am working on a project that involves a canvas displaying approximately 400-500 rectangles, each ranging in height and width from 20-30 pixels. The goal is to have each rectangle move one pixel to the left and up upon mouseover, and return to its original position on mouseout to create a selected effect. While my code functions well with a small number of shapes, it experiences significant slowdown when dealing with 500 of them. I came across an example online that suggested creating an "animation layer" to move the objects that need to be animated. However, this approach still requires redrawing the main layer to erase the previously moved item... Below is the code snippet:

 // Insert KineticJS code here

And this is the code responsible for rendering the canvas:

 // Insert HTML and script code for rendering the canvas here

I have a feeling that I am missing something crucial here, but my knowledge of KineticJS is limited. Could someone provide guidance on the correct approach?

Thank you in advance, Danny

Answer №1

If you haven't already, be sure to take a look at KineticJS v4.0.0. The event detection engine has been completely revamped to provide lightning-fast hit detection, even when dealing with a large number of shapes:

Answer №2

This challenge turned out to be quite interesting. Initially, I believed the issue lied within the shape events, but the enhancements made there were minimal.

In KineticJS, the events are applied to specific on-screen shapes by searching through the coordinates of the models until a match is found with the current mouse coordinates. By having only one layer, we increase the size of the array to search, which can be inefficient.

The solution was to implement multiple layers. I introduced a layer for each row in order to optimize the search process.

Some of the key changes made in the code are:

  1. Avoid moving shapes between layers to prevent array splicing and index shifting.
  2. Utilize the relative move(x,y) method over the setX() and setY() methods for small shifts.
  3. The animation layer concept described in tutorials is meant for actual animations on timed intervals; what we are doing here is just a normal shape movement on an event trigger.
  4. Implemented a minor JavaScript enhancement; when returning an object to conceal private data and methods, refrain from using new to create an object.

Check out the code snippet below for the implementation:

<script>
    $(function () {
        // Code implementation here
    });
</script>

Another portion of the code:

More code implementation here

Answer №3

Creating 500 shapes can have that impact. The speed of your computer and the browser you use (Chrome is currently the quickest) will also play a role in performance, although you'll want satisfactory performance on all browsers.

For more information on performance and tips for enhancing it, visit this link.

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

In Node.js, use the `[]` operator to select elements from an array of strings without including

In my situation, I am working with an array of strings which can sometimes be an array containing only one string. The issue is that when this happens, using array[0] to retrieve the value does not return the entire string but rather just the first charact ...

In JavaScript, an HTTP request file includes a JavaScript variable

I am currently working on a Node.js project that involves making an HTTP request to fetch a JavaScript file. For example, let's say we have a file named a.js: var a = 'i am a.js'; var b = 'please convert me to js'; Here is the a ...

Interactive bar chart that updates in real-time using a combination of javascript, html, and

Current Situation: I am currently in the process of iterating through a machine learning model and dynamically updating my "divs" as text labels. My goal is to transform these values into individual bars that visually represent the values instead of just d ...

Stop Code Execution || Lock Screen

Is there a way to address the "challenge" I'm facing? I'm an avid gamer who enjoys customizing my game using JavaScript/jQuery with Greasemonkey/Firefox. There are numerous scripts that alter the DOM and input values. In my custom script, I hav ...

Transforming varied JavaScript objects into a serial form

In my application, there is a concept of an interface along with multiple objects that implement this interface in various ways. These objects are created using different factory methods, with the potential for more factories to be added as the application ...

What is the method for obtaining a date in the format of 2018-05-23T23:00:00.000+00:00?

Recently, I've been attempting to filter data based on dates in my database. The format in which the dates are saved is as follows: 2018-05-23T23:00:00.000+00:00 This was my initial approach: router.get('/byDate', function (req, res) { ...

Displaying subtotal in a list using Vue.js and conditional rendering with v-if statement

Seeking guidance on calculating a total for a vue.js list that contains invoice items. To illustrate, let's consider a scenario where a table of invoice items is being rendered. Here is the code snippet: <table> <template v-for="(invoice_ite ...

Click on the button to be directed to a different page using React

<div class="clickable-icon"> <button component={Link} to={"/create"}> <EditIcon></EditIcon> </button> </div> I have written this code snippet to turn an icon into a clickable button that directs ...

Experimenting with d3 using Node.js, JSDOM, and Jasmine

I am new to Javascript and node.js, and I want to test if a basic bar chart is being rendered in my node.js/d3 application using jasmine. Could someone provide guidance on what steps I need to take? test.js (file that needs testing): var d3 = require(&ap ...

Node.js: How to handle spaces when running a UNIX command

Currently, I am utilizing the following command to compress files in node.js: var command = '7z a ' + dest + ' ' + orig; exec( command, function(err, stdout, stderr) { ...}); An issue arises when a file containing spaces is involved, ...

Is it possible to modify a portion of the zod schema object according to the value of a

My form consists of several fields and a switch component that toggles the visibility of certain parts of the form, as shown below: <Field name="field1" value={value1} /> <Field name="field2" value={value2} /> &l ...

Unable to run JavaScript file fetched from cache API

For a web application built using pure vanilla JavaScript without utilizing service workers, I am looking to cache a JavaScript file hosted on an AWS S3 file server explicitly. The script below will be embedded in the index.html file of the application (UR ...

Developing a new class within a function

In this section of the HTML file, I have set up a form to collect email and password information from new users in order to create a new profile: <form name="userInfo"> <fieldset> <legend>Create a new account</legend> ...

"Patience is key when it comes to waiting for components to render

Short Overview of the Issue I'm currently exploring methods to access an onRendered lifecycle hook. Finding on the Topic A similar query was posted here: Vue $nextTick in mounted() hook doesn't work as expected The explanation provided suggests ...

A guide on dynamically checking the checkbox values in each row of a table using JavaScript or jQuery

My table is dynamically populated with values from a database using the following code: var row = table.insertRow(i); i = i+1; // Insert new cells (<td> elements) at the 1st and 2nd position of the new <tr> element: var cell1 = row.insertCell ...

Tips for concealing labels until the submit button is activated

I am currently handling a project involving a Spring Controller and JSP. Within a JSP page, I have included a button labeled Process. Upon clicking this button, two radio buttons are displayed below it along with a Submit button. After tapping the Submit ...

Using incorrect string as JavaScript argument

Currently, I am facing an issue with passing parameters elegantly in my code. I have a link for deleting items which looks like this: <a href="javascript:confirmDelete('${result.headline}', ${result.id});"> The problem occurs when the res ...

What is the best way to merge angularjs modules?

In my angularjs application using the codeigniter PHP framework, I have implemented 3 modules - cars.js for car details, cellphones.js for cellphone details, and home.js for home details. Each module caters to a different client's needs. I am trying ...

What is the best way to include multiple action creators in a single listenerMiddleware in Redux Toolkit?

I am looking to store the current state in my database every time there is a change in any of its properties. I have implemented two middlewares to handle this task, each responsible for dispatching the saveTrip function. Although both middlewares are ide ...

Encountered an error in React JS and Axios when attempting to select a form: "Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'."

I'm encountering an issue while attempting to send FormData using Axios within a React JS application. The form should include both text inputs and multiple file uploads. However, when I tried to select the form with querySelector, it doesn't app ...