When the draggable item is released near the drop zone, allow drag and drop to combine

In my latest project, I created a fun game that involves matching different shapes. The goal is to drag the correct figure next to its corresponding shadow figure for a perfect match. Currently, if you partially overlap the square with its shadow, the game still accepts it as a good match.

$(document).ready(function() {
  $(".selectable").draggable({
    addClasses: false,
    snap: true,
    stack: ".destination",
    scroll: false
  });

  $(".destination").draggable({
    snapMode: "inner"
  });

  $(".destination").draggable("disable");

  $(".destination").droppable({
    drop: function(event, ui) {
      var selectedShape = ui.draggable.attr("id");
      var dropZone = $(this).attr("id");
      dropZone = dropZone.replace("inside", "");
      if (selectedShape == dropZone) {
        $("#" + selectedShape).draggable("disable");
        checkShapeStatus();
      } else {
        
        alert("Wrong choice!");
      }
    }
  });
});

function checkShapeStatus() {
  var counter = 0;
  $(".selectable").each(function() {
    var $thisId = $(this);
    var booleanValue = $thisId.draggable('option', 'disabled');
    if (booleanValue) {
      counter = counter + 1;
    } else {

    }

    if (counter == 4) {
      win.play();
    }

  })
}
#square {
  width: 100px;
  height: 100px;
  background: red;
  margin-top: 8%;
  z-index: 1;
}

#circle {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  z-index: 2;
}

#triangle-up {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid green;
  z-index: 3;
}

#pacman {
  width: 0px;
  height: 0px;
  border-right: 60px solid transparent;
  border-top: 60px solid yellow;
  border-left: 60px solid yellow;
  border-bottom: 60px solid yellow;
  border-top-left-radius: 60px;
  border-top-right-radius: 60px;
  border-bottom-left-radius: 60px;
  border-bottom-right-radius: 60px;
  z-index: 4;
}

#squareinside {
  width: 100px;
  height: 100px;
  background: gray;
}

#circleinside {
  width: 100px;
  height: 100px;
  background: gray;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
}

#triangle-upinside {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid gray;
}

#pacmaninside {
  width: 0px;
  height: 0px;
  border-right: 60px solid transparent;
  border-top: 60px solid gray;
  border-left: 60px solid gray;
  border-bottom: 60px solid gray;
  border-top-left-radius: 60px;
  border-top-right-radius: 60px;
  border-bottom-left-radius: 60px;
  border-bottom-right-radius: 60px;
}

body {
  background-color: bisque;
  overflow: hidden;
}

#centerText {
  font-family: 'Rock Salt', cursive;
  font-size: xx-large;
  style="width:100%;
 height: 100%;
  z-index: 0;
  text-align: center;
  margin-top: 2%;
}

.grid-1 {
  display: grid;
  grid-template-columns: 150px 150px 150px 150px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 150px 150px 150px 150px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Shape Matching</title>
  <script src="https://code.jquery.com/jquery-1.7.2.min.js"></script>
  <script src="https://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
  <link href='https://fonts.googleapis.com/css?family=Rock+Salt' rel='stylesheet' type='text/css'>
  <link href="style.css" rel="stylesheet">
  <script src="script.js"></script>
</head>

<body>

  <div class="grid-1">
    <div id="pacmaninside" class="destination"></div>
    <div id="triangle-upinside" class="destination"></div>
    <div id="circleinside" class="destination"></div>
    <div id="squareinside" class="destination"></div>
  </div>

  <div class="grid-2">
    <div id="square" class="selectable"></div>
    <div id="circle" class="selectable"></div>
    <div id="triangle-up" class="selectable"></div>
    <div id="pacman" class="selectable"></div>
  </div>

</body>

</html>

If you're interested in modifying the code and testing the game yourself, feel free to give it a try!

Answer №1

To determine the coordinates of the dropzone and the dropped object, follow these steps:

$(document).ready(function() {
  $(".selectable").draggable({
    addClasses: false,
    snap: true,
    stack: ".destination",
    scroll: false
  });

  $(".destination").draggable({
    snapMode: "inner"
  });

  $(".destination").draggable("disable");

  $(".destination").droppable({
    drop: function(event, ui) {
      var selectedShape = ui.draggable.attr("id");
      var dropZone = $(this).attr("id");
      dropZone = dropZone.replace("inside", "");
      var destPos = ui.draggable.position(),
          selPos = $(this).position();

      if (selectedShape == dropZone && selPos.left == destPos.left && selPos.top == destPos.top) {
        $("#" + selectedShape).draggable("disable");
        checkShapeStatus();
      } else {
        
        alert("Incorrect selection!");
      }
    }
  });
});

function checkShapeStatus() {
  var counter = 0;
  $(".selectable").each(function() {
    var $thisId = $(this);
    var booleanValue = $thisId.draggable('option', 'disabled');
    if (booleanValue) {
      counter = counter + 1;
    } else {

    }

    if (counter == 4) {
      win.play();
    }

  })
}
#square {
  width: 100px;
  height: 100px;
  background: red;
  margin-top: 8%;
  z-index: 1;
}

