Incorporate div elements into the scene using the JQuery.throwable plugin

Attempting to enhance a jQuery function that inserts a new div into a parent div containing other divs with the JQuery.throwable physics engine applied.

Created a <p> with the class .button and added this jQuery code:

$(".button").click(function () {
$("#wrapper").append('<div class="instrument bass"></div>');

To include the new div in the gravity engine, had to reinitialize the JQuery.Throwable function. The updated jQuery code for adding a new div is as follows:

$(".button").click(function () {
$("#wrapper").append('<div class="instrument bass"></div>');    
$(".bass").throwable({
          containment:"parent",
          drag:true,
          gravity:{x:0,y:0},
          impulse:{
                   f:52,
                   p:{x:1,y:1}
                  },
          shape:"circle",
          autostart:true,
          bounce:2,
          damping:2,
          areaDetection:[[0,0,300,300]],
          collisionDetection: true
});

The #wrapper div serves as a container covering 100vh and 100w of the browser window to prevent the bouncing divs from falling out. Elements within the container are named with the class instrument and colored using additional classes such as bass, drums, lead, and chord.

Encountering issues with collisions when multiple new divs are added consecutively. Strange interactions between elements occur, possibly due to invisible boundaries or incorrect implementations in the code. Attempted using display:inline-block on .instrument but the issue persists.

Troubleshooting the code that adds the new div, aiming to simplify and resolve abnormal collision behavior. Provided a Plunker link below for further examination of the issue. Play around with the existing divs, then try adding multiple new divs to observe the erratic collision patterns.

Plunker: http://plnkr.co/edit/89LT1xqJZmLznymNypKL?p=preview

Answer №1

Make sure to include the button.click event right at the start of your $(document).ready(function() block. I've personally had success with this method, so hopefully it works for you too.

 $(document).ready(function() {
 $(".button").click(function () {
 $("#wrapper").append('<div class="instrument bass"></div>');
 $(".bass").throwable({
                    containment:"parent",
                    drag:true,
                    gravity:{x:0,y:0},
                    impulse:{
                        f:52,
                        p:{x:1,y:1}
                    },
                    shape:"circle",
                    autostart:true,
                    bounce:2,
                    damping:2,
                    areaDetection:[[0,0,300,300]],
                    collisionDetection: true
                });
});

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 sign-up button mysteriously vanishes after the page is refreshed

I am encountering an issue with the sign up button during the user registration process. There is a checkbox for Terms & Conditions, and the button should only become enabled after checking this box. Everything seems to be functioning correctly, but when I ...

Check to see if the image is accessible

When I need to retrieve an image path, I use an Ajax call. var triggerImageChange = function() { var selectedYear = $("#year2 option:selected").val(); var selectedType = $("#type2 option:selected").val(); var selectedImage = $("#imageSelect2 o ...

Is it possible to utilize the same Context in multiple forms within React?

Is it possible to utilize React Context to store the state of multiple forms, or should each form have its own unique Context? Below is a snippet of code that showcases the use of React Hooks API: In FormContext.js ... import {FormReducer} from './ ...

Each time a new client connects, socket.io replicates the information

Exploring Node.js and socket.io for the first time, I have developed a small application where a table is meant to be displayed in real-time via socket.io when a function is triggered through a websocket (function triggers an "emit"). However, I'm fac ...

Utilizing a function upon page initialization in VueX

I am currently learning Vue with Vuex and I have created a method called 'llamarJson' which is used to retrieve JSON data. However, I am facing difficulties in using this method when the page loads. I have tried various approaches but have not be ...

What is the best way to organize variable values in conjunction with their corresponding variable names?

Among the 7 variables - rodeo, saya, balthazar, mistral, luna, calypso, and kiara - each holds a value obtained from calculations in the initial part of my program. My goal is to arrange these variables in ascending order based on the values they contain, ...

jQuery: How can I add ajax content dynamically? Trigger?

Currently, I am developing the frontend of a web application that heavily relies on AJAX functionality, such as loading comments. All AJAX requests are being handled through jQuery functions. I am curious if there is a way for me to detect these insertion ...

Issue with jQuery content slider not working correctly due to embedded flash YouTube video causing div to overflow

I've created a custom ajax jQuery content slider that automatically cycles through each tab, each containing basic HTML content. The specific slider plugin I used can be found here: http://css-tricks.com/anythingslider-jquery-plugin/ However, I&apos ...

Every time I try to execute a function in Node.js, it triggers the emitHookFactory function which then causes the program to become trapped in an endless

I've encountered an issue where my Node.js program seems to be stuck in a loop within a function (emitHookFactory) located in \<node_internals>\internal\async_hooks.js). The strange thing is that I never explicitly called this fun ...

What is the best method for inserting the HTML content from a specific div into a textarea?

Everything seems to be working fine, but once I insert the HTML into the textarea, an issue arises where the div gets wrapped within another div, causing the layout to break. var urls = []; $('body').on('click', '.btn_video&apos ...

Leveraging the power of Required (html5) feature in Internet Explorer

What should I do if I want to make a textbox required on my webpage, but the issue is that it uses an HTML5 attribute? Is there a solution or alternative available? This is how I create my TextBox: @Html.TextBoxFor(model => model.SubChoiceText, new { ...

What is the best way to move the Grid upward when the above content is not visible?

Let me demonstrate what I have been working on. Currently, I am creating a weather application to explore the functionalities of https://material-ui.com/. I am attempting to prototype an animation inspired by Google Flights, which can be seen here: https: ...

Determine the possible width of a concealed element

I am currently customizing the lavalamp plugin to be compatible with dropdown menus, but I have come across a minor issue. I am trying to determine the offsetWidth of a hidden element. Obviously, this question is a bit nonsensical. What I actually need is ...

Managing error responses while fetching a blob through AJAX

Is it possible to execute an AJAX call that has the potential to return either a blob or a text string based on the server's response? My current task involves using AJAX to transform a user-provided video into an audio blob that can be utilized with ...

Exploring the integration of Mariadb COLUMN_JSON data in JavaScript

I am encountering difficulties while attempting to access COLUMN_JSON data in JavaScript. I referred to a MariaDB JSON example and inserted it into a table with the following query: UPDATE myTable SET myJsonColumn = COLUMN_CREATE('color', ' ...

The date entry is recorded as 0000-00-00 within the database

I'm attempting to save values from a Datepicker into my MySQL database using php Laravel. Based on what I can tell, the request is functioning correctly. When I use dd to display the request, the date shows up. However, upon inspecting the database, ...

Using PHP's echo syntax to resize an image

My code snippet looks like this: echo "<img src=\"images2/" . $row['image'] . "\" alt=\"\" /><br />"; } I'm trying to set a specific height and width for the image in my HTML, but I'm having trouble ge ...

Loading User Interface Control Dynamically with AJAX and C#

I'm currently working on a project where I need to dynamically load a PlaceHolder with a *.ascx page using an AJAX call. However, I've encountered an issue related to its static nature. The problem arises because I am unable to access the PlaceH ...

Convert coordinates from X and Y within a rotated ImageOverlay to latitude and longitude

After going over all the details in Projection's documentation and conducting various trial and error tests, I have hit a roadblock in finding a solution to this particular issue. All the solutions I have come across assume the x and y coordinates ar ...

Having trouble submitting ajax form data through nodemailer

Hey there, Recently, I created a web app using node.js and express. Everything seems to be working fine except for one issue - I am struggling to get the JSON data sent by AJAX into Nodemailer. Despite confirming that my AJAX is successfully sending the ...