Bloggity Blog

We love our work so much, we want to tell the world about it.

Simple Touch Events in CoffeeScript

Written by David Stump

I am an avid fan of simple, clean and elegant CoffeeScript (and thus JavaScript of course). On top of your code being more approachable, I love the sense of accomplishment when I find a clean way to solve a problem without immediately jumping to an external library or plugin. Thus, when I was faced with the problem of handling touch events on handheld and tablet devices for our new site, I started with a blank page in my favorite editor.

Touch.coffee is a simple and straightforward CoffeeScript class written with pure JavaScript that emits touch events to your application. The only dependency this class utilizes is EventEmitter to aid in handling the event triggers. The full source for this class is available for browsing on GitHub

To get our class started, we will setup a few variables that will help us along the way.

@Touch=

  horizontalSensitivity: 22
  verticalSensitivity: 6
  touchDX: 0
  touchDY: 0
  touchStartX: 0
  touchStartY: 0

The sensitivity variables allow us to specify how long a touch event should last before we go ahead and emit our event to the rest of the application. This helps us differentiate between a swipe style gesture versus a simple tap. The next two variables will help us gauge the distance travelled during our touch event. Finally, the last two will let us store the starting point of our event.

  bind: (elements...) ->
    for elem in elements
      elem.addEventListener "touchstart", (event) =>
       @handleStart(event, elem)
      elem.addEventListener "touchmove", (event) =>
       @handleMove(event, elem)
      elem.addEventListener "touchend", (event) =>
       @handleEnd(event, elem)

First we want to add a bind method to our class to attach the appropriate touch events to whichever DOM element (or elements) specified. This method handles one or more elements and attaches the touchstart, touchmove and touchend events and delegates the appropriate handler methods for each type.

  handleStart: (event, elem) ->
    if event.touches.length is 1
      @touchDX = 0
      @touchDY = 0
      @touchStartX = event.touches[0].pageX
      @touchStartY = event.touches[0].pageY

Once we have the events attached to the correct elements, we need to tell our class how to handle the various event types along the way. Here we store our initial values once the touch event is initially registered.

  handleMove: (event, elem) ->
    if event.touches.length > 1
      @cancelTouch(elem)
      return false

    @touchDX = event.touches[0].pageX - @touchStartX
    @touchDY = event.touches[0].pageY - @touchStartY

Next we need a method fired during the portion of the touch event where our finger is moving across the screen. During this event we want to detect if another touch event is registered and reset our listeners to avoid complications. We also want to update our two variables tasked with telling us the distance our touch event has travelled.

  handleEnd: (event,elem) ->
    dx = Math.abs(@touchDX)
    dy = Math.abs(@touchDY)

    if (dx > @horizontalSensitivity) and (dy < (dx * 2 / 3))
      if @touchDX > 0 then @emitSlideRight() else @emitSlideLeft()

    if (dy > @verticalSensitivity) and (dx < (dy * 2 / 3))
      if @touchDY > 0 then @emitSlideDown() else @emitSlideUp()

    @cancelTouch(event, elem)
    false

Once a touchend event is triggered and the gesture is complete, we need to gather the total distance travelled both horizontally and vertically. Once we have these values, we can use our sensitivity variables to determine if the distance of the event is great enough to warrant emitting to the rest of our application. We can also use the positive or negative value of our difference/distance variable to indicate the specific direction of our event and emit the correct notification.

  emitSlideLeft: -> @emit 'swipe:left'
  emitSlideRight: -> @emit 'swipe:right'
  emitSlideUp: -> @emit 'swipe:up'
  emitSlideDown: -> @emit 'swipe:down'

Finally, once we have successfully registered a touch event and determined that it passes our sensitivity threshold set initially, we need to emit the correct event to our application. Using EventEmitter we can easily send out the proper event notification with very little code.

With our new touch class, we can now bind touch events to any elements in our application.

Touch.bind document.getElementById('menu')

Once we have our bindings set, performing actions based our touch events is as easy as calling a method when a touch event is detected.

Touch.on 'swipe:left', => @toggleMenu()

While this class certainly doesn't handle every possible interaction on a handheld or tablet device, it does provide you with a great base for handling basic touch events throughout your application with very little code. I welcome and look forward to any and all feedback, suggestions and improvements and hope this little class proves helpful!


Littlelines in the Wild (Spring Edition)

Written by Matt Sears

The conference season is in full swing for the web development community. Littlelines will be participating in quite a few events and conferences around the world. If you attend any Ruby on Rails or Web Design events this year, there's a good chance you might run into a Littleines' crew member or two. If you see us, stop by and say hi, we'd love to meet you! Here are a few upcoming events where you can find us.

Ruby Midwest

April 3rd-4th in Kansas City, Missouri

Ruby Midwest

RubyMidwest is back! I had the pleasure of attending this premiere Kansas City Ruby event in 2011. This year, I returned as a speaker and had a blast presenting. This go around featured two awesome keynotes from Michael Feathers and James Edward Gray II. We will look forward to attending this event again next year.

Dayton Ruby Brigade

April 23rd in Dayton, Ohio

Dayton Ruby Brigade

Every month we host the Dayton Ruby Brigade where share all things Ruby. This month is no exception. Whether you're a seasoned Ruby developer or just learning join us each month at the Littlelines HQ for talks, pizza, drinks, and good times.

Hybridconf

August 15th-16th in Cardiff, Wales

Hybrid Conf

HybridConf is a brand new conference dedicated to marriage between design and development. This speaks to the heart of Littlelines I was honored to be asked to speak at their inaugural event. Located in Cardiff, South Whales, the conference includes an incredable lineup with such names Cameron Moll and Peter Cooper.

Steel City Ruby

August 16th-17th in Pittsburgh, Pennsylvania

Steel City Ruby

One of the highlights of 2012, Steel City Ruby conference is back this summer. We're happily sponsoring this event again and super excited to be apart of this amazing conference. If youre in the Pittsburgh area in August, do yourself a favor and register for a ticket soon. Oh, did I mention that we will be designing the tee shirts again?

Check back later this summer and find out where we will be in the Fall and Winter!


Say Hi to Jeremy

Written by Beth Morris

12/ 3

Beth Morris

We are happy to host a very talented young designer for the next three weeks. Jeremy Purvis is from Cincinnati, Ohio and is currently a second year student at the School of Advertising Art in Kettering Ohio. He will be helping us with web design and a chance to test his skills in a little front-end development as well.

image

Jeremy has a background in fine arts and computer graphics, which has lead him to his ultimate passion - graphic design. He is most excited to experience working day in and day out in a creative and fund business environment while externing with us. Besides designing in Illustrator Jeremy loves to play guitar and bike ride BMX style of course in his spare time.

We can’t wait to see what we can teach Jeremy over the next couple weeks and also to see what we might in turn learn from him.


best websites of the world