clip, translate compatible [edit: and scale compatible]

-- an extension of clip function:
--  clip(x,y,w,h) is as usual
--  clip(x,y,w,h,true) is using the current translation state to define its position

local stdClip = clip -- remember standard clip function
clip = function(x,y,w,h,relative)
    if x == nil then stdClip() return end
    if relative then -- translate the clip using current translation
        local m = modelMatrix()
        local x0,y0 = m[13],m[14]
        stdClip(x0+x,y0+y,w,h)
    else
        stdClip(x,y,w,h)
    end
end

i’ve often been annoyed by clip() being absolute coordinates only, so i thought that was worth sharing.
It’s fully backward compatible with your existing code.

having a look i found that one too: http://codea.io/talk/discussion/comment/13172/#Comment_13172 which is also manaaging scale

so here is a backward compatible version

-- New clip function, affected by translate() and scale()
local _clip = clip
function clip(x, y, w, h,relative)
    if x ~= nil then
        if relative then
        local m = modelMatrix()
        x = x * m[1] + m[13]
        y = y * m[6] + m[14]
        w = w * m[1]
        h = h * m[6]
        end
        _clip(x, y, w, h)
    else
        _clip()
    end
end