SHARE
TWEET

Zoom In Out Pinch

a guest Nov 20th, 2011 637 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --gestures
  2. --this could be improved by considering angles between tocuhes
  3. --angles could be used to zoom in on something other than the center
  4. --angles could also be used for rotate behavior
  5. function setup()
  6.     --create a table of 10 random shapes[drawn as ellipses in draw()]
  7.     s = {}
  8.     for i = 1,10 do
  9.         x = math.random() * WIDTH
  10.         y = math.random() * HEIGHT
  11.         w = math.random() * WIDTH
  12.         h = math.random() * HEIGHT
  13.         s[i] = {x=x,y=y,w=w,h=h} 
  14.     end
  15.     --set up two touch tables
  16.     t1 = {id=nil}
  17.     t2 = {id=nil}
  18.     --set current scale, 1 is 100%
  19.     cs = 1
  20. end
  21.  
  22. function touched(touch)
  23.     if touch.state == BEGAN then
  24.         --store two touches
  25.         if t1.id == nil then
  26.             t1 = touch
  27.         elseif t2.id == nil then
  28.             t2 = touch
  29.         end
  30.     elseif touch.state == MOVING then
  31.         if (t1.id ~= nil) and (t2.id ~= nil) then
  32.             --if we have both touches active
  33.             if touch.id == t1.id then
  34.                 --determine the current (cd) and previous (pd) 
  35.                 --distances between the two touches
  36.                 --using the movement of the first touch
  37.                 t1 = touch  
  38.                 --the systems keeps prevX and PrevY updated
  39.                 --(you won't find them set anywhere in this code)         
  40.                 pv = vec2(t1.prevX,t1.prevY)  
  41.                 t1v = vec2(t1.x,t1.y)
  42.                 t2v = vec2(t2.x,t2.y)
  43.                 cd = t1v:dist(t2v)
  44.                 pd = pv:dist(t2v)           
  45.             end 
  46.             if touch.id == t2.id then
  47.                 --using the movement of the first touch
  48.                 t2 = touch
  49.                 pv = vec2(t2.prevX,t2.prevY) 
  50.                 t1v = vec2(t1.x,t1.y)
  51.                 t2v = vec2(t2.x,t2.y)
  52.                 cd = t1v:dist(t2v)
  53.                 pd = pv:dist(t1v)                         
  54.             end
  55.             --recaculate current scale
  56.             --based on the change in current and previous distance
  57.             cs = cs * (cd/pd)
  58.         end    
  59.     else --if touch.state == ENDED then
  60.         if t1.id ~= nil then
  61.             if touch.id == t1.id then
  62.                 --forget touch 1
  63.                 t1 = {id=nil}
  64.             end
  65.         end
  66.         if t2.id ~= nil then
  67.             if touch.id == t2.id then
  68.                 --forget touch 2
  69.                 t2 = {id=nil}
  70.             end 
  71.         end       
  72.     end
  73. end
  74.  
  75. function draw()
  76.     background(0,0,0,255)
  77.     --scale the drawing area to the current scale
  78.     scale(cs)
  79.     --keep the shapes in the center
  80.     --without this zoom out would push everthing to the lower right
  81.     translate((WIDTH/2/cs)-WIDTH/2,(HEIGHT/2/cs)-HEIGHT/2)
  82.     strokeWidth(30)
  83.     stroke(0, 255, 57, 255) 
  84.     fill(6, 10, 239, 255)
  85.     for i = 1,10 do
  86.         ellipse(s[i].x,s[i].y,s[i].w,s[i].h)
  87.     end    
  88. end
  89.  
  90.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top