Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --gestures
- --this could be improved by considering angles between tocuhes
- --angles could be used to zoom in on something other than the center
- --angles could also be used for rotate behavior
- function setup()
- --create a table of 10 random shapes[drawn as ellipses in draw()]
- s = {}
- for i = 1,10 do
- x = math.random() * WIDTH
- y = math.random() * HEIGHT
- w = math.random() * WIDTH
- h = math.random() * HEIGHT
- s[i] = {x=x,y=y,w=w,h=h}
- end
- --set up two touch tables
- t1 = {id=nil}
- t2 = {id=nil}
- --set current scale, 1 is 100%
- cs = 1
- end
- function touched(touch)
- if touch.state == BEGAN then
- --store two touches
- if t1.id == nil then
- t1 = touch
- elseif t2.id == nil then
- t2 = touch
- end
- elseif touch.state == MOVING then
- if (t1.id ~= nil) and (t2.id ~= nil) then
- --if we have both touches active
- if touch.id == t1.id then
- --determine the current (cd) and previous (pd)
- --distances between the two touches
- --using the movement of the first touch
- t1 = touch
- --the systems keeps prevX and PrevY updated
- --(you won't find them set anywhere in this code)
- pv = vec2(t1.prevX,t1.prevY)
- t1v = vec2(t1.x,t1.y)
- t2v = vec2(t2.x,t2.y)
- cd = t1v:dist(t2v)
- pd = pv:dist(t2v)
- end
- if touch.id == t2.id then
- --using the movement of the first touch
- t2 = touch
- pv = vec2(t2.prevX,t2.prevY)
- t1v = vec2(t1.x,t1.y)
- t2v = vec2(t2.x,t2.y)
- cd = t1v:dist(t2v)
- pd = pv:dist(t1v)
- end
- --recaculate current scale
- --based on the change in current and previous distance
- cs = cs * (cd/pd)
- end
- else --if touch.state == ENDED then
- if t1.id ~= nil then
- if touch.id == t1.id then
- --forget touch 1
- t1 = {id=nil}
- end
- end
- if t2.id ~= nil then
- if touch.id == t2.id then
- --forget touch 2
- t2 = {id=nil}
- end
- end
- end
- end
- function draw()
- background(0,0,0,255)
- --scale the drawing area to the current scale
- scale(cs)
- --keep the shapes in the center
- --without this zoom out would push everthing to the lower right
- translate((WIDTH/2/cs)-WIDTH/2,(HEIGHT/2/cs)-HEIGHT/2)
- strokeWidth(30)
- stroke(0, 255, 57, 255)
- fill(6, 10, 239, 255)
- for i = 1,10 do
- ellipse(s[i].x,s[i].y,s[i].w,s[i].h)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement