rubysketch 0.3.7 → 0.3.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41b3a75aeb6c8bdc443d2d87e44c60361a527e4d6b8b3918e8b1eaa2503f92e4
4
- data.tar.gz: 7f373d0f3fdb31d38bb76d8e8ed88ce2f3cedca0c696d8a5f571ded1e78fb15d
3
+ metadata.gz: fb34f91483c8d24fb65c537455d808c3f0f9ac337eed9199440623ba564ef710
4
+ data.tar.gz: 2c7dd117d90a04210d256dd36185f599ca7527abbaad4dd48757cb83b49edfe8
5
5
  SHA512:
6
- metadata.gz: ffcba1a2e5ae98525c4c1512230d249995071e6be9183918b6f62ed5293d9ac8f530d38aa5f544d2d4fc431ca17f38c7746955df313e9609e0ee528551045a51
7
- data.tar.gz: cb49daa453cdf6dd9027a59c508363a63fed2e210b0acbcfa311e66334b107a88471e5b8b59dac7f77b5aa04423aaa2d0cd2b2fa0c4b1221b76997a93ce89e79
6
+ metadata.gz: 50ef4e1473605388da623a0e252061c4cd611969d9665bd5cf2212ed9a7930dd313915f2fb27879939a9ef0bea4e1d84bcc70349c6757a931dbfcd7fb30fa240
7
+ data.tar.gz: 818c2ffc497f2f4f044832e702799ba005f826f03dcf626e8862dd1127b7061f6d1e4653dfe0ffcf07e3fc7a7b7579655387452a77f421a289de2660b2344a48
@@ -1,6 +1,34 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.12] - 2020-12-10
5
+
6
+ - size() and createCanvas() take 'pixelDensity' parameter and default value is 1
7
+
8
+
9
+ ## [0.3.11] - 2020-12-9
10
+
11
+ - add size(), createCanvas() and pixelDensity()
12
+
13
+
14
+ ## [0.3.10] - 2020-12-1
15
+
16
+ - invert angle parameter value for arc() to fix compatibility to processing API
17
+
18
+
19
+ ## [0.3.9] - 2020-11-30
20
+
21
+ - Graphics#beginDraw() can take block to call endDraw automatically
22
+ - Capture#start() always returns nil
23
+ - add delay_camera.rb
24
+
25
+
26
+ ## [0.3.8] - 2020-11-27
27
+
28
+ - Capture#initialize() can take requestWidth, requestHeight and cameraName
29
+ - add Capture#width and Capture#height
30
+
31
+
4
32
  ## [0.3.7] - 2020-11-18
5
33
 
6
34
  - add Capture class
data/README.md CHANGED
@@ -1,3 +1,11 @@
1
- # RubySketch - Processing like Creative Coding Framework
1
+ # RubySketch - Processing compatible Creative Coding Framework
2
2
 
3
3
  by xordog@gmail.com
4
+
5
+
6
+ # How to release
7
+
8
+ ```
9
+ $ rake version:bump:patch message="..."
10
+ $ git push origin --tags
11
+ ```
data/Rakefile CHANGED
@@ -23,3 +23,28 @@ generate_documents
23
23
  build_ruby_gem
24
24
 
25
25
  task :default => :test
26
+
27
+
28
+ namespace :version do
29
+
30
+ namespace :bump do
31
+
32
+ task :major do
33
+ update_and_tag_version 0
34
+ end
35
+
36
+ task :minor do
37
+ update_and_tag_version 1
38
+ end
39
+
40
+ task :patch do
41
+ update_and_tag_version 2
42
+ end
43
+
44
+ task :build do
45
+ update_and_tag_version 3
46
+ end
47
+
48
+ end# bump
49
+
50
+ end# version
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.7
1
+ 0.3.12
@@ -5,7 +5,7 @@
5
5
  require 'rubysketch-processing'
6
6
 
7
7
 
8
- cam = Capture.new
8
+ cam = Capture.new 300, 300
9
9
  cam.start
10
10
 
11
11
  draw do
@@ -0,0 +1,33 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ w, h = width, height
9
+
10
+ cam = Capture.new w, h, Capture.list.last
11
+ cam.start
12
+
13
+ images = 60.times.map {
14
+ Graphics.new w, h
15
+ }
16
+
17
+ draw do
18
+ if frameCount % 2 == 0
19
+ images.unshift images.pop
20
+ images.first.tap do |image|
21
+ image.beginDraw {
22
+ image.image cam, 0, 0
23
+ }
24
+ end
25
+ end
26
+
27
+ background 0
28
+ segment_h= h / images.size
29
+ images.each.with_index do |image, i|
30
+ y = i * segment_h
31
+ copy image, 0, y, w, segment_h, 0, y, w, segment_h
32
+ end
33
+ end
@@ -737,22 +737,41 @@ module RubySketch
737
737
  # @return [Array] device name list
738
738
  #
739
739
  def self.list ()
740
- ['default']
740
+ Rays::Camera.device_names
741
741
  end
742
742
 
743
743
  # Initialize camera object.
744
744
  #
745
- def initialize ()
746
- @camera = Rays::Camera.new
745
+ # @overload Capture.new()
746
+ # @overload Capture.new(cameraName)
747
+ # @overload Capture.new(requestWidth, requestHeight)
748
+ # @overload Capture.new(requestWidth, requestHeight, cameraName)
749
+ #
750
+ # @param requestWidth [Integer] captured image width
751
+ # @param requestHeight [Integer] captured image height
752
+ # @param cameraName [String] camera device name
753
+ #
754
+ def initialize (*args)
755
+ width, height, name =
756
+ if args.empty?
757
+ [-1, -1, nil]
758
+ elsif args[0].kind_of?(String)
759
+ [-1, -1, args[0]]
760
+ elsif args[0].kind_of?(Numeric) && args[1].kind_of?(Numeric)
761
+ [args[0], args[1], args[2]]
762
+ else
763
+ raise ArgumentError
764
+ end
765
+ @camera = Rays::Camera.new width, height, device_name: name
747
766
  end
748
767
 
749
768
  # Start capturing.
750
769
  #
751
- # @return [Capture] self
770
+ # @return [nil] nil
752
771
  #
753
772
  def start ()
754
773
  raise "Failed to start capture" unless @camera.start
755
- self
774
+ nil
756
775
  end
757
776
 
758
777
  # Stop capturing.
@@ -778,6 +797,22 @@ module RubySketch
778
797
  @camera.image
779
798
  end
780
799
 
800
+ # Returns the width of captured image
801
+ #
802
+ # @return [Numeric] the width of captured image
803
+ #
804
+ def width ()
805
+ @camera.image&.width || 0
806
+ end
807
+
808
+ # Returns the height of captured image
809
+ #
810
+ # @return [Numeric] the height of captured image
811
+ #
812
+ def height ()
813
+ @camera.image&.height || 0
814
+ end
815
+
781
816
  # @private
782
817
  def getInternal__ ()
783
818
  @camera.image || dummyImage__
@@ -878,10 +913,7 @@ module RubySketch
878
913
  #
879
914
  SQUARE = :square
880
915
 
881
- def setup__ (painter)
882
- @painter__ = painter
883
- @painter__.miter_limit = 10
884
-
916
+ def init__ (image, painter)
885
917
  @drawing__ = false
886
918
  @hsbColor__ = false
887
919
  @colorMaxes__ = [1.0] * 4
@@ -894,6 +926,8 @@ module RubySketch
894
926
  @matrixStack__ = []
895
927
  @styleStack__ = []
896
928
 
929
+ updateCanvas__ image, painter
930
+
897
931
  colorMode RGB, 255
898
932
  angleMode RADIANS
899
933
  rectMode CORNER
@@ -905,13 +939,19 @@ module RubySketch
905
939
  stroke 0
906
940
  end
907
941
 
908
- def beginDraw ()
942
+ def updateCanvas__ (image, painter)
943
+ @image__, @painter__ = image, painter
944
+ end
945
+
946
+ # @private
947
+ def beginDraw__ ()
909
948
  @matrixStack__.clear
910
949
  @styleStack__.clear
911
950
  @drawing__ = true
912
951
  end
913
952
 
914
- def endDraw ()
953
+ # @private
954
+ def endDraw__ ()
915
955
  @drawing__ = false
916
956
  end
917
957
 
@@ -1332,8 +1372,8 @@ module RubySketch
1332
1372
  def arc (a, b, c, d, start, stop)
