Advertisement
Guest User

Utils.modulate example

a guest
Dec 21st, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bg = new BackgroundLayer
  2.     backgroundColor: "#56B4D3"
  3.  
  4. box = new Layer
  5.     borderRadius: 10
  6.     backgroundColor: "#7b4397"
  7.     opacity: 1
  8.    
  9. box.center()
  10. box.draggable.enabled = true
  11.  
  12. originX = box.midX
  13. originY = box.midY
  14.  
  15. dist = 0
  16.  
  17.  
  18. box.on Events.DragMove, ->
  19.     delta =
  20.         x: originX- Events.touchEvent(event).clientX
  21.         y: originY - Events.touchEvent(event).clientY
  22.        
  23.     dist = Math.abs(delta.x) + Math.abs(delta.y)
  24.    
  25.     # dist is the value that we will be adapting
  26.     # 0 - 400 represents the range of that value
  27.     # 1 - 0.6 represents the range that we wish to adapt it to
  28.     # the boolean sets whether the new value should be limited to the range set
  29.     # or whether it can continue outside it
  30.     #
  31.     # the greater the dist value, the smaller the scale we want
  32.     boxScale = Utils.modulate(dist,[0,400],[1, 0.6], true)
  33.    
  34.     # here we do the same with the opacity, adapting the diff value differently
  35.     boxOpacity = Utils.modulate(dist,[0,400],[1, 0.5], true)
  36.  
  37.  
  38.     box.scale = boxScale
  39.     box.opacity = boxOpacity
  40.    
  41.    
  42.  
  43. #when the user has stopped dragging we animate the box to return to its original position
  44. box.on Events.DragEnd, ->
  45.     box.animate({
  46.        
  47.         properties: {
  48.             midX: originX
  49.             midY: originY
  50.             scale: 1
  51.             opacity: 1
  52.         }
  53.         time: 0.3
  54.         curve: "spring(200, 40, 0)"
  55.        
  56.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement