Category: PHP
Jan 5, 2017

Normally in web application development, clicks are a matter of listeners. Either a click hit a thing or it didn’t. When using the HTML5 canvas, this isn’t the case. Images and features are rendered onto it, but you can’t attach a listener to them. Instead, you monitor where these elements visually reside versus where clicks land.

If an object is rectangular and will never rotate, the solution is easy. If click X is between object start X and object end X, and click Y likewise, then it hit. If not, it didn’t.

In this post we’ll construct a simple series of functions for determining the same thing if the rectangle has rotated, then look at how the idea can apply to more complex shapes. Nothing new or groundbreaking, and nothing language-specific — consider this a StackOverflow answer.

Visualization

For a developer who hasn’t mathed in a while, this can seem tricky at first. Once you see a few images it’s perfectly sensible.

We’ll be drawing four triangles. Each one consists of the click point and two vertices of the object. We then compare the area of the object to the combined area of the triangles. If they’re the same, the click had to be in the object. If the triangles are larger, the click couldn’t have been in the object.

Rotated Rectangle - Click hit

If the click was inside, the areas are equal.

Rotated Rectangle - Click miss

If the click was outside, the combined area of the triangles is bigger than the area of the rectangle.

How much it’s rotated doesn’t matter. Where it’s at doesn’t matter. That’s literally all there is to this.

First things first

To figure out if the click hit, we’ll need four pieces of data:

  • The coordinates of the click
  • The position of the rectangle
  • The size of the rectangle
  • The angle of rotation of the rectangle

Those last three pieces we’re just putting together so we can find the vertices. Once we know the click position and the vertex positions, figuring out if the click hit is easy.

Finding the vertices

Since you’re probably not tracking the individual locations of the vertices and are instead tracking the position of the object, you’ll want to start with a function that can find these by the object’s size and position. How this will be done is hugely variable depending on where you’re measuring from (tracking left top? center?) and if/how the object has been scaled.

If we pretend like there’s no scaling or rotation and we’re measuring from left and top, the function may initially look like this:

Since this doesn’t take into account rotation yet, we’ll need a utility function to find a point’s new location after it’s been rotated by X degrees around another point:

Modifying our previous findRectVertices  function to also use rotation may then look like this, assuming we’re rotating objects around their center:

Calculating the areas

Now that we have all the necessary points, all that’s left are three things:

  • Calculate the area of the rectangle
  • Calculate the area of the triangles
  • Compare the two

If you recall high school geometry, point two is the loaded one there. To find the area of an unknown triangle we’d use Heron’s formula, but first the distances of each side will be needed. The two utility functions may look like this:

With those functions ready, we can now write a complete function that performs the three bullet points and returns a simple True or False:

Common issues

By far the most frequent annoyance with this approach when working in the DOM is going to be getting good position numbers. So many variables can throw a wrench in how you’re measuring where the object is. If your reported object position doesn’t match up with the reported mouse or touch position, all of the above is useless.

Look for situations in which clicking near something instead of on it reports a hit, then try to identify elements that match the size of the inaccuracy. Everything from margins on the HTML or body elements, borders on divs, document scroll position, and box-sizing settings can play havoc on your ability to get clean measurements that match up with what’s plainly visible.

Going farther

This method isn’t just good for rectangles. It would be straightforward to apply to triangles, too, or for any other convex polygon. Consider how you might implement the ability to check if a click landed inside a hexagon. As a brain teaser, what would happen on a concave polygon?

Apr 15, 2015
Update: The Sendy Invoicing add-on has now been added to Sendy’s API docs as a third party resource! View this add-on and other great supporting software at https://sendy.co/api.

Our organization has needed a more user-accessible mass email solution for some time, so when we saw Sendy it was a no-brainer to pick up the dead cheap $59 license and spin up an instance. The whole thing has a minimal, functional, easy-to-explain feel to it and allows the creation of as many “Brands” as needed, each with their own logins, custom templates, custom lists, etc. The sole hangup was that it only supported billing for campaigns through PayPal, and we simply aren’t allowed to accept payment that way.

Internal payments here work through an in-house system and are issued with invoices, which I’ve heard are pieces of paper that people hand to each other and put in things called “filing cabinets.” I needed to figure out a way to do invoicing with Sendy, and I’m happy to say it wasn’t all that difficult.

The resulting Sendy Invoicing add-on, available on GitHub, allows an admin to check a new box labeled “Charge via invoice” under a brand’s payment settings. Users are then shown a customizable, printable invoice when they’d normally be sent to Paypal. Upon accepting an invoice, a copy is emailed to the payee for record-keeping.

A sample Sendy invoice

The whole project plugs right into the Sendy server, and after running an install script that uses a few broad regular expressions to insert two small blocks of code into Sendy’s files, it’s ready to run. Admittedly, it’s not an ideal integration. There isn’t really any plugin architecture or hooks that I could find, so it relies on modifying a couple of the core files, but I tried to make it as minimally invasive, straightforward, and well-documented as possible so that version updates don’t necessarily put it out of commission.

View the project on GitHub and transport your own Sendy server back to the 80s!

Fork me on GitHub