In this post I’ll cover the basics of implementing drag and drop with collision detection using the Canvas element and JavaScript. The end result will produce the following:

Each item on the canvas is represented by a rectangle. When clicking inside the bounds of an rectangle the inner circle’s colour changes to grey to indicate the item is selected. Release the button results in a mouse up event . Between the mouse down and up events the item can be dragged and thus moved around the canvas. When two objects collide they will be coloured red. This functionality has been implemented as follows.

Each item is represented in JavaScript as an object called ‘Thing’. This object contain the following code:

The majority of the above is fairly self-explanatory. The object consists of properties representing the position e.g. x, y. The cartesian points are used within several functions to draw the rectangle and its inner shapes.

To draw a ‘Thing’ object we first need to create one. This is performed within the ‘init’ function:

The first item in the above list, allows us to resize the canvas whenever the window size changes. The function which is called following a window resize event simply resizes the canvas to fill the Browser’s windows.

The second item simply uses the document object to obtain a handle to the canvas and its context. The context gives us access to the drawing features the canvas posses.

The third item creates an instance of the ‘MouseHelper’ object. This object handles the mouse down, move and up events. This consist of the following code:

The above object handles the mouse events. The mouse down function uses basics maths to work out whether the mouse pointer is within any of the ‘Thing’ objects. When a match is found the index of the object is stored. This index is used in the mouse move event to re-position the selected object as the mouse moves. Finally the mouse up function triggers the call to the collision check callback function which is passed into the objects constructor. Each event also calls the main draw function.

The main draw function is first called as part of item four in the aforementioned list. The function performs the bulk of the work; drawing the objects in turn. The code for this is:

The above function is really short and clean. It simply iterates through each ‘Thing’ and calls its draw function. Before drawing the object it first clears the canvas by calling the function of the same name. To fully understand how this works we next need to look into the draw function of the ‘Thing’ object:

The above function uses various native function calls to the canvas’ context to draw the rectangle and bezier curves that make up the shape. The final part of the code we need to cover is the collision callback function:

This function uses the ‘IntersectHelper’ object to determine whether any of the ‘Thing’ objects collide. When two objects overlap the over-lapping flag is set which is the trigger to colour the object red.

That concludes the article. The full source code is available at my git hub account. A fully working version can be viewed here. Please feel free to use the code for any project or comment on way to improve the above.

The official site claims ‘Node is built on Chrome’s JavaScript runtime it can be used for building fast, scalable network applications’. Drilling into that verbiage; it is quite obvious what fast means. Scalable is also semi-obvious; but, only within context. Scalable in this context means an application – for example a website similar to twitter – can easily be modified to cater for increasing demand: such as the number of users accessing a site increasing. Without understanding the technology behind sites such as twitter we can all imagine the amount of people tweeting at any one time – a lot. How does the technology behind twitter handle this amount of activity: in short the answer is, by being scalable. The final part of Node’s official statement refers to the ‘network application’. This simply means the components that provide technology behind any given website and/or mobile app. Read more…

Tip #2 in this series touches on the topic of security. With the latest heartbleed vulnerability within OpenSSL hitting the headlines security it currently at the forefront. I think it is fair to say that security is not a priority for most web developers: for some it is not even on the radar. The problem is, that it is way, way too easy, to roll-out a site for a new project, get paid and move on. But, what happens to the site a week, month or a year down the line? Worse case scenario is the site gets hacked. Why? Because, it is not being kept up-to-date with the latest security patches and your web developer has moved on.

The simple solution is to keep you platform up-to-date. Whether it Magento, Symfony, WordPress or Joomla. It needs to be kept current. If you were offered a way to improve the security of your house would you take it? Of course you would. The same should apply to your website. Security vulnerabilities are discovered all the time. We should give the framework vendors credit here. They issue security patches with a high level of speed and efficiency. The problem you face is getting that security patch installed on your website. I’d suggest striking a deal with your web designer/agency so they are responsible. Of course they would expected some recompense for this, but, it shouldn’t be a lot. Any web designer/agency who will not agree to this are obviously not a good choice to begin with. Security first!

Web development is a hard playing field to operate in. The competition is great and keeping ahead of your competitors is difficult. Assuming potential customers rate your work and compare your goods against the competition means there is no place for complacency. This tip will help ensure level’s of quality are high and more importantly transparent and measurable to your audience!

2013 Responsive Design really exploded. It seems to have past the test of time and is here to say. Yes, it causes web developers a little more of a headache than a non-responsive site; however, the advantages it brings are well worth reaping. With the ever increasing number of mobile devices it is hard to ignore responsive design.Read more…

In the previous parts (1, 2 and 3) we’ve developed a basic game which allows a player to move a target around the screen and shot some bad guys. In this post we’ll add labels to show the shots fired, hits and accuracy ratio. This will give the player a reason for playing e.g. improve your statistics by shooting all of the bad guys with the minimum amount of shots. With this in place we can start to think about adding levels and keeping a high-score table.Read more…

The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

Madison Square Garden can seat 20,000 people for a concert. This blog was viewed about 61,000 times in 2013. If it were a concert at Madison Square Garden, it would take about 3 sold-out performances for that many people to see it.

Following on from Part 2 we have put in place the foundation of our game and have some ‘bad guys’ moving in a menacing manner. We are close to having a game: if we could just shot the ‘bad guys’. Reminding ourselves of the goals set out in Part 1 we need to achieve the following: Read more…

Following on from Part 1 we have put in place the foundation of our game. But, we can’t really call it a game as we have no interaction. Reminder ourselves of the goal set out in Part 1 we need to achieve the following:

In you want to have a peek at the result of following this post click here.

I’ve always wanted to create a game. It seems the older I get the closer I get. This post is the first in a small series which walks you through the steps I followed to create a simple game. Here is a taster of the end result: