Touch relative to translate and scale [SOLVED]

I’m making a map editor for my game but Im having a bit of a brain freeze when it comes to thinking up what ratios to apply to the touch position for translating the map and zooming in and out.
I want the centre of the map to be 0,0 and then the edge should be 750 away from the centre either way. Any help is well appreciated.


-- mapeditor

-- Use this function to perform your initial setup
function setup()
    mapimg = readImage("Documents:level1")
    mapmesh = mesh()
    maprect = mapmesh:addRect(WIDTH/2,HEIGHT/2,1500,1500)
    mapmesh.texture = mapimg
    tch = {}
    odist,dist = 0,0
    scl = 1
end

function touched(t)
    if t.state == BEGAN then
        for k,v in pairs(tch) do
            if v.z == t.id then
                return
            end
        end
        table.insert(tch,vec3(t.x,t.y,t.id))
    end
    if t.state == MOVING then
        for k,v in pairs(tch) do
            if v.z == t.id then
                v.x,v.y = t.x,t.y
                return
            end
        end
    end
    if t.state == ENDED then
        for k,v in pairs(tch) do
            if v.z == t.id then
                table.remove(tch,k)
            end
        end
    end
end

function zooming()
    local n = 0
    if tch[1] and tch[2] then
        local v1,v2 = vec2(tch[1].x,tch[1].y),vec2(tch[2].x,tch[2].y)
        dist = v1:dist(v2)
        if dist > odist then
            n = 1
        elseif dist < odist then
            n = -1
        elseif dist == odist then
            n = 0
        end
        odist = dist
    end
    return n
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    local zm = zooming()
    scl = scl + zm/50
    scl = math.min(math.max(scl,0.1),1)
    local imgsize = 1500*scl
    local tm = vec2(WIDTH-WIDTH*0.5*(1-scl)-imgsize/2,HEIGHT-HEIGHT*0.5*(1-scl)-imgsize/2)
    translate(tm.x,tm.y)
    scale(scl)
    if tch[1] and tch[2] == nil then
        local t = tch[1]
        local d = (t.x/imgsize)*scl
        local b = 1500/WIDTH
        d = d-0.25
        d = d*2
        d = d*1500
         
        print(d,scl)
    end

    -- Do your drawing here
    mapmesh:draw()
end

Level1 image: https://www.dropbox.com/s/j1xvoteb9wgycpw/Untitled%2034.png

I didn’t get you, do you want to scroll through the map by sliding your finger or…? from what I understood just change the scl from math.max(scl,.1) to math.max(scl,.5)

No if you put that code in to a project and look at it it works with the zoom and the map always stays in the centre of the screen, but when I click somewhere on the map, I want it to give me a position between -750 and 750 on the map relative to the scale of it and where the touch positions are. So no matter what scale the map if I press to the very right bottom corner of the map it should return vec2(750,-750)

Try this. You just had to do d=d/scl and remove d=d*2


-- mapeditor

-- Use this function to perform your initial setup
function setup()
    mapimg = readImage("Documents:Maze")
    mapmesh = mesh()
    maprect = mapmesh:addRect(WIDTH/2,HEIGHT/2,1500,1500)
    mapmesh.texture = mapimg
    tch = {}
    odist,dist = 0,0
    scl = 1
end

function touched(t)
    if t.state == BEGAN then
        for k,v in pairs(tch) do
            if v.z == t.id then
                return
            end
        end
        table.insert(tch,vec3(t.x,t.y,t.id))
    end
    if t.state == MOVING then
        for k,v in pairs(tch) do
            if v.z == t.id then
                v.x,v.y = t.x,t.y
                return
            end
        end
    end
    if t.state == ENDED then
        for k,v in pairs(tch) do
            if v.z == t.id then
                table.remove(tch,k)
            end
        end
    end
end

function zooming()
    local n = 0
    if tch[1] and tch[2] then
        local v1,v2 = vec2(tch[1].x,tch[1].y),vec2(tch[2].x,tch[2].y)
        dist = v1:dist(v2)
        if dist > odist then
            n = 1
        elseif dist < odist then
            n = -1
        elseif dist == odist then
            n = 0
        end
        odist = dist
    end
    return n
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    local zm = zooming()
    scl = scl + zm/50
    scl = math.min(math.max(scl,0.1),1)
    local imgsize = 1500*scl
    local tm = vec2(WIDTH-WIDTH*0.5*(1-scl)-imgsize/2,HEIGHT-HEIGHT*0.5*(1-scl)-imgsize/2)
    translate(tm.x,tm.y)
    scale(scl)
    if tch[1] and tch[2] == nil then
        local t = tch[1]
        local dx = (t.x/imgsize)*scl
        local b = 1500/WIDTH
        dx = dx-0.25
        dx = dx*1500
        dx=dx/scl
        local dy = (t.y/imgsize)*scl
        dy = dy-0.25
        dy = dy*1500
        dy=dy/scl

        print(dx,dy)
    end

    -- Do your drawing here
    mapmesh:draw()
end

Perfect thanks!