Paint Program with moving zooming and rotating canvas

This is a simple paint program, with one cool feature: you can zoom in to the canvas and draw details and then zoom out again.
Please have a look at my own ontouch function. I try to optimize it to my best abilities.
Every now and again pinch movements still leave stray brush marks, but any suggestions are welcome to get rid of them completely.

--# Main
-- Paint

-- Use this function to perform your initial setup
function setup()
    parameter.color("brushcolor",color(255,255,255,125))
    parameter.integer("brushsize",1,250,10)
    print("This is a simple paint program.  Set the brush color and size with the parameters. Pinch will move, zoom and rotate the canvas.")
    zoom=1
    zoomold=1
    touches ={}
    handle=vec2(WIDTH/2,HEIGHT/2)
    
    handle1= vec2(0,0)
    handleold=vec2(0,0)
    origin=vec2(WIDTH/2,HEIGHT/2)
    brush=vec2(0,0)
    v={}
    nt=0
    length1=0
    length2=0
    angle1=0
    angle2=0
    angle=0
    angleold=0
    m = mesh()
    drw=false
   -- make a soft round brush image 
    brushim=image(600,600) 
    setContext(brushim)
    noFill()
    strokeWidth(2)
    blendMode(NORMAL)
    for i=1,200 do        
        stroke(255,255,255,255*math.sin(i/200))
        ellipse(300,300,600-i*3)   
    end
    setContext()  
   photo=image(WIDTH,HEIGHT)
    setContext(photo)
background(0)
    setContext()
    m.texture = photo    
 cw,ch = spriteSize(photo)
 rIdx = m:addRect(0, 0, 0, 0)
        
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
fill(color(0))
    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    m:setRect(rIdx, origin.x,origin.y, cw*zoom,ch*zoom,angle)
    m:draw()    
    if drw then
        setContext(photo)
        tint(brushcolor)
        sprite(brushim,brush.x,brush.y,brushsize,brushsize)
        setContext()
    end
end

function touched(t)
    
if t.state== BEGAN then 
        if nt==0 then
            touches[1]=t
            nt=1
            v[1]=vec2(t.x,t.y)
            drw=false
        elseif nt==1 then
            touches[2]=t
            nt=2
            v[1]=vec2(touches[1].x,touches[1].y)
            v[2]=vec2(t.x,t.y)
            length1=v[1]:dist(v[2])
            angle1=(v[2]-v[1]):angleBetween(vec2(0,1))
            angle2=angle1
            handle=(v[1]+v[2])/2
            handle1=handle
            origin0=origin
            zoomold=zoom
            angleold=angle
            drw=false
        end
        elseif t.state==ENDED then
            if nt~=0 then
                touches[nt]=nil
                nt=nt-1
                length1=0
                length2=0
                angle1=0
                angle2=0
                drw=false
            end
        else
            if nt==1 then
                touches[1]=t
                v[1]=vec2(t.x,t.y) 
                brush=(v[1]-(origin)):rotate(-angle)/zoom+vec2(cw,ch)/2
                drw=true
            elseif nt==2 then
                if touches[1].id==t.id then
                    touches[1]=t
                    v[1]=vec2(touches[1].x,touches[1].y)
                elseif touches[2].id==t.id then
                    touches[2]=t
                    v[2]=vec2(touches[2].x,touches[2].y)
                end
            drw=false
            length2=v[1]:dist(v[2])
            angle2=(v[2]-v[1]):angleBetween(vec2(0,1))
        end
        
    end
    if length1~=0 and length2~=0 then  
            zoom=zoomold*length2/length1
            angle=angleold+angle1-angle2
            handle=(v[1]+v[2])/2
            origin=zoom/zoomold*(origin0-handle1):rotate(angle-angleold)+handle
            drw=false
    end
end

http://youtu.be/uz9ZZ_k8CKg