title: Adding collision detection to images drawn on canvas
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p><strong>Edit 17/12/2012 @ 16:45</strong></p>
  
  <p>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.</p>
  
  <p>Anyone have any ideas?</p>
  
  <p><strong>Edit 01/01/2013 @ 12:35</strong></p>
  
  <p>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. </p>
  
  <p>To add the 'dropzone' feature to my description boxes, should I add them to the canvas with the line</p>
  
  <pre><code>var i = new Image(200, 200, 50, 50, 'cat.jpg', 300, 300, 60, 60); 
  </code></pre>
  
  <p>as you have done in your example?</p>