Skip to content

Instantly share code, notes, and snippets.

@NerfedWar
Created May 28, 2013 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NerfedWar/5665580 to your computer and use it in GitHub Desktop.
Save NerfedWar/5665580 to your computer and use it in GitHub Desktop.
AutoGist your Codea projects!
DrawCanvas = class()
function DrawCanvas:init(x)
-- create an off screen image to hold what is being drawn
self.canvas = image(1024, 768)
-- set the default pen color, this will change for each line drawn
self.penColor = color(255, 255, 255, 255)
-- get image for particle
self.particle = nil
http.request("http://goo.gl/xV6ha",
function(theImage, status, head)
self.particle=theImage
end
)
-- the size of the particles that make up the line
self.minSize = 32
self.maxSize = 64
self.size = self.minSize
self.step = 0.5
self.frame = 0
-- boolean that determines whether to allow drawing or now
self.drawing = false
self.drawingCount = 0
end
-- enable drawing n the canvas
function DrawCanvas:startDrawing()
self.drawing = true
end
-- disable drawing on the canvas
function DrawCanvas:stopDrawing()
self.drawing = false
end
-- called each frame by codea
function DrawCanvas:draw()
-- use screen context to obtain touch coordinates
setContext()
spriteMode(CENTER)
-- get the angle of the touch vector
px = CurrentTouch.prevX
py = CurrentTouch.prevY
dx = CurrentTouch.deltaX
dy = CurrentTouch.deltaY
dh = math.sqrt(dx*dx + dy*dy)
angle = math.atan2(dy, dx)
-- increment particle size as the line gets longer up to a m self.size = self.size self.step
self.size = self.size + self.step
psize = -1 * self.size * math.sin(math.log(self.size*self.size))
-- flip from increase to decrease
if(self.size>self.maxSize or self.size<self.minSize) then
self.step = self.step * - 1
end
-- switch to canvas and set color
setContext(self.canvas)
blendMode(ADDITIVE)
tint(self.penColor)
-- loop through each point of of dh vector adding particles so we get a relatively smooth line
nx = 0
ny = 0
if self.drawing then
for h = 0, dh, 2 do
ax = math.floor(h * math.cos(angle))
ay = math.floor(h * math.sin(angle))
nx = px+ax
ny = py+ay
-- draw onto canvas
--sprite("Dropbox:particle flare", nx, ny, psize, psize)
sprite(self.particle, nx, ny, psize, psize)
end
end
-- switch back to screen context and copy contents of canvas to it
noTint()
setContext()
blendMode(ADDITIVE)
background(0, 0, 0, 255)
spriteMode(CORNER)
sprite(self.canvas, 0, 0)
end
function DrawCanvas:touched(touch)
-- switch pen color at the start of each touch
if(touch.state==BEGAN) then
self.size = 32
self:startDrawing()
self.penColor = color(math.random(100,255), math.random(100,255), math.random(100,255), 25)
end
-- dont update if we are not drawing
if(touch.state==ENDED) then
self:stopDrawing()
self.size = self.minSize
end
end
function FPS()
return math.floor((1/DeltaTime) + 0.5)
end
function setup()
dc = DrawCanvas()
parameter.watch("FPS()")
end
function draw()
dc:draw()
end
function touched(touch)
dc:touched(touch)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment