Function Question

@dave1707 the URL request aka the code didn’t load the picture. Did you test before you sent me that?

displayMode(FULLSCREEN)

function setup()
    bcolor= 0
    x=WIDTH/2
    y=HEIGHT/2+100
    w,h=200,50
    left=x-w/2
    right=x+w/2
    bottom=y-h/2
    top=y+h/2
    rectMode(CENTER) 
    str=""
end

function draw()
    background(40, 40, 50)
    if img==nil then
        fill(255)
        text("Loading image",WIDTH/2,HEIGHT/2)
    else
        sprite(img,WIDTH/2,HEIGHT/2,700)
    end

       if bcolor == 0 then
        fill(255, 0, 0, 255)
    else
        fill(128,0,0,255)
    end    
    rect(x,y,w,h)
    fill(255)
    strokeWidth(6)
    text(str,x,y)

end

function getImg()
    http.request("https://"..str,gotImage)
end

function gotImage(image,status,header)
    img=image
end

function touched(t)
    if t.x > left and t.x < right and t.y > bottom and t.y < top then
        showKeyboard()
        bcolor=1
        str=""
    end
end

function keyboard(key)
    if key==RETURN then
        bcolor=0
        val=tostring(str)
    else
        str=str..key
    end
end

@dave1707 ok I’m at the realization you were messing with me, yet another rabbit hole.

@Jarc Add getImg() to the keyboard function when RETURN is pressed.

PS. You don’t need the val=tostring(str). str is already a string.

function keyboard(key)
    if key==RETURN then
        bcolor=0
        val=tostring(str)
        getImg()
    else
        str=str..key
    end
end

@Jarc the parameters in the sidebar can’t be rendered outside of the sidebar unless you handle all the drawing and interaction yourself, using the sidebar allows you to add parameters with much less code than writing custom ones from scratch

@dave1707 Thank you very much. This is a continuation of the uploading image but with a added feature. How do I get this line

 img3=str

To read as that and this

img3=readImage()

So basically I need the spritesheet url to read as readImage() so when I press extract it will work. May seem like a useless question to you but for a project I’m working on it will be useful for me. Here is the full code and a link for the spritesheet url to copy and paste in.

Link www.kindpng.com/picc/m/12-129142_exemplo-de-spritesheet-sprite-sheet-megaman-png-transparent.png

function setup()
    bcolor= 0
    x2=WIDTH/2
    y2=HEIGHT/2+100
    w2,h2=200,50
    left=x2-w2/2
    right=x2+w2/2
    bottom=y2-h2/2
    top=y2+h2/2
    rectMode(CENTER) 
    str=""
    
    ---extract
    tab2={}
    delay=0
    i=1
    img3=str
    iw=img3.width
    ih=img3.height
    dx,dy=0,HEIGHT/2-300
    sizeX=iw
    sizeY=ih    
    spriteMode(CORNER)
    parameter.text("wide")
    parameter.text("high")
    parameter.action("extract",extract)
    parameter.integer("scale",1,500,ih)
    img3=image(iw,ih)    
    setContext(img3)
    background(255)
    sprite(img3,0,0)
    setContext()    
    w=img3.width
    h=img3.height
        
end

function draw()
    background(40, 40, 50)
    if img3==nil then
        fill(255)
        text("Loading image",WIDTH/2,HEIGHT/2)
    else
        sprite(img3,WIDTH/2,HEIGHT/2,700)
    end

    if bcolor == 0 then
        fill(255, 0, 0, 255)
    else
        fill(128,0,0,255)
    end    
    rect(x2,y2,w2,h2)
    fill(255)
    strokeWidth(6)
    text(str,x2,y2)
    
    ---extract
    delay=delay+1    
    if #tab2>0 then
        if delay>10 then
            delay=1
            i=i+1
        end
        if i>#tab2 then
            i=1
        end
        sprite(tab2[i],dx,dy,scale)
    else
        sprite(img3,dx,dy,400)
    end
end

function getImg()
    http.request("https://"..str,gotImage)
  
end

function gotImage(image,status,header)
    img3=image
end

function touched(t)
    if t.x > left and t.x < right and t.y > bottom and t.y < top then
        showKeyboard()
        bcolor=1
        str=""
    end
end

function keyboard(key)
    if key==RETURN then
        bcolor=0
        val=tostring(str)
        getImg()
    else
        str=str..key
    end
end

function extract()
    tab2={}
    dx,dy=WIDTH/2,HEIGHT/2
    if wide=="" or high=="" then
        return
    end
    stepX=iw//wide
    stepY=ih//high
    for y=1,sizeY,stepY do
        for x=1,sizeX,stepX do
            table.insert(tab2,img3:copy(x,y,stepX,stepY))
        end
    end
end

Thanks again! I know I always swarm you with questions.

@Jarc Here’s the working code. You can go thru it and remove what’s not needed. To run this, just tap the red button then press enter without keying anything. I set the URL in setup to the variable aa. Then in the keyboard function when you press RETURN, I move aa to str. That way I didn’t have to key anything to get it working. You can remove those when you want to key a URL.

function setup()
    aa="www.kindpng.com/picc/m/12-129142_exemplo-de-spritesheet-sprite-sheet-megaman-png-transparent.png"
    bcolor= 0
    x2=WIDTH/2
    y2=HEIGHT/2+100
    w2,h2=200,50
    left=x2-w2/2
    right=x2+w2/2
    bottom=y2-h2/2
    top=y2+h2/2
    rectMode(CENTER) 
    str=""

    ---extract
    tab2={}
    delay=0
    i=1
    img3=str
    iw=img3.width
    ih=img3.height
    dx,dy=0,HEIGHT/2-300
    sizeX=iw
    sizeY=ih 
    print(iw,ih)   
    spriteMode(CORNER)
    parameter.text("wide")
    parameter.text("high")
    parameter.action("extract",extract)
    parameter.integer("scale",1,500,200)
    img3=image(iw,ih)    
    setContext(img3)
    background(255)
    sprite(img3,0,0)
    setContext()    
    w=img3.width
    h=img3.height

end

function draw()
    background(40, 40, 50)
    if img3==nil then
        fill(255)
        text("Loading image",WIDTH/2,HEIGHT/2)
    end

    if bcolor == 0 then
        fill(255, 0, 0, 255)
    else
        fill(128,0,0,255)
    end    
    rect(x2,y2,w2,h2)
    fill(255)
    strokeWidth(6)
    text(str,x2,y2)

    ---extract

    if #tab2>0 then
        delay=delay+1    
        if delay>10 then
            delay=1
            i=i+1
        end
        if i>#tab2 then
            i=1
        end
        sprite(tab2[i],dx,dy,scale)
    else
        sprite(img3,dx,dy)
    end
end

function getImg()
    http.request("https://"..str,gotImage)
end
    
function gotImage(image,status,header)
    img3=image
    iw=img3.width
    ih=img3.height
end

function touched(t)
    if t.x > left and t.x < right and t.y > bottom and t.y < top then
        showKeyboard()
        bcolor=1
        str=""
    end
end

function keyboard(key)
    if key==RETURN then
        bcolor=0
        str=aa              -- remove this line
        getImg()
    else
        str=str..key
    end
end

function extract()
    tab2={}
    dx,dy=WIDTH/2,HEIGHT/2
    if wide=="" or high=="" then
        return
    end
    sizeX=iw
    sizeY=ih
    stepX=iw//wide
    stepY=ih//high
    for y=1,sizeY,stepY do
        for x=1,sizeX,stepX do
            table.insert(tab2,img3:copy(x,y,stepX,stepY))
        end
    end
end

@dave1707 You’re the man! Thanks! I see now why mine wasn’t working, I was so close yet so far.