1333
1373
  assertDrawing__
1334
1374
  x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
1335
- start = toAngle__ start
1336
- stop = toAngle__ stop
1375
+ start = toAngle__ -start
1376
+ stop = toAngle__ -stop
1337
1377
  @painter__.ellipse x, y, w, h, from: start, to: stop
1338
1378
  nil
1339
1379
  end
@@ -1675,23 +1715,27 @@ module RubySketch
1675
1715
  # Initialize graphics object.
1676
1716
  #
1677
1717
  def initialize (width, height)
1678
- @image__ = Rays::Image.new width, height
1679
- setup__ @image__.painter
1718
+ image = Rays::Image.new width, height
1719
+ init__ image, image.painter
1680
1720
  end
1681
1721
 
1682
1722
  # Start drawing.
1683
1723
  #
1684
- def beginDraw ()
1724
+ def beginDraw (&block)
1685
1725
  @painter__.__send__ :begin_paint
1686
- super
1726
+ beginDraw__
1687
1727
  push
1728
+ if block
1729
+ block.call
1730
+ endDraw
1731
+ end
1688
1732
  end
1689
1733
 
1690
1734
  # End drawing.
1691
1735
  #
1692
1736
  def endDraw ()
1693
1737
  pop
1694
- super
1738
+ endDraw__
1695
1739
  @painter__.__send__ :end_paint
1696
1740
  end
1697
1741
 
@@ -1708,8 +1752,10 @@ module RubySketch
1708
1752
  Capture = Processing::Capture
1709
1753
  Graphics = Processing::Graphics
1710
1754
 
1755
+ # @private
1711
1756
  @@context__ = nil
1712
1757
 
1758
+ # @private
1713
1759
  def self.context__ ()
1714
1760
  @@context__
1715
1761
  end
@@ -1719,23 +1765,21 @@ module RubySketch
1719
1765
  @@context__ = self
1720
1766
 
1721
1767
  @window__ = window
1722
- @image__ = @window__.canvas
1723
- setup__ @window__.canvas_painter.paint {background 0.8}
1768
+ init__ @window__.canvas, @window__.canvas_painter.paint {background 0.8}
1724
1769
 
1725
1770
  @loop__ = true
1726
1771
  @redraw__ = false
1727
1772
  @frameCount__ = 0
1728
1773
  @mousePos__ =
1729
- @mousePrevPos__ = Rays::Point.new 0
1774
+ @mousePrevPos__ = [0, 0]
1730
1775
  @mousePressed__ = false
1731
1776
  @touches__ = []
1732
1777
 
1733
- @window__.before_draw = proc {beginDraw}
1734
- @window__.after_draw = proc {endDraw}
1778
+ @window__.before_draw = proc {beginDraw__}
1779
+ @window__.after_draw = proc {endDraw__}
1735
1780
 
