adding content to div is becoming less speedy

Currently, I'm developing a drawing board using only html/css/jquery and the drawing functionality is working smoothly. The approach I've taken involves capturing the mousemove events and placing a dot (div) at each point where the event occurs, then adding dots between those points to form a line.

While this method is effective initially, I have noticed that as I draw more, the process of appending dots to the div slows down, resulting in a noticeable lag in the drawing experience. Even though I am simply doing straightforward appends without any complex document searches for specific elements, the performance is affected.

You can test the functionality on this fiddle: http://jsfiddle.net/u4mn2b84/14/

Could this slowdown be attributed to the increasing number of elements in the document, or is there another factor at play?

Snippet from the code:

$('#sketch-box').on('mousemove', function(e) {
        if (isPerformingLeftMouseClick) {
            var offset = $(this).offset();
            var currentMouseLeft = e.pageX - offset.left;
            var currentMouseTop = e.pageY - offset.top;
            var linkDotTemp = $(linkDot).clone();
            $(linkDotTemp).css('top', currentMouseTop);
            $(linkDotTemp).css('left', currentMouseLeft);
            if (lastMouseTop != null && lastMouseLeft != null) {
                drawLineInBetween(lastMouseTop, lastMouseLeft, currentMouseTop, currentMouseLeft);
            }
            $(this).append(linkDotTemp);
            lastMouseTop = currentMouseTop;
            lastMouseLeft = currentMouseLeft;
        }
    });

Answer №1

Having an abundance of divs on a webpage unnecessarily can lead to performance issues, as the browser becomes overwhelmed trying to render them all. They are not meant to be used in this way.

Even when you use Chrome's element inspector, the developer console can start struggling when inspecting multiple dots.

For a more efficient solution, I suggest utilizing HTML5 canvas instead.

Check out the HTML5 Canvas w3schools page for more information

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

Stop accidental form submissions on iOS devices by disabling the return button

In my Ionic 3 application running on iOS, I encountered a bug that allows users to submit a form even when the submit button is disabled. Despite trying different solutions from this source, I have not been successful in resolving it. To prevent accidenta ...

There is plenty of negative space within masonry design

I'm having trouble configuring a masonry layout for my cards. $('.grid').masonry({ fitWidth: true, horizontalOrder: true, // set itemSelector so .grid-sizer is not used in layout itemSelector: '.grid-item', // us ...

Tips for integrating Chart.js into my AngularJS application?

I am a beginner with AngularJS and I'm currently developing an application on Ubuntu. While trying to add Chart.js using npm install chart.js, an error is being displayed as follows. npm WARN <a href="/cdn-cgi/l/email-protection" class="__cf_emai ...

Creating dependent dropdowns using Laravel Inertia Vue: A step-by-step guide

In my AddressController, I have a function called loadCity along with other CRUD functions: public function loadCities(Request $request) { $provinceId = $request->province_id; $cities = Province::where('province_id' ...

Error 500 encountered during the AJAX request

Here is a snippet from my script: ......................... ......................... data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "POST", dataType: "json", url ...

Fetch response headers not being detected by Web Worker

Currently in my chrome extension, I'm utilizing web workers to collect response header cookies from a specific website. Interestingly, when I execute the request on the main thread, the response contains all the expected cookies. However, when the exa ...

When you click anywhere on the page, JavaScript's method __dopostback is triggered

I'm encountering an issue in my asp.net application where I have an html table. Specifically, when a user clicks on a td element within the table, I use JavaScript to store the id of that td element in a hidden field. Afterwards, I trigger __dopostbac ...

Tips for sending information from PHP to an AJAX function

Hello, I am in the process of learning PHP and I am attempting to retrieve data from a PHP file using the script below. However, I am not receiving any response: $.ajax({ type: 'POST', url: "mark_mod.php", ...

Unlocking the Power of Global Props in AlpineJS: A Step-by-Step Guide

I'm currently exploring AlpineJS after having some experience with React and basic knowledge of Vue. One thing that has puzzled me about AlpineJS is how to update a state value from within a function, similar to how it's done in React. Let' ...

Creating eight equal sections within HTML <div> elements using Bootstrap

I am brand new to Angular. I need to design something like this: https://i.sstatic.net/Zcb9i.png However, I'm struggling to find the right solution. Here is what I have so far: https://i.sstatic.net/7hsrk.png. Having been a backend developer, I&ap ...

What is the process for retrieving document field values for an item in Umbraco 7 backoffice?

I have developed a unique Umbraco 7 dashboard where I aim to retrieve specific field details from instances of a particular document type in my Umbraco CMS. After successfully obtaining a list of all documents of the specified type using entityResource.ge ...

Gather all script tags and place them into an array

Here is a sample markup that I need help with: <p><span style="color: blue;">Yes this one now</span></p> <p><br></p> <p>The main configuration value is this</p> <p><br></p> & ...

Delivering information to personalized components using React Bootstrap

I am working with a custom styled checkbox that is part of a mapped data array. {nfcArray && nfcArray.map((item, key) => { return ( <tr class="hover"> <td className="cell_style pl-3 pl-lg-5"> ...

Refreshing the webpage without reloading using Ajax ResponseText

While the form successfully sends data to the MySQL database, it is unable to display the responseText within the specified <div id="ajaxGetUserServletResponse"></div>. How can I retrieve the response? <form id="form-id" class="ajaxform" ac ...

"Troubleshooting VueJS: Issue with default value not being set in

Trying to set a default value for a select in Vue. <script src="https://unpkg.com/vue"></script> <div id="app"> <select v:model="select"> <option value="1">1</option> <option value="2">2</optio ...

Combining Multiple Tables Using Knex SQL Joins and Storing the Result in an Array

At the moment, I'm utilizing Knex to carry out queries on a MSSQL database. In my schema, there is a table named "Meals" which has the following columns: Id Additionally, there is also a table named "Vegetables" with the following columns: Id Meal ...

Having trouble verifying the selection option in Angular 6

I've been trying to implement Select option validation in Angular 6, but neither Aria-required nor required seem to be effective. The requirement is for it to display a message or show a RED border similar to HTML forms. Here's the HTML snippet ...

Take command of the asynchronous loop

I have two Node.js applications that serve different purposes. The first is an Express.js Server (PROGRAM1), responsible for providing the user interface and RESTful APIs. The second is a web crawler (PROGRAM2), tasked with continuously reading items, do ...

Tips for including arguments in pm2 file execution

What steps should I take to run the cluster.js file on pm2 successfully? When I try executing the file locally with 'node cluster.js 0 1' command, it appends two arguments but encountering an error when using 'pm2 start "cluster.js 0 1"&apos ...

Display issue with ThreeJS cube

Currently, I'm delving into the world of ThreeJS and decided to incorporate the library into my existing NextJS project. My goal was simple - to display a cube on the front page. However, despite my best efforts, nothing seems to be appearing on the s ...