#circle {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  z-index: 2;
}

#triangle-up {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid green;
  z-index: 3;
}

#pacman {
  width: 0px;
  height: 0px;
  border-right: 60px solid transparent;
  border-top: 60px solid yellow;
  border-left: 60px solid yellow;
  border-bottom: 60px solid yellow;
  border-top-left-radius: 60px;
  border-top-right-radius: 60px;
  border-bottom-left-radius: 60px;
  border-bottom-right-radius: 60px;
  z-index: 4;
}

#squareinside {
  width: 100px;
  height: 100px;
  background: gray;
}

#circleinside {
  width: 100px;
  height: 100px;
  background: gray;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
}

#triangle-upinside {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid gray;
}

#pacmaninside {
  width: 0px;
  height: 0px;
  border-right: 60px solid transparent;
  border-top: 60px solid gray;
  border-left: 60px solid gray;
  border-bottom: 60px solid gray;
  border-top-left-radius: 60px;
  border-top-right-radius: 60px;
  border-bottom-left-radius: 60px;
  border-bottom-right-radius: 60px;
}

body {
  background-color: bisque;
  overflow: hidden;
}

#centerText {
  font-family: 'Rock Salt', cursive;
  font-size: xx-large;
  style="width:100%;
 height: 100%;
  z-index: 0;
  text-align: center;
  margin-top: 2%;
}

