Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:21:14+00:00 2026-06-16T13:21:14+00:00

I’m creating a simple web based game using the HTML5 canvas and JavaScript. I

  • 0

I’m creating a simple web based game using the HTML5 canvas and JavaScript. I currently have some images displayed on the canvas, each representing items found in a fast food restaurant. I also have four ‘description boxes’, each labelled either asset, liability, income or expenditure.

Each of the images belong to one of these description boxes, and the user is required to drag and drop each image to the correct description box.

I plan on checking if the user has dragged an image to the correct box by using the name of the JS variable holding the box image, and the HTML alt tag of each of the item images, i.e. img “chair” has alt tag “asset”, and the description box for assets has the variable name “assetsDescriptionBox”, so I would use an ‘if’ statement to detect if the image the user is dragging was dragged to the same area of the canvas as where the assets description box is being displayed, and if that image has the alt tag “asset”, it would then disappear from the canvas (and be added to an array for use later in the game).

However, if the image the user was dragging didn’t have the alt tag “asset”, but had some other tag, i.e. “liability”, then it would be redrawn on the canvas, back where it was originally drawn.

What I’m not sure about is how to implement this. I’ve had a look at collision detection, and it seems one way of doing it is to use the JS method getBoundingClientRect to get the ‘outer limits’ of an image, and then check if the outer limits of the two images overlap at all, and if they do, then do something. But, I’m not sure about how to use this method, and couldn’t find anything particularly helpful when doing a quick Google search.

Does anyone know if this would be the best way to do this? If so, could you post an example of how to use the getBoundingClientRect method? Or if not, how else would you do it?

Edit 17/12/2012 @ 16:45

By the way, I’m using the KineticJS library (a copy I have saved locally to make one or two changes to) to add the drag and drop functionality, so I assume there would be something in the library that I need to alter/ add to in order to add the collision detection.

Anyone have any ideas?

Edit 01/01/2013 @ 12:35

Hi there, thanks for your answer- it certainly seems like that’s what I want to do. I already have the images all displayed on the canvas, with four ‘static’ ones, that can’t be dragged around the canvas- these are the ones I want to use as my ‘drop zones’, and it is possible to drag and drop the rest. I’m not quite sure how I would add the functionality your code provides to what I already have? If you go to the URL: users.aber.ac.uk/eef8/project/development/featureset2dev you will be able to see what I have already working.

To add the ‘dropzone’ feature to my description boxes, should I add them to the canvas with the line

var i = new Image(200, 200, 50, 50, 'cat.jpg', 300, 300, 60, 60); 

as you have done in your example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:21:15+00:00Added an answer on June 16, 2026 at 1:21 pm

    I’d say the best way to go about doing this is keeping track of each object’s X/Y position along with width and height; using a simple rectangular collision function like this one takes two objects and checks if their bounding boxes overlap.

    function collides(a, b)
    {
        if (a.x < b.x + b.width &&
            a.x + a.width > b.x &&
            a.y < b.y + b.height &&
            a.y + a.height > b.y) return true;
    }
    

    If I was going about this, I might set it up with an object that has a drop zone object attached to it, so that image could only trigger one drop zone, instead of checking the alt tag, using pure JS.

    function Image(x, y, w, h, i, dx, dy, dw, dh)
    {
        this.x = x;
        this.y = y;
        this.width = w;
        this.height = h;
        this.image = i;
        this.dropzone = new DropZone(dx, dy, dw, dh);
    }
    
    function DropZone(x, y, w, h)
    {
        this.x = x;
        this.y = y;
        this.width = w;
        this.height = h;
    }
    
    var i = new Image(200, 200, 50, 50, 'cat.jpg', 300, 300, 60, 60);
    

    Then you’d have a loop that updated the co-ordinates of the dragged image when you moved it. Note that you can’t have click handlers on drawn sprites inside the canvas, only on the canvas itself; you check whether the mouse co-ordinates are inside the drawn sprites.

    The other way of going about this, of course, is using the HTML5 drag and drop API, although I have no experience on how to use that it may be more suitable for your needs.

    http://www.html5rocks.com/en/tutorials/dnd/basics/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am reading a book about Javascript and jQuery and using one of the
I am using jsonparser to parse data and images obtained from json response. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.