Class: HotCocoa::Graphics::Rope

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas, options = {}) ⇒ Rope

Returns a new instance of Rope.



20
21
22
23
24
25
26
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 20

def initialize(canvas, options={})
  @canvas       = canvas
  @width        = options[:width] || 200
  @fibers       = options[:fibers] || 200
  @roundness    = options[:roundness] || 1.0
  @strokewidth  = options[:strokewidth] || 0.4
end

Instance Attribute Details

#fibersObject

Returns the value of attribute fibers.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def fibers
  @fibers
end

#roundnessObject

Returns the value of attribute roundness.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def roundness
  @roundness
end

#strokewidthObject

Returns the value of attribute strokewidth.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def strokewidth
  @strokewidth
end

#widthObject

Returns the value of attribute width.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def width
  @width
end

#x0Object

Returns the value of attribute x0.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def x0
  @x0
end

#x1Object

Returns the value of attribute x1.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def x1
  @x1
end

#y0Object

Returns the value of attribute y0.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def y0
  @y0
end

#y1Object

Returns the value of attribute y1.



18
19
20
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 18

def y1
  @y1
end

Instance Method Details

#hair(hair_x0 = @x0, hair_y0 = @y0, hair_x1 = @x1, hair_y1 = @y1, hair_width = @width, hair_fibers = @fibers) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 28

def hair(hair_x0=@x0, hair_y0=@y0, hair_x1=@x1, hair_y1=@y1, hair_width=@width, hair_fibers=@fibers)
  @canvas.push
  @canvas.strokewidth(@strokewidth)
  @canvas.autoclosepath(false)
  @canvas.nofill
  hair_x0 = choose(hair_x0)
  hair_y0 = choose(hair_y0)
  hair_x1 = choose(hair_x1)
  hair_y1 = choose(hair_y1)
  vx0 = random(-@canvas.width   / 2,  @canvas.width   / 2) * @roundness
  vy0 = random(-@canvas.height  / 2,  @canvas.height  / 2) * @roundness
  vx1 = random(-@canvas.width   / 2,  @canvas.width   / 2) * @roundness
  vy1 = random(-@canvas.height  / 2,  @canvas.height  / 2) * @roundness
  hair_fibers.times do |j|
    #x0,y0,x1,y1 = [@x0.choose,@y0.choose,@x1.choose,@y1.choose]
    @canvas.beginpath(hair_x0, hair_y0)
    @canvas.curveto(
        hair_x0 + vx0 + rand(hair_width), hair_y0 + vy0 + rand(hair_width), # control point 1
        hair_x1 + vx1, hair_y1 + vy1,                                       # control point 2
        hair_x1, hair_y1                                                    # end point
    )
    @canvas.endpath
  end
  @canvas.pop
end

#ribbon(ribbon_x0 = @x0, ribbon_y0 = @y0, ribbon_x1 = @x1, ribbon_y1 = @y1, ribbon_width = @width, ribbon_fibers = @fibers) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hotcocoa/graphics/elements/rope.rb', line 54

def ribbon(ribbon_x0=@x0, ribbon_y0=@y0, ribbon_x1=@x1, ribbon_y1=@y1, ribbon_width=@width, ribbon_fibers=@fibers)
  @canvas.push
  @canvas.strokewidth(@strokewidth)
  @canvas.autoclosepath(false)
  @canvas.nofill
  black = Color.black
  white = Color.white
  ribbon_x0 = choose(ribbon_x0)
  ribbon_y0 = choose(ribbon_y0)
  ribbon_x1 = choose(ribbon_x1)
  ribbon_y1 = choose(ribbon_y1)
  vx0 = random(-@canvas.width   / 2, @canvas.width  / 2) * @roundness
  vy0 = random(-@canvas.height  / 2, @canvas.height / 2) * @roundness
  vx1 = random(-@canvas.width   / 2, @canvas.width  / 2) * @roundness
  vy1 = random(-@canvas.height  / 2, @canvas.height / 2) * @roundness
  xwidth = rand(ribbon_width)
  ywidth = rand(ribbon_width)
  ribbon_fibers.times do |j|
    #x0,y0,x1,y1 = [@x0.choose,@y0.choose,@x1.choose,@y1.choose]
    xoffset = (j-1).to_f * xwidth / ribbon_fibers
    yoffset = (j-1).to_f * ywidth / ribbon_fibers
    cpx0 = ribbon_x0 + vx0 + xoffset
    cpy0 = ribbon_y0 + vy0 + yoffset
    cpx1 = ribbon_x1 + vx1
    cpy1 = ribbon_y1 + vy1
    # debug - show control points
    # @canvas.fill(black)
    # @canvas.oval(cpx0,cpy0,5,5,:center)
    # @canvas.fill(white)
    # @canvas.oval(cpx1,cpy1,5,5,:center)
    # @canvas.nofill

    @canvas.beginpath(x0, y0)
    @canvas.curveto(
      cpx0, cpy0,           # control point 1
      cpx1, cpy1,           # control point 2
      ribbon_x1, ribbon_y1  # end point
    )
    @canvas.endpath
  end
  @canvas.pop
end