[RESOLVED] DP2.0: how about Invalidation of children without clearing cashed image

OK I tried out my idea and it works.

The only issue is how long you have to wait for the validation to take effect - in theory it is the next draw pass, but I had to insert a delay of 300 milliseconds between each update to actually have it kick in (or else it returned a blank texture).

Here’s my code.

Every 300 milliseconds it creates 100 random lines, swaps the two snapshot objects, then clears all the lines.

The end result is it keeps filling up and filling up with lines overlaid.

Just copy and paste the following code into a fresh main.lua (no other files needed) and run it (on the iphone simulator for ‘fullscreen’, heh - can always just set the width and height to whatever device you prefer simulating on).

[lua]-- Set up status bar

display.setStatusBar( display.HiddenStatusBar )


– Set up ----------------------------------------------------

local width  = 320

local height = 480

local snapshots = {

    active =  display.newSnapshot( width, height ),

    storage = display.newSnapshot( width, height ),

}

local drawGroup = display.newGroup()

local group     = drawGroup.parent

for k, v in pairs( snapshots ) do

    v.anchorX = 0

    v.anchorY = 0

end

group:insert( snapshots.active )

snapshots.active.x, snapshots.active.y = 0, 0

– Place the storage group into the active group

snapshots.active:insert( snapshots.storage )

snapshots.storage.x, snapshots.storage.y = -width / 2, -height / 2

– Place the draw group into the active group

snapshots.active:insert( drawGroup )

drawGroup.x, drawGroup.y = -width / 2, -height / 2


– Run event -------------------------------------------------

local function drawLines()

    for i = 1, 100 do

        local line = display.newLine( drawGroup, math.random( 0, width ), math.random( 0, height ), math.random( 0, width ), math.random( 0, height ) )

        line.strokeWidth = math.random( 1,  10 )

        line:setStrokeColor( math.random( 0, 255 ) / 255, math.random( 0, 255 ) / 255, math.random( 0, 255 ) / 255 )

    end

    

end

local function toggleGroups()

    – Swap snapshots

    snapshots.active, snapshots.storage = snapshots.storage, snapshots.active

    – Kill the draw group (and hence everything in it) and recreate it

    drawGroup:removeSelf()

    drawGroup = display.newGroup()

    – Place the active group into the group

    group:insert( snapshots.active )

    snapshots.active.x, snapshots.active.y = 0, 0

    

    – Place the storage group into the active group

    snapshots.active:insert( snapshots.storage )

    snapshots.storage.x, snapshots.storage.y = -width / 2, -height / 2

    – Place the draw group into the active group

    snapshots.active:insert( drawGroup )

    drawGroup.x, drawGroup.y = -width / 2, -height / 2

    

    – Draw more lines

    drawLines()

    

    – Update

    snapshots.active:invalidate()

end

– Repeat 10000 times

timer.performWithDelay( 300, toggleGroups, 10000 )

[/lua]

You can probably simplify the code above a bit - in particular the offsetting of everything cos I’m not really sure on the positioning etc - I just played with values until I saw what I expected.

Same goes for the draw group and parent group, these aren’t likely needed, but after trying to access the numChildren of the snapshot and getting back a nil value, I figured I’d just chuck everything into actual display groups as at least I knew how to manipulate 'em.

eltromac - given the delay in the updating, I would probably not recommend the above swapping system constantly, just in natural breaks (eg the user draws into a normal group, lifts his finger, and in that instant you chuck the new lines into the active snapshot’s drawgroup and update, ready for the next batch of drawing).

Thanks rakoonic,

Really appreciated.

But, i still want to get the solution as corona sdk platform level.

Resolved !!

Thanks all

http://www.coronalabs.com/blog/2013/11/01/snapshot-canvas-paint-brushes-trailing-object-effects-etc/

Resolved !!

Thanks all

http://www.coronalabs.com/blog/2013/11/01/snapshot-canvas-paint-brushes-trailing-object-effects-etc/