I am looking for a way to display multiple images when the user clicks a button. It needs to be done

After successfully implementing a draggable single circle image when clicking a button, I am now looking to add multiple circle images each time the button is clicked.

I'm considering using the append function with the canvas tag for this purpose. Can someone help me understand how to implement this?

<script>

$(document).ready(function() {

  $('#pink-circle-button').click(function() {
    $('#currentCircle').show();

                   });
            });

    $(function() {
        $( "#currentCircle" ).draggable();
    });

    $(document).ready(function(){
    $("button#pink-circle-button").click(function(){
    $("p").append("<canvas id='currentCircle' class='drawCircle'/>");
  });
});
</script>

<style>
.drawCircle{border: 2px solid rgb(255, 0, 255);background-color:black; position: fixed; display: none; top: 97px; left: 572px; width: 153px; height: 150px; border-radius: 76.5px 76.5px 76.5px 76.5px;}
</style>


<canvas id="currentCircle" class='drawCircle'/></canvas>

Answer №1

Here's a starting point that I came up with, although it may not fully meet your requirements: http://jsfiddle.net/eGjak/2/.

var ctx = $('#cv').get(0).getContext('2d');

$('body').bind('selectstart', function() {return false});

var Circle = function() {
    this.x = Math.random() * 400;
    this.y = Math.random() * 400;
    this.c = 'rgb(' + Math.floor(Math.random() * 256) + ',' +
                 Math.floor(Math.random() * 256) + ',' +
                 Math.floor(Math.random() * 256) + ')';
};

var circles = [];

function d(xp, yp, x, y) {
    return Math.sqrt((x-xp)*(x-xp) + (y-yp)*(y-yp));
}

$('button').click(function() {
    circles.push(new Circle);
    render();
});

var currindex = -1;

$('#cv').mousedown(function(e) {
    bx = e.offsetX, by = e.offsetY;
    for(var i = 0; i < circles.length; i++) {
        if(d(circles[i].x, circles[i].y, e.offsetX, e.offsetY) < 50) {
            currindex = i;
            tx = circles[i].x;
            ty = circles[i].y;
        }
    }
});

var bx = 0, by = 0, tx = 0, ty = 0;

$('#cv').mouseup(function(e) {
    currindex = -1;
});

$('#cv').mousemove(function(e) {
    if(currindex > -1) {
        circles[currindex].x = tx + (e.offsetX - bx);
        circles[currindex].y = ty + (e.offsetY - by);
    }
    render();
});

function render() {
    ctx.clearRect(0, 0, 400, 400);
    for(var i = 0; i < circles.length; i++) {
        ctx.beginPath();
        ctx.arc(circles[i].x, circles[i].y, 50, 0, Math.PI*2);
        ctx.fillStyle = circles[i].c;
        ctx.fill();
    }
}

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

Tips for maintaining checkbox checked status through page reloads

Hello there! In my Laravel e-commerce store, I have a list of currency values displayed as checkboxes. When a checkbox is selected, the page reloads to update the currency value. However, the checkbox selection is lost after the page reloads. Any suggest ...

Is it recommended to continue using vendor prefixes for border-radius property?

When coding, I currently utilize the following: -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; After running tests, it seems that there was no noticeable difference in modern browsers (Chrome 33+, Opera 25+, Safari 8+). Internet ...

What is the method for eliminating quotes from a URL?

Changing the URL: history.replaceState('data to be passed', 'Title of the page', '<?php echo getAddress(); ?>/?usp-custom-14="'+urldates+'"&usp-custom-8="'+title+'"'); After making changes, it s ...

What is the best way to display information in a newly added row of a datatables table?

Working on ASP.NET gridview conversions with datatables.net plug-in. The reason behind this is complex and subject to debate. But, I need assistance with a specific issue. The process of converting the gridview using Javascript was simple and effective. H ...

Inspect every div and perform an action if the criteria are met

