It’s ok when run on android, but slow on ios 7.
-- -- Abstract: SnapshotPaint sample app -- -- Version: 2.0 -- -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2013 Corona Labs Inc. All Rights Reserved. -- -- Supports Graphics 2.0 ------------------------------------------------------------ local w = display.actualContentWidth local h = display.actualContentHeight display.setDefault( "background", 1 ) local snapshot = display.newSnapshot( w,h ) snapshot:translate( display.contentCenterX, display.contentCenterY ) -- Don't record changes to save memory. -- -- However, saving memory comes at a cost. If the GL context changes, -- the snapshot will \*not\* be able to re-draw the contents. For example, -- on some Android devices, this can occur after a suspend/resume cycle. snapshot.canvasMode = "discard" local previousX, previousY local threshold = 0 local thresholdSq = threshold\*threshold local pointA = {} local pointB = {} local t = display.newText("", 100 , 100 , native,systemFont, 16) t:setFillColor(0, 0, 0) local monitorMem = function() collectgarbage() local memory = collectgarbage("count") -- print( "MemUsage: " .. memory ) local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000 -- print( "TexMem: " .. textMem ) -- local textMem1 = system.getInfo( "maxTextureUnits" ) / 1000000 -- print( "TexMem: " .. textMem1 ) -- local textMem2 = system.getInfo( "maxTextureSize" ) / 1000000 -- print( "TexMem: " .. textMem2 ) t.text = "总使用内存:" .. memory .."\n".. "纹理总大小:" .. textMem .."\n" end Runtime:addEventListener( "enterFrame", monitorMem ) local function draw( x, y ) local o = display.newImage( "brush.png", x, y ) o:setFillColor( 1, 0, 1 ) -- magenta paint o:scale( 0.1, 0.1) snapshot.canvas:insert( o ) snapshot:invalidate( "canvas" ) -- accumulate changes w/o clearing end function lengthOf( a, b ) local width, height = b.x-a.x, b.y-a.y return (width\*width + height\*height)^0.5 end function interpolation( pA, pB ) local len = lengthOf(pA, pB) -- local len = self:lengthOf(pA, pB) / self.interDensity if ((len ~= 0) and (len \> 2)) then local cos = (pB.x - pA.x) / len local sin = (pB.y - pA.y) / len local floor = math.floor(len) local average = len / math.ceil(len) for i = 1, floor do local drawPointX = pA.x + i \* average \* cos local drawPointY = pA.y + i \* average \* sin draw( drawPointX, drawPointY ) end end end local function listener( event ) local x,y = event.x - snapshot.x, event.y - snapshot.y if ( event.phase == "began" ) then -- previousX,previousY = x,y pointA.x = x pointA.y = y draw( x, y ) elseif ( event.phase == "moved" ) then -- local dx = x - previousX -- local dy = y - previousY -- local deltaSq = dx\*dx + dy\*dy -- if ( deltaSq \> thresholdSq ) then pointB.x = x pointB.y = y draw( x, y ) interpolation( pointA, pointB ) pointA.x = pointB.x pointA.y = pointB.y -- previousX,previousY = x,y -- end elseif ( event.phase == "ended") then -- pointA = nil -- pointB = nil -- pointA = {} -- pointB = {} end end Runtime:addEventListener( "touch", listener ) local instruction = display.newText{ text="Touch screen to start painting", x = display.contentCenterX, y = 30, fontSize=12, } instruction:setFillColor( 0 ) -- local a = display.newImageRect( "abc\_on.png", 64, 64) -- a.x, a.y = display.contentCenterX - 100, display.contentCenterY