Random functions/classes #1: Pulse class

This class will pulsate four lines away from your touch.


--# Main
-- Pulsation
displayMode(FULLSCREEN)
supportedOrientations(CurrentOrientation)

function setup()
    
end

function draw()
    background(40, 40, 50)
    if tched then p:draw() end
end

function touched(t)
    if t.state==BEGAN and not tched then
        p = Pulse(t.x,t.y)
        tched = true
    end
end
--# Pulse
Pulse = class()

function Pulse:init(x,y)
    self.alpha = 255
    rightx1,rightx2 = x+10,x+50
    leftx1,leftx2 = x-10,x-50
    bottomy1,bottomy2 = y-10,y-50
    topy1,topy2 = y+10,y+50
    self.x = x
    self.y = y
    self.red = math.random(255)
    self.green = math.random(255)
end

function Pulse:draw()
    rightx1,rightx2 = rightx1+10,rightx2+10
    leftx1,leftx2 = leftx1-10,leftx2-10
    bottomy1,bottomy2 = bottomy1-10,bottomy2-10
    topy1,topy2 = topy1+10,topy2+10
    self.alpha = self.alpha - 15
    strokeWidth(5)
    stroke(self.red,self.green,self.alpha)
    line(rightx1,self.y,rightx2,self.y)
    line(leftx1,self.y,leftx2,self.y)
    line(self.x,topy1,self.x,topy2)
    line(self.x,bottomy1,self.x,bottomy2)
    if self.alpha<5 then tched=false end
end

Nice work! If you change the if t.state==ENDED to if t.state==MOVING, then as you move your finger on the screen, the lines appear to angle in the direction of your drag, but they are actually straight.

@Saturn031000 I don’t see it…
EDIT: But thanks anyway :slight_smile:

@Doge It’s an optical illusion.

Replace main with this and you get a cool effect when you drag:

-- Pulsation
displayMode(FULLSCREEN)
supportedOrientations(CurrentOrientation)

function setup()
    pulses = {}
end

function draw()
    background(40, 40, 50)
    for i = #pulses, 1, -1 do
        pulses[i]:draw()
        if pulses[i].alpha < 5 then
            table.remove(pulses, i)
        end
    end
end

function touched(t)
    if t.state==MOVING then
        table.insert(pulses, Pulse(t.x,t.y))
    end
end

Thanks for sharing @JakAttak