Word Spring

I created the word spring that each character can jump up and down as it is on spring. Thanks @Herwig for the outline routine from the physics based Wordcloud.

Here is the video

http://www.youtube.com/watch?v=7EuYILR3Dkg&feature=youtube_gdata_player


--# Main

-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    print("Word Spring")
    y = 650
    PhyChar(80,y,"C",0.9)
    PhyChar(200,y,"o",0.8)
    PhyChar(300,y,"d",0.7)
    PhyChar(400,y,"e",0.6)
    PhyChar(500,y,"a",0.5)
    floor = physics.body(POLYGON,vec2(0,0),vec2(WIDTH,0),vec2(WIDTH,10),vec2(0,10))
    floor.type = STATIC
    floor.x = 0
    floor.y = 300
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0)

    -- This sets the line thickness
    UI:draw()
end

--# PhyChar
PhyChar = class()

function PhyChar:init(x,y,ch,r)
    -- you can accept and set parameters here
    self.x = x
    self.y = y 
    self.char = ch
    self.showLine = false
    local xmin,ymin,xmax,ymax = PhyChar:boundingBox(ch)
    self.body = physics.body(POLYGON,vec2(xmin,ymin),vec2(xmax,ymin),
                vec2(xmax,ymax),vec2(xmin,ymax))
    self.body.x = self.x
    self.body.y = self.y
    self.body.restitution = r
    self.body.fixedRotation = true
    UI:add(self)
end

function PhyChar:boundingBox(chr)
    pushMatrix()
    pushStyle()
    self.font = "Arial-BoldMT"
    self.fontSize = 150
    font(self.font)
    fontSize(self.fontSize)
    local w,h=textSize(chr)
    local img=image(w,h)
    setContext(img)
    textMode(CORNER)
    fill(255, 255, 255, 255)
    text(chr,0,0)
    setContext()
    local xmin,ymin,xmax,ymax=w,h,0,0
    for x=1,w do
        for y=1,h do
            local r,g,b,a=img:get(x,y)
            if (a>0) then
                if (xmin>=x) then
                    xmin=x
                end
                if (xmax<=x) then
                    xmax=x
                end
                if (ymin>=y) then
                    ymin=y
                end
                if (ymax<=y) then
                    ymax=y
                end 
            end
        end
    end
    --workaround for text bug
    setContext(img)
    textMode(CORNER)
    text("adsrfetgy",0,0)
    setContext()
    
    popStyle()
    popMatrix()
    return xmin,ymin,xmax,ymax
end

function PhyChar:draw()
    -- Codea does not automatically call this method
    local body = self.body
    pushMatrix()
    pushStyle()
        translate(body.x,body.y)
        rotate(body.angle)
        font(self.font)
        fontSize(self.fontSize)
        fill(110, 195, 111, 255)
        textMode(CORNER)
        text(self.char,0,0)
        if self.showLine then
            stroke(255, 255, 0, 255)
            strokeWidth(3)
            local points = body.points
            for i=1,#points do
                a = points[i]
                b = points[(i % #points)+1]
                line(a.x,a.y,b.x,b.y)
            end
        end
    popStyle()
    popMatrix()
end

function PhyChar:touched(touch)
    -- Codea does not automatically call this method
end

--# UI
_elements = {}
UI = class()

function UI:init()
    -- you can accept and set parameters here
end

function UI:add(ele)
    -- Codea does not automatically call this method
    table.insert(_elements,ele)
end

function UI:draw()
    -- Codea does not automatically call this method
    for k,v in pairs(_elements) do
        v:draw()
    end
end

function UI:touched(touch)
    -- Codea does not automatically call this method
end

Very nice @sanit, cool effect. Would be cool to type long sentences and see them fall down like that.

Very nice. Like it.