How to efficiently erase pixels from an image

@Luatee you would have to render it back to the original image instead of the scratch image (or, rather, swap the original and scratch images after rendering) but that’s not difficult.

Ahh I understand, I’ll give it a try now. In the meantime I had an idea using a shader and discarding pixels in a certain position, passed by a uniform vec2 that holds the touch position then discard all pixels in a radius of it then draw that to the original image using setContext? I’m not sure how fast that will be, I’m guessing 30fps though…

@Luatee isn’t that what Ignatz’ code does? I don’t think it would be as bad as 30fps, but I can say that mine runs at full speed.

I think that alpha masks are the best way to achieve this, but whether to render them using a shader or blend mode depends on what you then want to do with them and how long the mask should persist (ie should you just apply it and then get rid of it, or should you have the facility to work on both the image and mask independently?)

Yes, it does actually I hadn’t fully looked at it til I saw it on codea community, but that runs at only about the same speed as what I have already surprisingly so it still doesn’t give a smooth outcome

Nice one @Andrew_Stacey
I get 30hz on ipad1, i just refactored your code a little bit to get 60Hz.
(actually it is still 30Hz when you draw the mask, but the 60 the rest of the time, which makes your solution the fastest one).

local mask,canvas
    
function setup()
    mask = image(WIDTH,HEIGHT)
    setContext(mask)
    background(0,0,0,0)
    setContext()
    canvas = image(WIDTH,HEIGHT)
    parameter.number("radius",1,50,20)
    fps = {}
    nfps = 20
    for k=1,nfps do
        table.insert(fps,1/60)
    end
    parameter.watch("math.floor(nfps/afps)")
end

function draw()
    table.remove(fps,1)
    table.insert(fps,DeltaTime)
    afps = 0
    for k,v in ipairs(fps) do
        afps = afps + v
    end
    background(37, 147, 155, 255)
    sprite(canvas,0,0)
end

function touched(touch)
    setContext(mask)
    noStroke()
    fill(0,0,0, 255)
    ellipse(touch.x,touch.y,radius)
    setContext()
    spriteMode(CORNER)
    pushStyle()
    setContext(canvas)
    background(0,0,0,0)
    sprite("Cargo Bot:Codea Icon",0,0,WIDTH,HEIGHT)
    blendMode(ZERO,ONE,DST_ALPHA,ZERO)
    sprite(mask,0,0)
    setContext()
    canvas.premultiplied = false
    popStyle()
end

@Andrew_Stacey That’s hardly covered in the documentation and the blend modes example doesn’t cover it… So can you tell me how it works?

The problem is its using two images which isn’t how my app works

@Luatee New version on Codea Community with only one image. @Jmv38 please try it as it should be a lot faster.

@SkyTheCoder I’ll do that tomorrow.

@Andrew_Stacey you nailed it! That’s exactly what I need, thanks.

@andrew_stacey how do i get your new version? Cant find it on CC. Can you post a link?

I found it on CC. Very good job: short and fast.

Hiya, I,m not on CC at present, could this code be posted?

Many thanks, Brookesi

@brookesi - Ideally, everything will be on CC in future, so (unless you have a technical problem), please get onto it