Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Polygon shaped buttons

Is there a way to create polygon shapes for buttons? Should I just use an imagemap in a webview? I want to create a clickable USA image map.

— asked May 14th 2010 by Jeff Krause
  • button
  • imagemap
  • poly
  • polygon
  • shapes

3 Answers

  • Accepted Answer

    Here's what I finally came up.

    I created an imageview with a photo of the USA Map. I then created several dictionaries of points, defining the regions I wanted to capture clicks from on the image. I then added an event listener that uses the following function to detect if the click event happened inside any of the polygon dictionaries I had defined.

    // Point inside a polygon
    function isPointInPoly(poly, pt)
        for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) 
            ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y)) && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x) && (c = !c);
        return c;
    /** Example implementation code
    points = [
        {x: 0, y: 0},
        {x: 0, y: 50},
        {x: 50, y: 10},
        {x: -50, y: -10},
        {x: 0, y: -50},
        {x: 0, y: 0}
    alert(isPointInPoly(points, {x: 10, y: 10}) ? "In" : "Out");
    — answered May 16th 2010 by Jeff Krause
    • Hi Jeff,

      could you please share your code. imageview with a photo of the USA Map.


      — commented April 2nd 2014 by Rupesh Borkar
    • Jeff Krause, many thanks for this, you saved my day !

      — commented April 17th 2014 by Julien Gattegno
    • nice logic Jeff..thanks..works like charm..

      — commented April 19th 2014 by Rama krishna
    • Thanks a lot. Blessings Jeff!!

      — commented July 16th 2015 by Max Morales
  • Jeff,

    amazing script, but now if i want to check if a poly is in poly do you have any guideline to realize it...

    Thanks a lot

    — answered February 10th 2012 by Stefano Talotta
  • Hey Jeff,

    You could also just use an imageView and add a click eventListener to it.

    — answered May 14th 2010 by Dan Giulvezan
    1 Comment
    • what is this answer?

      please first read question carefully…

      — commented April 19th 2014 by Rama krishna
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.