.grid-1 {
  display: grid;
  grid-template-columns: 150px 150px 150px 150px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 150px 150px 150px 150px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Shape Matching</title>
  <script src="https://code.jquery.com/jquery-1.7.2.min.js"></script>
  <script src="https://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
  <link href='https://fonts.googleapis.com/css?family=Rock+Salt' rel='stylesheet' type='text/css'>
  <link href="style.css" rel="stylesheet">
  <script src="script.js"></script>
</head>

<body>

  <div class="grid-1">
    <div id="pacmaninside" class="destination"></div>
    <div id="triangle-upinside" class="destination"></div>
    <div id="circleinside" class="destination"></div>
    <div id="squareinside" class="destination"></div>
  </div>

  <div class="grid-2">
    <div id="square" class="selectable"></div>
    <div id="circle" class="selectable"></div>
    <div id="triangle-up" class="selectable"></div>
    <div id="pacman" class="selectable"></div>
  </div>

</body>

</html>

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

Personalized Bootstrap grid rows and columns

https://i.sstatic.net/R0yFY.png As a beginner in Bootstrap, I am having trouble achieving this specific layout in Bootstrap 4 grid. The areas marked in red are not appearing as expected. Here is what I have attempted so far: <div class="row"> ...

I found myself pondering over possible solutions to rectify this parse error

I need help resolving an issue with parsing. Here is the link to the error image: https://i.stack.imgur.com/jKQat.jpg. Additionally, my HTML code connecting to the CSS site can be found here: https://i.stack.imgur.com/ST31r.jpg. The website I linked in t ...

Using html() to load dynamic data can cause the script to malfunction if the content being loaded contains special characters

Utilizing the html() function, I am retrieving dynamic data from the database and appending it to my HTML. Everything functions correctly, except when the dynamic data contains '>' or '<' tags as data. In this scenario, the script ...

What are some ways to align text both vertically and horizontally within columns?

How can I center text both vertically and horizontally inside two columns using Bootstrap 4? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"> <div class="row"> <div class="co ...

Ways to retrieve the most recent state in a second useEffect call?

Currently, I am encountering a situation where I have implemented two useEffect hooks in a single component due to the presence of two different sets of dependencies. My challenge lies in the fact that even though I update a state within the first useEffec ...

What are some strategies for navigating the constraint of having multiple root elements in Vue.js?

Seeking assistance from the experts here to solve this particular issue. I have a dataset that looks like this: [ { title: 'Header', children: [ { title: 'Paragraph', children: [], }, ], }, ...

Is it possible to integrate a jQuery function within PHP code when encountering a "is not defined"

I am looking to implement the jQuery-confirm dialog in place of using a JavaScript alert generated by PHP. However, when I execute the following code, I encounter the following error message: $ is not defined echo "<script>$.alert({title: ' ...

Instructions for sending an email through a form while displaying a pop-up message

My Objective To create a functionality on my website where users can input their email addresses in a form and receive a validation popup indicating whether the email is valid or not. Background Information I am currently working on a website that allow ...

Using this functionality on a ReactJS Functional Component

Hey everyone, I'm fairly new to using React and I'm currently trying to wrap my head around some concepts. After doing some research online, I stumbled upon a situation where I am unsure if I can achieve what I need. I have a functional componen ...

Node.js QuickStart guide for authenticating with the Youtube API encounters error

Using node.js for a Discord bot, I encountered an issue with Google's API tutorial being outdated. Here is the link to their tutorial. The tutorial asks to select an "Other" option which no longer exists, now replaced by "desktop app". This was an ea ...

Is there a way to incorporate tailwind classes into each react component when utilizing the map() function?

While working on a React Movie app, I encountered an issue with the tailwind class mx-auto. It was not applying properly to all items in the array; only the first element was affected. const MoviesList = () => { return( <div className=&a ...

Why is the Border Radius hiding the corners of my image?

I've never experienced this issue before, but no matter how much padding I add to the picture, the edges are still slightly covered up. Here is an example image (notice that it doesn't happen for every icon - the third one has a normal round back ...

Unlocking the npm package event emitter on the client side in Meteor

Having some trouble with a seemingly basic issue. I came across an NPM package called LOG-WATCHER (used as an example) which keeps an eye on a specific log file on a client's local file system. This package emits events such as 'START', &apo ...

What is the best way to make IE 10 display a pointer instead of an I-bar for a select list?

Is there a way to make IE 10 display a pointer instead of an I-bar when selecting from a list? Despite trying various methods found in similar questions, I have been unable to resolve this issue. The cursor: pointer property works as expected on other br ...

displaying a pair of inputs next to each other

Is it possible to display two input fields side by side? I am using Angular's matInput forms, but struggling to position the second input next to the first. What I would like to achieve is to have "input1 , input2" on the same line. Here is my code: ...

Unspecified origins of Js in Chrome Extension

console.log(chrome.runtime.sendMessage({from:"script2",message:"hello!"})); However, attempting to send the message from a background script to a content script is proving to be unsuccessful. https://i.stack.imgur.com/ERgJB.png ...

Angular Material failing to adapt to mobile devices

My website was created using Angular Material and looks great on desktop, but is completely broken and out of place when viewed on mobile devices. I'm thinking it might be because I haven't set the column size for each element properly. Any advic ...

What is the reason for React.Component being implemented as a function rather than an ES6 class?

After delving into the codebase of React, I made an interesting discovery. When you define a class like class App extends React.Component, you are essentially extending an object that is generated by the following function: function Component (props, cont ...

Express server experiencing issues with generating Swagger documentation

I've been working on an Express API and decided to implement documentation using Swagger and JSDoc. However, the documentation is not working as expected. Here's how I've set it up: docs.ts: import swaggerJSDoc, { Options } from "swagg ...

Vibrant DT data table featuring vertical radio buttons

I am in need of a polished DT datatable that includes radio buttons embedded within a column. While this application provides a solution for horizontal buttons, I have taken on the task of adapting it for a vertical layout. Modifying the matrix was a strai ...