sketch 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,6 +3,8 @@ source 'http://rubygems.org'
3
3
  # Specify your gem's dependencies in sketch.gemspec
4
4
  gemspec
5
5
 
6
+ gem 'nokogiri'
7
+
6
8
  group :test do
7
9
  gem 'test-unit'
8
10
  gem 'mocha'
data/examples/star.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Star < Sketch
2
+
3
+ def draw(canvas)
4
+ Sketch::Star.new(:radius => self.width.to_f / 2.1, :x => self.width.to_f / 2, :y => self.width.to_f / 2, :fill => 'yellow', :stroke => 'black', :stroke_width => 1, :orientation => 45).draw(canvas)
5
+ end
6
+ end
7
+
data/lib/sketch.rb CHANGED
@@ -48,4 +48,5 @@ require 'sketch/ellipse'
48
48
  require 'sketch/rect'
49
49
  require 'sketch/rectangle'
50
50
  require 'sketch/path'
51
+ require 'sketch/star'
51
52
 
@@ -0,0 +1,13 @@
1
+ class Sketch::Line < Sketch::Element
2
+
3
+ has_value :x1
4
+ has_value :x2
5
+ has_value :y1
6
+ has_value :y2
7
+ has_value :stroke, :default => 'black'
8
+ has_value :stroke_width, :default => 1
9
+ has_value :stroke_linecap
10
+ has_value :stroke_opacity
11
+
12
+ end
13
+
data/lib/sketch/path.rb CHANGED
@@ -1,17 +1,14 @@
1
1
  class Sketch::Path < Sketch::Element
2
-
3
- has_value :fill
4
- has_value :stroke
5
- has_value :stroke_width
2
+
3
+ has_value :fill, :default => 'none'
4
+ has_value :stroke, :default => 'black'
5
+ has_value :stroke_width, :default => 1
6
6
  has_value :stroke_linecap
7
- has_value :d, :alias => 'commands', :default => []
8
-
9
- def draw(canvas)
10
- canvas.path(svg_attributes)
11
- end
7
+ has_value :d, :alias => 'commands', :default => []
12
8
 
13
9
  def svg_attributes
14
- super.merge(:d => self.commands.join(' '))
10
+ out = super
11
+ out['d'] = out['d'].join(' ') if out.has_key('d')
15
12
  end
16
13
  end
17
14
 
data/lib/sketch/star.rb CHANGED
@@ -5,17 +5,50 @@ class Sketch::Star < Sketch::Element
5
5
  has_value :radius
6
6
  has_value :orientation, :default => 0
7
7
 
8
+ has_value :number_of_points, :default => 5
9
+
10
+ has_value :fill
11
+ has_value :stroke
12
+ has_value :stroke_width
13
+
8
14
  central_point :x, :y
9
15
 
16
+ def degrees_to_radians( degrees )
17
+ degrees * Math::PI / 180
18
+ end
19
+
20
+ def angle_between_points
21
+ 360 / number_of_points
22
+ end
23
+
24
+ def drawing_order_indexes
25
+ self.number_of_points.times.map do |n|
26
+ (n*2) % self.number_of_points
27
+ end
28
+ end
29
+
30
+ def points_in_drawing_order
31
+ drawing_order_indexes.map do |index|
32
+ points[index]
33
+ end
34
+ end
35
+
36
+ def points
37
+ @points ||= list = number_of_points.times.map do |point_number|
38
+ current_angle = orientation + ( angle_between_points * point_number )
39
+ [ x + (radius * Math.sin( degrees_to_radians( current_angle ))), y - (radius * Math.cos( degrees_to_radians( current_angle ))) ]
40
+ end
41
+ end
42
+
10
43
  def draw(canvas)
