Identify a div that manifests within the region of another element

Greetings everyone!

I have a target that shoots randomly.

I'm trying to make my "winner" div appear when a shot hits the center of the target.

I really appreciate any help, I've already attempted collision detection but it doesn't work since the shooting div appears later.

/*New sprinkle angles*/
var angles = ["45deg", "75deg", "-45deg", "-75deg"];
/*New sprinkle colors*/
var colors = ["black" , "blue" , "gold", "darkorange"];

/*Function to add a new random position, rotation and colour sprinkle*/
function addSprinkle() {
  /*Pick angle and color*/
  var randomangle = angles[Math.floor(Math.random() * 4)];
  var randomcolor = colors[Math.floor(Math.random() * colors.length)];

  /*Create new sprinkle div*/
  var newsprinkle = document.createElement("div");

  /*Assign new div class of .sprinkletemplate */
  newsprinkle.setAttribute("id", "sprinkletemplate");

  /*Set random angle rotation*/
  newsprinkle.style.transform = "rotate(" + randomangle + ")";

  /*Set random background color*/
  newsprinkle.style.background = randomcolor;

  /*Set random number for Top and Left*/
  var sprinkletopnum = Math.floor(Math.random() * 100 );
  var sprinkleleftnum = Math.floor(Math.random() * 100 );

  /*Check if they fall in the center or corners*/
  do {
    sprinkletopnum = Math.floor(Math.random() * 100 );
    sprinkleleftnum = Math.floor(Math.random() * 100 );
  } while (
    /*Middle*/
    (sprinkleleftnum >= 0 &&
      sprinkleleftnum <= 0 &&
      sprinkletopnum >= 0 &&
      sprinkletopnum <= 0) ||
    /*Top Left*/
    (sprinkletopnum <= 17 && sprinkleleftnum <= 17) ||
    /*Bottom Right*/
    (sprinkletopnum >= 77 && sprinkleleftnum >= 77) ||
    /*Bottom Left*/
    (sprinkletopnum >= 77 && sprinkleleftnum <= 17) ||
    /*Bottom Left*/
    (sprinkletopnum <= 17 && sprinkleleftnum >= 77) ||
    /*Trim Bottom*/
    sprinkletopnum >= 95
  );

  /*Change the top / left css of the new div*/
  newsprinkle.style.top = sprinkletopnum + "%";
  newsprinkle.style.left = sprinkleleftnum + "%";

  /*Append to #layer*/
  document.getElementById("layer").appendChild(newsprinkle);
}

Answer №1

<script src="https://ajax.googleapis.com/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Spectral+SC" rel="stylesheet">
<link rel="stylesheet" href="./cible.css" />
<script src="./cible.js"></script>

<body>
<div class="container">
    <div class="base">
    <img src="./cible.png">
    <div class="sprinklecontainer" id="layer"></div>
<div id="center"></div>
    <div id="couleur" class="couleur" ></div>
<div id="winner">WINNER !</div>
  </div>
</div>
</div>

<div class="buttonbox">
  <a class="button" onclick="addSprinkle()"><span class="sprinklestext">Hit</span><a>
</div>

</body>

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

Adjusting the background hue of a bootstrap panel

I'm attempting to utilize JavaScript to change the background color of a panel in order to create a "selected" effect. Upon clicking, I retrieve the div by its ID and modify its background color. Although it initially works as intended, the backgrou ...

Using jQuery to modify the collapse behavior in Bootstrap accordion from the "+" sign to the "-" sign

Currently, I am attempting to display the 'minus (hyphen)' sign when a user clicks the button. Despite trying various codes, none seem to be working as expected. Can anyone identify where the mistakes may be? <a role="button" data-toggle="col ...

Modify the text tone within a specific cell

Welcome to my unique webpage where I have implemented a special feature. The text highlighted in red represents the unique identifier for each individual cell. Interestingly, below there is an input field containing the code "0099CC", which corresponds to ...

Send a file using ajax with the help of JavaScript and PHP

Currently, I am looking to implement a method for uploading files using Ajax and JavaScript/PHP without having the page refresh. My initial thought is to use Ajax to send the file using xmlhttp.send(file) and then retrieve it in the PHP script, but I' ...

reversing an array does not have an effect

Whenever I attempt to reverse the order of my array using the reverse() function, the changes do not reflect in the view until I make a change to the file and save it. Items.js: import { useState } from "react"; const Items = (props) => { ...

Exploring the versatility of CSS variables in JavaFX

In need to style four buttons in a consistent way, but each one should have its own unique image. Three styles are required: one for normal state, and the others for hover and focused states. Using CSS for this task would involve a lot of repetition, as ...

Implementing Asynchronous Custom Validators in Angular 4

I've encountered the following code snippet: HTML: <div [class]="new_workflow_row_class" id="new_workflow_row"> <div class="col-sm-6"> <label class="checkmark-container" i18n>New Workflow <input type="che ...

Is there a way to prevent my <div> from scrolling when the mouse hovers over it?

I currently have a div that is set to scroll infinitely using JavaScript: <script language="javascript"> i = 0 var speed = 1 function scroll() { i = i + speed var div = document.getElementById("content") div.scrol ...

Issues arise when trying to integrate iframes with Ionic and AngularJS, as they

Using iframes in my ionic app to display webpages within the application has presented a challenge. This is what I currently have implemented: <iframe class= 'webPage' name= "eventsPage" ng-src="{{object.url}}"></iframe> The issue ...

Prevent MUI Autocomplete from closing when the option menu is opened with blur

Seeking assistance with modifying an autocomplete menu to include a dropdown for each item. Issue arises after trying to open the additional menu as it triggers an immediate closure of the autocomplete. For reference, attached is an image showcasing the i ...

What could be causing the promises in Promise.all to remain in a pending state?

After restructuring my code to correctly utilize promises, I encountered a challenge with ensuring that the lastStep function can access both the HTML and URL of each page. To overcome this issue, I'm attempting to return an object in nextStep(). Alt ...

Extracting information from ul li div elements and saving it in JSON format

I have been pondering this issue for the past few hours and haven't been able to find a solution. Essentially, I have a list structured like this <ul class="list"> <li class="user"> <div class="name">Name</div> ...

Employing multer in conjunction with superagent to enable file uploads from a React application

I am new to using multer and experiencing some difficulties with it. My goal is to upload an image file from a react client using the superagent library to my server. However, the req.file data always shows as undefined in my code: On the server side : ...

Incorporate new functions through the css class name "javafx"

On my dashboard, I have 10 labels that share the same CSS class for mouse over events. Now, I want to change the button style after it is pressed and remove the mouse over effect. How can I achieve this? ...

Where can I locate a specific child element in this scenario?

Currently, I am exploring the possibilities of integrating AngularJS into my application and have encountered a question regarding the click event implementation. Within my HTML code: <div ng-click='clickMe()' ng-controller='testCtrl&ap ...

Place the emoji where the cursor is located

My query was already posted on stack overflow but unfortunately, I did not receive a response. The issue revolves around 2 links named "add emoji 1" and "add emoji 2". As mentioned earlier, my question can be accessed here: Insert smiley at cursor positio ...

Tips for correctly mentioning a user while using message.channel.send in discord.js

Looking for guidance on tagging a user properly using message.channel.send in discord.js? Here's the code I'm currently working with: function replaceAll(str, find, replacer) { return str.replace(new RegExp(find, 'g'), replacer) ...

The React component does not trigger a re-render

Using React Redux, I am able to pass values successfully. However, the component I intend to render only does so once. Interestingly, when I save my code in VSCode, it renders again and the data displays on the page as expected. return ( <div classN ...

Creating a new music application and looking for ways to keep track of and update the number of plays for

I'm currently developing a music app and am looking for a way to update the play count every time a user listens to a song for at least 30 seconds. I've attempted the following approach: let current_final; let current_initial; ...

How can I use jQuery to trigger a left or right movement on a Waterwheel Carousel?

I'm a beginner in jquery and I'm looking to navigate my Waterwheel Carousel to the left or right using buttons. Let's say I have a button for moving to the right. How can I achieve this using the Waterwheel Carousel function? I've atte ...