1736
1781
  drawFrame = -> {
1737
- @image__ = @window__.canvas
1738
- @painter__ = @window__.canvas_painter
1782
+ updateCanvas__ @window__.canvas, @window__.canvas_painter
1739
1783
  begin
1740
1784
  push
1741
1785
  @drawBlock__.call if @drawBlock__
@@ -1754,9 +1798,11 @@ module RubySketch
1754
1798
  end
1755
1799
 
1756
1800
  updatePointerStates = -> event, pressed = nil {
1757
- @mousePos__ = event.pos
1801
+ @mousePos__ = @window__.to_canvas_coord event.pos.x, event.pos.y
1758
1802
  @mousePressed__ = pressed if pressed != nil
1759
- @touches__ = event.positions.map {|pos| Touch.new pos.x, pos.y}
1803
+ @touches__ = event.positions.map {|pos|
1804
+ Touch.new *@window__.to_canvas_coord(pos.x, pos.y)
1805
+ }
1760
1806
  }
1761
1807
 
1762
1808
  @window__.pointer_down = proc do |e|
@@ -1834,15 +1880,59 @@ module RubySketch
1834
1880
  nil
1835
1881
  end
1836
1882
 
1883
+ # Changes canvas size.
1884
+ #
1885
+ # @param width [Integer] new width
1886
+ # @param height [Integer] new height
1887
+ #
1888
+ # @return [nil] nil
1889
+ #
1890
+ def size (width, height, pixelDensity: 1)
1891
+ resizeCanvas__ :size, width, height, pixelDensity
1892
+ nil
1893
+ end
1894
+
1895
+ # Changes canvas size.
1896
+ #
1897
+ # @param width [Integer] new width
1898
+ # @param height [Integer] new height
1899
+ #
1900
+ # @return [nil] nil
1901
+ #
1902
+ def createCanvas (width, height, pixelDensity: 1)
1903
+ resizeCanvas__ :createCanvas, width, height, pixelDensity
1904
+ nil
1905
+ end
1906
+
1907
+ # Changes and returns canvas pixel density.
1908
+ #
1909
+ # @param density [Numeric] new pixel density
1910
+ #
1911
+ # @return [Numeric] current pixel density
1912
+ #
1913
+ def pixelDensity (density = nil)
1914
+ resizeCanvas__ :pixelDensity, width, height, density if density
1915
+ @painter__.pixel_density
1916
+ end
1917
+
1837
1918
  # @private
1838
- private def size__ (width, height)
1839
- raise 'size() must be called on startup or setup block' if @started__
1919
+ def resizeCanvas__ (name, width, height, pixelDensity)
1920
+ raise '#{name}() must be called on startup or setup block' if @started__
1840
1921
 
1841
1922
  @painter__.__send__ :end_paint
1842
- @window__.__send__ :reset_canvas, width, height
1923
+ @window__.__send__ :resize_canvas, width, height, pixelDensity
1924
+ updateCanvas__ @window__.canvas, @window__.canvas_painter
1843
1925
  @painter__.__send__ :begin_paint
1844
1926
 
1845
- @auto_resize__ = false
1927
+ @window__.auto_resize = false
1928
+ end
1929
+
1930
+ # Returns pixel density of display.
1931
+ #
1932
+ # @return [Numeric] pixel density
1933
+ #
1934
+ def displayDensity ()
1935
+ @window__.painter.pixel_density
1846
1936
  end
1847
1937
 
1848
1938
  def windowWidth ()
@@ -1869,20 +1959,12 @@ module RubySketch
1869
1959
  @window__.event.fps
1870
1960
  end
1871
1961
 
1872
- # Returns pixel density
1873
- #
1874
- # @return [Numeric] pixel density
1875
- #
1876
- def displayDensity ()
1877
- @painter__.pixel_density
1878
- end
1879
-
1880
1962
  # Returns mouse x position
1881
1963
  #
1882
1964
  # @return [Numeric] horizontal position of mouse
1883
1965
  #
1884
1966
  def mouseX ()
1885
- @mousePos__.x
1967
+ @mousePos__[0]
1886
1968
  end
1887
1969
 
1888
1970
  # Returns mouse y position
@@ -1890,7 +1972,7 @@ module RubySketch
1890
1972
  # @return [Numeric] vertical position of mouse
1891
1973
  #
1892
1974
  def mouseY ()
1893
- @mousePos__.y
1975
+ @mousePos__[1]
1894
1976
  end
1895
1977
 
1896
1978
  # Returns mouse x position in previous frame
@@ -1898,7 +1980,7 @@ module RubySketch
1898
1980
  # @return [Numeric] horizontal position of mouse
1899
1981
  #
1900
1982
  def pmouseX ()
1901
- @mousePrevPos__.x
1983
+ @mousePrevPos__[0]
1902
1984
  end
1903
1985
 
1904
1986
  # Returns mouse y position in previous frame
@@ -1906,7 +1988,7 @@ module RubySketch
1906
1988
  # @return [Numeric] vertical position of mouse
1907
1989
  #
1908
1990
  def pmouseY ()
1909
- @mousePrevPos__.y
1991
+ @mousePrevPos__[1]
1910
1992
  end
1911
1993
 
1912
1994
  # Returns array of touches
@@ -16,7 +16,8 @@ module RubySketch
16
16
  @auto_resize = true
17
17
  @error = nil
18
18
 
19
- reset_canvas 1, 1
19
+ painter.miter_limit = 10
20
+ resize_canvas 1, 1
20
21
 
21
22
  super *args, size: [width, height], &block
22
23
  end
@@ -43,7 +44,9 @@ module RubySketch
43
44
 
44
45
  def on_draw (e)
45
46
  draw_canvas {call_block @draw, e} if @draw
46
- e.painter.image @canvas
47
+
48
+ x, y, w, h = coord_converter
49
+ e.painter.image @canvas, x, y, @canvas.width * w, @canvas.height * h
47
50
  end
48
51
 
49
52
  def on_key (e)
@@ -64,27 +67,45 @@ module RubySketch
64
67
  end
65
68
 
66
69
  def on_resize (e)
67
- reset_canvas e.width, e.height if @auto_resize
70
+ resize_canvas e.width, e.height if @auto_resize
68
71
  draw_canvas {call_block @resize, e} if @resize
69
72
  end
70
73
 
74
+ def to_canvas_coord (x, y)
75
+ xx, yy, ww, hh = coord_converter
76
+ return (x - xx) / ww, (y - yy) / hh
77
+ end
78
+
71
79
  private
72
80
 
73
- def reset_canvas (width, height)
74
- return if width * height == 0
75
- return if width == @canvas&.width && height == @canvas&.height
81
+ def resize_canvas (width, height, pixel_density = nil)
82
+ return nil if width * height == 0
76
83
 
77
- old_canvas = @canvas
78
- old_painter = @canvas_painter
84
+ unless width == @canvas&.width &&
85
+ height == @canvas&.height &&
86
+ pixel_density == @canvas_painter&.pixel_density
79
87
 
80
- cs = old_canvas&.color_space || Rays::RGBA
81
- @canvas = Rays::Image.new width, height, cs, painter.pixel_density
82
- @canvas_painter = @canvas.painter
88
+ old_canvas = @canvas
89
+ old_painter = @canvas_painter
83
90
 
84
- if old_canvas
85
- @canvas_painter.paint {image old_canvas}
86
- copy_painter_attributes old_painter, @canvas_painter
91
+ cs = old_canvas&.color_space || Rays::RGBA
92
+ pd = pixel_density || painter.pixel_density
93
+ @canvas = Rays::Image.new width, height, cs, pd
94
+ @canvas_painter = @canvas.painter
95
+
96
+ if old_canvas
97
+ @canvas_painter.paint {image old_canvas}
98
+ copy_painter_attributes old_painter, @canvas_painter
99
+ end
100
+
101
+ resize_window width, height
87
102
  end
103
+
104
+ @canvas_painter
105
+ end
106
+
107
+ def resize_window (width, height)
108
+ size width, height
88
109
  end
89
110
 
90
111
  def copy_painter_attributes (from, to)
@@ -97,6 +118,21 @@ module RubySketch
97
118
  to.font = from.font
98
119
  end
99
120
 
121
+ def coord_converter ()
122
+ ww, wh = width.to_f, height.to_f
123
+ cw, ch = @canvas.width.to_f, @canvas.height.to_f
124
+ return [0, 0, 1, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
125
+
126
+ wratio, cratio = ww / wh, cw / ch
127
+ if wratio >= cratio
128
+ scaled_w = wh * cratio
129
+ return (ww - scaled_w) / 2, 0, scaled_w / cw, wh / ch
130
+ else
131
+ scaled_h = ww / cratio
132
+ return 0, (wh - scaled_h) / 2, ww / cw, scaled_h / ch
133
+ end
134
+ end
135
+
100
136
  def draw_canvas (&block)
101
137
  begin_draw
102
138
  block.call
@@ -12,11 +12,6 @@ require 'rubysketch'
12
12
  include Xot::Test
13
13
 
14
14
 
15
- unless $RAYS_NOAUTOINIT
16
- #def Rays.fin! () end
17
- end
18
-
19
-
20
15
  def assert_equal_vector (v1, v2, delta = 0.000001)
21
16
  assert_in_delta v1.x, v2.x, delta
22
17
  assert_in_delta v1.y, v2.y, delta
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-18 00:00:00.000000000 Z
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reflexion
@@ -43,6 +43,7 @@ files:
43
43
  - examples/breakout.rb
44
44
  - examples/camera.rb
45
45
  - examples/clock.rb
46
+ - examples/delay_camera.rb
46
47
  - examples/glsl.rb
47
48
  - examples/hello.rb
48
49
  - examples/image.rb