Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

created touch classes and cleaned up calls

  • Loading branch information...
commit 36f2973b971a462b8c3fa65f2a3664f89924aad6 1 parent 08a8a10
rabidpraxis authored
Showing with 76 additions and 148 deletions.
  1. +70 −136 src/smooth.core.coffee
  2. +6 −12 src/smooth.layer.coffee
View
206 src/smooth.core.coffee
@@ -50,22 +50,17 @@ $.fn.throwable = (options) ->
# Mouse bindings window publishing setup
self.bind "mousedown", (e) ->
- #--- touchdown event ------------------------------------------------{{{2
+
$(window).bind "touchdown", touch_down_call = ->
- _smg.tap_stopped_animation = true if self[0].animating
+ smooth.tap_stopped_animation = true if self[0].animating
self.stop()
- #-----------------------------------------------------------------------}}}
- #--- touchmoved event -----------------------------------------------{{{2
+
$(window).bind "touchmoved", touch_move_call = (e, vel) ->
- self.move
- tX: vel.x
- tY: vel.y
- #-----------------------------------------------------------------------}}}
- #--- touchthrow event -----------------------------------------------{{{2
+ self.move { tX: vel.x, tY: vel.y }
+
$(window).bind "touchthrow", touch_throw_call = (e, vel) ->
- throw_me vel
- #-----------------------------------------------------------------------}}}
- #--- touchup event --------------------------------------------------{{{2
+ self.toss vel
+
$(window).bind "touchup", touch_up_call = (e) ->
if self.position().left > 0
self.transition(time: .8).transform
@@ -75,8 +70,7 @@ $.fn.throwable = (options) ->
self.transition(time: .8).transform
tX: self.data("container_width") - self.outerWidth()
tY: self.position().top
- #-----------------------------------------------------------------------}}}
- #--- touchcomplete event --------------------------------------------{{{2
+
$(window).bind "touchcomplete", touch_complete_call = ->
# Remove window bindings
$(window).unbind "touchdown", touch_down_call
@@ -84,7 +78,6 @@ $.fn.throwable = (options) ->
$(window).unbind "touchthrow", touch_throw_call
$(window).unbind "touchup", touch_up_call
$(window).unbind "touchcomplete", touch_complete_call
- #-----------------------------------------------------------------------}}}
# Check for boundry animation penetration {{{2
do ->
@@ -115,149 +108,87 @@ $.fn.throwable = (options) ->
self.data("container_width") - self.outerWidth(),
top_pos)
# }}}
- # throw_me(): give me a throw around {{{2
- throw_me = (vel) ->
- self[0].animating = true
- self.toss vel
- start_pos = [self.position().left, self.position().top]
- start_time = (new Date).getTime()
- setTimeout (->
- end_pos = [self.position().left, self.position().top]
- after_speed = [end_pos[0]-start_pos[0], end_pos[1]-start_pos[1]]
- vel_diff = [Math.abs(after_speed[0] - vel.x), after_speed[1] - vel.y]
- stats =
- "Start Pos": start_pos,
- "End Pos": end_pos,
- "Orig Speed": [vel.x, vel.y]
- "Actual Speed": after_speed
- "Difference": vel_diff
- "Time": (new Date).getTime() - start_time
- if end_pos[0] != start_pos[0]
- window.vel_differences.push([Math.abs(vel_diff[0]), stats])
- if window.display_stats
- _oll "Post Throw Stats", stats
- ), 10
-# }}}
# Transition End {{{2
self[0].addEventListener "webkitTransitionEnd", ((event) ->
self[0].animating = false
), false
# }}}
- # Speed Check {{{2
- do ->
- # TODO: Remove, just a speed check
-
- averager = do ->
- avg_val = avg_ct = 0
- {
- avg: (num) ->
- avg_ct++
- avg_val += num
- result: ->
- avg_val/avg_ct
- reset: ->
- avg_val = avg_ct = 0
- }
-
- calc_avg = ->
- for obj in window.vel_differences
- _o obj[0]
- _ol obj[1]
- averager.avg(obj[0])
-
- _o averager.result()
-
- awesome = (start, end, passed) ->
- self.stop()
- throw_me {x: passed, y:0}
- setTimeout ( ->
- if passed < end
- awesome(start, end, passed+1)
- else
- calc_avg()
- ), 10
-
- unless clickable
- awesome(20, 100, 20)
- # }}}
# }}}
# $.tappable(): Endow tapping action on element {{{1
$.fn.tappable = (options) ->
self = $(this)
+
self.bind "mousedown", (mouse_event) ->
- # tap_start: (bubbled item, parent item)
- options.tap_start mouse_event.target, self if options.tap_start?
-
- # tapped callback
- $(window).bind "touchtap", touch_tap_call = (e, tap_time, win_e) ->
- return_obj =
- normal_parent_pos:
- x: mouse_event.offsetX / self.width()
- y: mouse_event.offsetY / self.height()
- target: mouse_event.target
- caller: self
- window_event: win_e
- mouse_event: mouse_event
-
- options.tapped return_obj if options.tapped?
- # stop triggering!!
+ down_event = new SmoothTapEvent(mouse_event, self)
+
+ # Check to see if there should only be one type of click
+ if options.target_class? and not down_event.target.hasClass(options.target_class)
+ $(window).trigger "touchcomplete"
+ options.tap_missed down_event if options.tap_missed?
+ return true
+
+ # Initial touch
+ options.tap_start down_event if options.tap_start?
+
+ # Finger removed
+ $(window).bind "touchtap", touch_tap_call = (e, tap_time) ->
+ options.tapped new SmoothTapEvent(mouse_event, self) if options.tapped?
$(window).trigger "touchcomplete"
$(window).bind "touchcomplete", touch_complete_call = ->
options.tap_complete mouse_event.target, self if options.tap_complete?
$(window).unbind "touchtap", touch_tap_call
$(window).unbind "touchcomplete", touch_complete_call
-# }}}
-# $.holdable(): Endow extended tap action on element {{{1
-$.fn.holdable = (options) ->
- self = $(this)
- self.bind "mousedown", (e) ->
- unless _smg.did_move()
- console.log "held"
+
# }}}
# $.strokeable(): grab your position while sliding across an item {{{1
-$.fn.strokeable = (options) ->
+$.fn.slideable = (options) ->
self = $(this)
- # use pos to calculate relative position change to layer
- pos = {x: 0, y: 0}
- width = self.width()
- height = self.height()
- self.bind "mousedown", (mouse_e) ->
- pos = {x: mouse_e.layerX, y: mouse_e.layerY}
- session_movement = {x: 0, y: 0}
- options.stroke_start self if options.stroke_start?
+ self.bind "mousedown", (mouse_event) ->
+ slide_event = new SmoothSlideEvent mouse_event, self
+
+ options.slide_start slide_event if options.slide_start?
# touchmoved callback
- $(window).bind "touchmoved", touch_stroke_call = (e, vel, win_e) ->
- pos.x += vel.x
- pos.y += vel.y
- session_movement.x += Math.abs(vel.x)
- session_movement.y += Math.abs(vel.y)
- return_obj =
- delta_pos: pos
- normal_parent_pos:
- x: pos.x / width
- y: pos.y / height
- parent_pos:
- x: mouse_e.layerX
- y: mouse_e.layerY
- target: win_e.target
- caller: mouse_e.target
- vel: vel
- session_movement: session_movement
- window_event: win_e
- mouse_event: mouse_e
-
- options.stroked return_obj if options.stroked?
+ $(window).bind "touchmoved", touch_stroke_call = (event, vel) ->
+ slide_event.update_pos(vel)
+ options.sliding slide_event if options.sliding?
$(window).bind "touchcomplete", touch_complete_call = ->
- options.complete mouse_e if options.complete?
+ options.complete mouse_event if options.complete?
$(window).unbind "touchmoved", touch_stroke_call
$(window).unbind "touchcomplete", touch_complete_call
#}}}
+
+class SmoothTapEvent
+ constructor: (@mouse_event, @parent) ->
+ @target = $(@mouse_event.target)
+
+class SmoothSlideEvent
+ constructor: (@mouse_event, @parent) ->
+ @parent_width = @parent.width()
+ @parent_height = @parent.height()
+ @pos = { x: @mouse_event.layerX, y: @mouse_event.layerY }
+ @session_movement = { x: 0, y: 0 }
+
+ update_pos: (vel) ->
+ @pos.x += vel.x
+ @pos.y += vel.y
+ @session_movement.x += Math.abs(vel.x)
+ @session_movement.y += Math.abs(vel.y)
+
+ parent_pos: ->
+ x: @mouse_event.layerX
+ y: @mouse_event.layerY
+
+ normal_pos: ->
+ x: @pos.x / @parent_width
+ y: @pos.y / @parent_height
+
+
class SmoothNotifier
constructor: ->
@mousedown = false
@@ -269,9 +200,12 @@ class SmoothNotifier
this.init()
init: ->
- $(window).bind "mousedown", this.touchdown
- $(window).bind 'mouseup', this.touchup
- $(window).bind "mousemove", this.touchmove
+ $(document).bind "mousedown", this.touchdown
+ $(document).bind 'mouseup', this.touchup
+ $(document).bind "mousemove", this.touchmove
+
+ current_time: ->
+ (new Date).getTime()
did_move: ->
_move = @prev_session_movement
@@ -284,15 +218,15 @@ class SmoothNotifier
touchdown: (event) =>
$(window).trigger "touchdown", [event]
@mousedown = true
- @session_move_time = @session_start_time = (new Date).getTime()
+ @session_move_time = @session_start_time = this.current_time()
@prevX = event.pageX
@prevY = event.pageY
touchup: (event) =>
- $(window).trigger "touchup", [event]
+ $(self).trigger "touchup", [event]
@prev_session_movement = @session_movement
@session_movement = [ 0, 0 ]
- time_now = (new Date).getTime()
+ time_now = this.current_time()
time_since_movement = time_now - @session_move_time
total_tap_time = time_now - @session_start_time
@@ -321,7 +255,7 @@ class SmoothNotifier
@prevX = eX
@prevY = eY
$(window).trigger "touchmoved", [ @velocity, event ]
- @session_move_time = (new Date).getTime()
+ @session_move_time = this.current_time()
window.smooth = new SmoothNotifier()
View
18 src/smooth.layer.coffee
@@ -6,11 +6,10 @@ TRANFORM_TYPE = "3d"
$.fn.toss = (vel) ->
$obj = this
mag = do ->
- unless $obj.data("constraint")?
- if $obj.data("constraint") == "y"
- vel.y
- else if $obj.data("constraint") == "x"
- vel.x
+ if $obj.data("constraint") == "y"
+ vel.y
+ else if $obj.data("constraint") == "x"
+ vel.x
else
Math.sqrt(vel.x * vel.x + vel.y * vel.y)
@@ -18,12 +17,8 @@ $.fn.toss = (vel) ->
speed_add = mag * .21
translate_delta =
- # x: ( vel.x * 10 + speed_add),
- # y: ( vel.y * 10 + speed_add)
x: ( vel.x * 10.20),
y: ( vel.y * 10.20)
- # x: ( vel.x * 7),
- # y: ( vel.y * 7)
translateY = ($obj.position().top + translate_delta.y)
translateX = ($obj.position().left + translate_delta.x)
time_cof = (Math.abs(mag - 180) / 180) * 2.5
@@ -37,9 +32,9 @@ $.fn.toss = (vel) ->
'Time Coeffecient': time_cof
time_add = ( mag * .7 )
+ console.log time_add
$obj.transition
time: .8 + time_add
- # time: 1.5
$obj.transform
tX: translateX
tY: translateY
@@ -59,8 +54,7 @@ $.fn.move = (obj) ->
trans =
tY: o_pos.top + obj.tY
tX: o_pos.left + obj.tX
- @transform trans
-
+ @.transform trans
@
# }}}
# $.stop(): Stop transition animation {{{1
Please sign in to comment.
Something went wrong with that request. Please try again.