Class: HotCocoa::Graphics::SandPainter

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/graphics/elements/sandpainter.rb

Overview

draw watercolor-like painted strokes (adapted from code by Jared Tarbell - complexification.net)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas, color = Color.red) ⇒ SandPainter

Returns a new instance of SandPainter.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 22

def initialize(canvas, color=Color.red)
  @canvas           = canvas
  @color            = color
  # set a random initial gain value
  @gain             = random(0.01, 0.1)
  @grainsize        = 6.0
  @grains           = 64
  @maxalpha         = 0.1
  @jitter           = 0
  @huedrift         = 0.2
  @saturationdrift  = 0.3
  @brightnessdrift  = 0.3
end

Instance Attribute Details

#brightnessdriftObject

Returns the value of attribute brightnessdrift.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def brightnessdrift
  @brightnessdrift
end

#colorObject

Returns the value of attribute color.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def color
  @color
end

#grainsObject

Returns the value of attribute grains.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def grains
  @grains
end

#grainsizeObject

Returns the value of attribute grainsize.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def grainsize
  @grainsize
end

#huedriftObject

Returns the value of attribute huedrift.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def huedrift
  @huedrift
end

#jitterObject

Returns the value of attribute jitter.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def jitter
  @jitter
end

#maxalphaObject

Returns the value of attribute maxalpha.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def maxalpha
  @maxalpha
end

#saturationdriftObject

Returns the value of attribute saturationdrift.



20
21
22
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 20

def saturationdrift
  @saturationdrift
end

Instance Method Details

#render(ox, oy, x, y) ⇒ Object

render a line that fades out from ox,oy to x,y



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hotcocoa/graphics/elements/sandpainter.rb', line 37

def render(ox, oy, x, y)
  @canvas.push
  # modulate gain
  @gain += random(-0.050, 0.050)
  @gain = inrange(@gain, 0.0, 1.0)
  # calculate grains by distance
  #@grains = (sqrt((ox-x)*(ox-x)+(oy-y)*(oy-y))).to_i

  # ramp from 0 to .015 for g = 0 to 1
  w = @gain / (@grains-1)

  #raycolor = @color.analog
  #@color.drift(0.2,0.1,0.1)
  @color.drift(huedrift, saturationdrift, brightnessdrift)
  #for i in 0..@grains do ##RACK change to fixnum.times
  (@grains + 1).times do |i|
    # set alpha for this grain (ramp from maxalpha to 0.0 for i = 0 to 64)
    a = @maxalpha - (i / @grains.to_f) * @maxalpha
    fillcolor = @color.copy.a(a)
    @canvas.fill(fillcolor)
    #C.rect(ox+(x-ox)*sin(sin(i*w)),oy+(y-oy)*sin(sin(i*w)),1,1)
    scaler = sin(sin(i * w)) # ramp sinusoidally from 0 to 1
    x1 = ox + (x - ox) * scaler + random(-@jitter, @jitter)
    y1 = oy + (y - oy) * scaler + random(-@jitter, @jitter)
    #puts "#{scaler} #{w} #{i} #{a} => #{x1},#{y1} #{scaler}"
    @canvas.oval(x1, y1, @grainsize, @grainsize, :center)
    #C.oval(x,y,@grainsize,@grainsize,:center)
    #C.oval(ox,oy,@grainsize,@grainsize,:center)
  end
  @canvas.pop
end