11
- top = [x, y - radius]
12
- mid_right = [x + radius, y-(.3*radius)]
13
- lower_right = [x + (0.6 * radius), y + radius]
14
- lower_left = [x - (0.6 * radius), y + radius]
15
- mid_left = [x - radius, y-(0.3*radius)]
16
- points = [ top, lower_right, mid_left, mid_right, lower_left, top ]
17
- points.each{|point| point.join(',')}
18
- points = points.join(' ')
19
- canvas.polygon( :points => points)
44
+ if x && y && radius
45
+ atts = {}
46
+ atts['points'] = points_in_drawing_order.each{|point| point.join(',')}.join(' ')
47
+ atts['fill'] = attributes[:fill] if attributes.has_key?(:fill)
48
+ atts['stroke'] = attributes[:stroke] if attributes.has_key?(:stroke)
49
+ atts['stroke-width'] = attributes[:stroke_width] if attributes.has_key?(:stroke_width)
50
+
51
+ canvas.polygon( atts )
52
+ end
20
53
  end
21
54
  end
@@ -0,0 +1,27 @@
1
+ class Sketch::Triangle < Sketch::Element
2
+
3
+ has_value :point
4
+ has_value :height
5
+ has_value :direction
6
+ has_value :stroke
7
+ has_value :stroke_width
8
+ has_value :fill
9
+
10
+ def draw(canvas)
11
+ case direction
12
+ when :up
13
+ point1 = "M#{point.join(',')}"
14
+ point2 = "l-#{0.5*height},#{height*0.5}"
15
+ point3 = "l#{height},0"
16
+ point4 = "l-#{height*0.5},-#{height*0.5}"
17
+ when :down
18
+ point1 = "M#{point.join(',')}"
19
+ point2 = "l-#{0.5*height},-#{height*0.5}"
20
+ point3 = "l#{height},0"
21
+ point4 = "l-#{height*0.5},#{height*0.5}"
22
+ end
23
+
24
+ Sketch::Path.new(:stroke_width => self.stroke_width, :fill => self.fill, :stroke => self.stroke, :commands => [point1, point2, point3, point4]).draw(canvas)
25
+ end
26
+ end
27
+
data/sketch.version CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/test/star_test.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class StarTest < Test::Unit::TestCase
4
+
5
+ def test_that_star_is_drawn_as_a_polygon
6
+ canvas = stub
7
+ canvas.expects(:polygon).with(:points => '100,10 40,180 190,60 10,60 160,180 100,10')
8
+ art = Sketch::Star.new.draw(canvas)
9
+ end
10
+
11
+ end
12
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sketch
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Johnathon Wright
@@ -15,12 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-10 00:00:00 -06:00
18
+ date: 2011-11-11 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: valuable
23
- prerelease: false
22
+ type: :runtime
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
25
  requirements:
@@ -31,11 +30,11 @@ dependencies:
31
30
  - 0
32
31
  - 8
33
32
  version: "0.8"
34
- type: :runtime
33
+ name: valuable
35
34
  version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: nokogiri
38
35
  prerelease: false
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
39
38
  requirement: &id002 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
@@ -46,8 +45,9 @@ dependencies:
46
45
  - 1
47
46
  - 4
48
47
  version: "1.4"
49
- type: :runtime
48
+ name: nokogiri
50
49
  version_requirements: *id002
50
+ prerelease: false
51
51
  description: build SVG images
52
52
  email: jw@mustmodify.com
53
53
  executables: []
@@ -63,17 +63,20 @@ files:
63
63
  - Rakefile
64
64
  - TODO.txt
65
65
  - examples/smile.rb
66
+ - examples/star.rb
66
67
  - lib/sketch.rb
67
68
  - lib/sketch/base.rb
68
69
  - lib/sketch/canvas.rb
69
70
  - lib/sketch/circle.rb
70
71
  - lib/sketch/element.rb
71
72
  - lib/sketch/ellipse.rb
73
+ - lib/sketch/line.rb
72
74
  - lib/sketch/path.rb
73
75
  - lib/sketch/polygon.rb
74
76
  - lib/sketch/rect.rb
75
77
  - lib/sketch/rectangle.rb
76
78
  - lib/sketch/star.rb
79
+ - lib/sketch/triangle.rb
77
80
  - sketch.gemspec
78
81
  - sketch.version
79
82
  - test/.canvas_test.rb.swp
@@ -82,6 +85,7 @@ files:
82
85
  - test/doctype_test.rb
83
86
  - test/element_test.rb
84
87
  - test/helper.rb
88
+ - test/star_test.rb
85
89
  - test/svg_namespace_test.rb
86
90
  has_rdoc: true
87
91
  homepage: http://sketch.mustmodify.com/