I need assistance with adding text inside a specific div based on certain conditions. Currently, all the div elements are being populated with the added text. Please review my code for more clarity on what I am trying to achieve. Can you suggest the most e ...

Error: The function jQuery(...).hexColorPicker does not exist

I am attempting to utilize a color-picker jQuery plugin. Below is a screenshot of the JavaScript file: https://i.stack.imgur.com/ovRjx.png Here is the code I am using to initialize it: <script type="text/javascript> jQuery(function(){ ...

Tips for implementing AJAX in the menu bar using PrimeFaces

Hey there, I was wondering something. Let's say I have a navigation bar like the one below: Navigation Bar Is there a way to use ajax or any other method so that when I click on a menu item or submenu in the navigation bar, the name of the menu item ...

Dynamically scrolling using SuperScrollorama and Greensocks

I'm currently facing a JavaScript animated scroll challenge that has me scratching my head. My approach involves using the SuperScrollorama jQuery plugin, which relies on the Greensock JS tweening library. The main effect I'm aiming for is to " ...

I'm experiencing some unusual behavior with the Windows media app when using HTML and CSS

I have a Windows Media Player box on my page, but it overlaps every piece of HTML code. How can I get it to move to the back so that I can still use it? Another problem is that everyone who visits my page needs a plugin to load it, even though most people ...

JQuery does not recognize $(this) after the HTML has been altered

I manage a website that includes several href links and 2 buttons that have the ability to modify those links dynamically. <div class="links"> <p class="hlCategory">Classical Mechanics</p> <ul> <li><a cl ...

How can you prevent an element from overflowing when employing window.pageYOffset and using a switch case to alter the background color at specified pixel thresholds?

I want the <body> element's background-color to change every 500px as I scroll down. I've scoured the web for answers, but still can't figure out what's going wrong. Please review the code. However, Firefox Browser indicates that ...

Ensure that the div remains within the viewport

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Tit ...

The coloring of my div elements is not displaying as intended

After reviewing the provided html and css, I am puzzled as to why the parent container is being colored green while the child items remain uncolored. It seems like all div elements are being affected by the green color. What I actually want is for the pa ...

Fill the list items with values from the given array

I have an array called var array = ["one", "two"]; and I want to display each element in a separate list item. The desired output is: <li>Number one</li> <li>Number two</li> However, the current output is: <li>Number onetw ...

What is the proper way to utilize JQuery and Ajax to communicate with a global function in writing?

My goal is to retrieve data from an AJAX request and store it in a global variable. Despite trying to make the call synchronous, I am encountering issues where the value of the global variable becomes undefined outside of the Ajax request. I have attempt ...

Combining 2 objects with changing properties using jQuery

Two javascript objects need to be merged, but one contains dynamic form field values saved as variables. An example code snippet can be found here: http://jsfiddle.net/ZAa7L/ I came across this code in a stackoverflow post where it worked perfectly. Now ...

Modify picture properties when listbox is altered using jQuery

I need to set up a unique album gallery display where different folders are selected based on the item chosen in a selectbox. Below is the HTML code: <select name="album" id="album" onChange="changeimage();"> <option value="0" selected disab ...

Show a textbox once a dropdown option is chosen

Initially, the textboxes are hidden and only a dropdown list is visible. When a value is selected from the dropdown list, the textboxes become visible. <select class="dropdown"> <option>Choose your location</option> <optio ...

What is stopping me from utilizing ES6 template literals with the .css() method in my code?

I am currently working on a project where I need to dynamically create grid blocks and change the color of each block. I have been using jQuery and ES6 for this task, but I am facing an issue with dynamically changing the colors. Below is the snippet of m ...

There seems to be a malfunction in the PHP controller causing issues with the functionality

Currently, I am utilizing jQuery and PHP for my project. In the controller, I have integrated AJAX functionality. Upon the initial click action, the result is successfully retrieved (the current "show more button" is hidden and a new "show more button" is ...