Odd Choppiness in very simple (non-physics) program

Running the code below, it is very choppy (20-30 FPS, it claims, but if you drag the triangle it actually moves 1-10 times per second). If you pause the program then press play again the framerate magically jumps to 60FPS with no choppiness or lag. I don’t know what the problem is, it is quite perplexing.

Triangle = class()

function Triangle:init(x)
    self.p = {vec2(0,0),vec2(100,0),vec2(50,100*math.sin(math.rad(60)))}
    self:cc()
    local ta = vec2(WIDTH/2,HEIGHT/2)-self.centroid
    self:translate(ta.x,ta.y)
    self:ca()
    self.m = mesh()
    self.m.vertices = self.p
    self.m:setColors(color(0,0,0,255))
end

function Triangle:draw()
    pushStyle()
    fill(0,0,0,0)
    strokeWidth(1)
    stroke(0,0,255)
    ellipse(self.p[1].x,self.p[1].y,25)
    ellipse(self.p[2].x,self.p[2].y,25)
    ellipse(self.p[3].x,self.p[3].y,25)
    stroke(255,0,0)
    ellipse(self.p[1].x,self.p[1].y,5)
    ellipse(self.p[2].x,self.p[2].y,5)
    ellipse(self.p[3].x,self.p[3].y,5)
    self.m:draw()
    ellipse(self.centroid.x,self.centroid.y,25)
    stroke(0,0,255)
    ellipse(self.centroid.x,self.centroid.y,5)
    popStyle()
end

function Triangle:touched(t)
    if t.state == BEGAN then
        local op = false
        if not self.p[7] then
        for i=1,3 do
            if not self.p[i+3] then
                if self.p[i]:dist(vec2(t.x,t.y)) < 25 then
                    self.p[i+3] = t
                    op = true
                    break
                end
            end
        end
        end
        if not op then
            if self.centroid:dist(vec2(t.x,t.y)) < 25 then
                self.p[7] = t
            end
        end
    end
    if t.state == MOVING then
        if self.p[7] and self.p[7].id == t.id then
            self:translate(t.deltaX,t.deltaY)
            self.p[4],self.p[5],self.p[6] = nil,nil,nil
        end
        for i=1,3 do
            if self.p[i+3] and self.p[i+3].id==t.id then
                self.p[i]=self.p[i]+vec2(t.deltaX,t.deltaY)
            end
        end
    end
    if t.state == ENDED or t.state == CANCELLED then
        for i=1,3 do
            if self.p[i+3] and self.p[i+3].id==t.id then
                self.p[i+3] = nil
            end
        end
        if self.p[7] and self.p[7].id == t.id then
            self.p[7] = nil
        end
    end
    self.m.vertices={self.p[1],self.p[2],self.p[3]}
    self:cc()
    self:ca()
end
function Triangle:translate(dx,dy)
    for i=1,3 do
        self.p[i] = self.p[i] + vec2(dx,dy)
    end
    self.centroid = self.centroid + vec2(dx,dy)
end
function Triangle:cc()
    self.centroid = vec2((1/3)*(self.p[1].x+self.p[2].x+self.p[3].x),(1/3)*(self.p[1].y+self.p[2].y+self.p[3].y))
end
function Triangle:ca()
    local d1=self.p[1]:dist(self.p[2])
    local d2=self.p[2]:dist(self.p[3])
    local d3=self.p[3]:dist(self.p[1])
    local s=((d1+d2+d3)/2)
    self.area=math.sqrt(s*(s-d1)*(s-d2)*(s-d3))
end

displayMode(OVERLAY)
function setup()
    print("Hello World!")
    tr=Triangle()
    score = 0
end
function draw()
    background(255)
    tr:draw()
    output.clear()
    print(tr.area)
    score = score + tr.area/200000
    print(math.floor(score))
    print(math.floor(1/DeltaTime))
    collectgarbage()
end
function touched(t)
    tr:touched(t)
end

Thank you in advance for your help :slight_smile: