rubysketch 0.2.2 → 0.2.7

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: cb7fa5f9cc69b67a9339f9f7170c8901999d0007e6b6e74de7d7b55b36bc09d3
4
- data.tar.gz: c5158bda3a8ac90ec2255c3bfda5f8c3dbb8e9815bd74b9d5df0f8bfa601024e
3
+ metadata.gz: 35b603297108689f78cc6cd1b7c1577c314e3658763889cd751c57351d0ded11
4
+ data.tar.gz: adf06019c19a710d427c39ccf6febfa7aa01b8c01e748cd6061fff27bf286445
5
5
  SHA512:
6
- metadata.gz: b691e41b1e7ba08ad1a732f81333febe97cc68e30244ad7f8aea76b7660b7af5b47ae60542610f797340cc25bf9bc6f673dc893a19dd27e0b83583a19a8c99d8
7
- data.tar.gz: 969de4e7e4f840c590c0758af55a53bb28838b05cf63a84d9c389bd99b5493df23cb06a5020f25c8531a906d67be4da014b5e888929e4734c83f18d6f47a87e0
6
+ metadata.gz: a427610a5a9c648ce0b301d903f2c7b70a9fc18ae9b9286fe896151485d6df7ef3a2b15cb823d978b790f267367c42686e3fc651d86a21168f5a24d4cd76c7ab
7
+ data.tar.gz: 17a16b9259156c098ba2a34db1aac50cd7483481734b0c1bcb839870313fdb08c216ae0421c21324f59366e1c5b72bfccee35b81bd426edaba05db0d03acaa6e
@@ -0,0 +1,21 @@
1
+ # RubySketch ChangeLog
2
+
3
+
4
+ ## [0.2.7] - 2020-04-17
5
+
6
+ - add strokeCap() and strokeJoin()
7
+
8
+
9
+ ## [0.2.6] - 2020-04-17
10
+
11
+ - push(), pushMatrix() and pushStyle() take block to automatic pop
12
+ - refine startup process
13
+ - add curve() and bezier()
14
+ - add imageMode(), Image#resize(), Image#copy() and copy()
15
+ - add loop(), noLoop() and redraw()
16
+
17
+
18
+ ## [0.2.5] - 2020-03-29
19
+
20
+ - delete debug prints
21
+ - show unsupported image type
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.7
@@ -20,12 +20,10 @@ end
20
20
  draw do
21
21
  background 0
22
22
 
23
- begin
24
- pushMatrix
23
+ pushMatrix do
25
24
  translate width / 2, height / 2
26
25
 
27
- begin
28
- pushMatrix
26
+ pushMatrix do
29
27
  fill COLORS[0]
30
28
  ellipse 0, 0, 20, 20
31
29
  rotate (now - start) / 60.0 * 360
@@ -33,32 +31,25 @@ draw do
33
31
  strokeWeight 5
34
32
  line 0, 0, 200, 0
35
33
  fill 1
36
- popMatrix
37
34
  end
38
35
 
39
- begin
40
- pushMatrix
36
+ pushMatrix do
41
37
  strokeWeight 3
42
38
  60.times do
43
39
  rotate 6
44
40
  stroke COLORS[1]
45
41
  line 200, 0, 210, 0
46
42
  end
47
- popMatrix
48
43
  end
49
44
 
50
- begin
51
- pushMatrix
45
+ pushMatrix do
52
46
  strokeWeight 5
53
47
  12.times do
54
48
  rotate 30
55
49
  stroke COLORS[3]
56
50
  line 190, 0, 210, 0
57
51
  end
58
- popMatrix
59
52
  end
60
-
61
- popMatrix
62
53
  end
63
54
 
64
55
  textSize 20
@@ -1,4 +1,33 @@
1
1
  require 'rubysketch'
2
2
 
3
3
 
4
- RubySketch.__send__ :start_on_object_at_exit, self, RubySketch::Processing.new
4
+ begin
5
+ context = RubySketch::Processing.new
6
+ methods = context.class.instance_methods(false)
7
+ .reject {|name| name =~ /__$/}
8
+ consts = context.class.constants
9
+ .reject {|name| name =~ /__$/}
10
+ .each_with_object({}) {|name, h| h[name] = context.class.const_get name}
11
+
12
+ self.class.class_eval do
13
+ methods.each do |name|
14
+ define_method name do |*args, &block|
15
+ context.__send__ name, *args, &block
16
+ end
17
+ end
18
+ consts.each do |(name, value)|
19
+ const_set name, value
20
+ end
21
+ end
22
+
23
+ window = RubySketch::Window.new do |_|
24
+ window.start
25
+ end
26
+ context.setup__ window
27
+
28
+ window.canvas_painter.__send__ :begin_paint
29
+ at_exit do
30
+ window.canvas_painter.__send__ :end_paint
31
+ Reflex.start {window.show}
32
+ end
33
+ end
@@ -5,7 +5,6 @@ require 'open-uri'
5
5
 
6
6
  require 'reflex'
7
7
  require 'rubysketch/module'
8
- require 'rubysketch/starter'
9
8
  require 'rubysketch/window'
10
9
  require 'rubysketch/processing'
11
10
  require 'rubysketch/glsl'
@@ -4,8 +4,6 @@ module RubySketch
4
4
  # @private
5
5
  class GLSL
6
6
 
7
- extend Starter
8
-
9
7
  def initialize (glsl)
10
8
  @shader = Reflex::Shader.new glsl
11
9
  end
@@ -7,34 +7,59 @@ module RubySketch
7
7
 
8
8
  include Math
9
9
 
10
- extend Starter
11
-
12
10
  HALF_PI = PI / 2
13
11
  QUARTER_PI = PI / 4
14
12
  TWO_PI = PI * 2
15
13
  TAU = PI * 2
16
14
 
17
- # RGB mode for colorMode() function.
15
+ # RGB mode for colorMode().
18
16
  #
19
17
  RGB = :RGB
20
18
 
21
- # HSB mode for colorMode() function.
19
+ # HSB mode for colorMode().
22
20
  #
23
21
  HSB = :HSB
24
22
 
25
- # Radian mode for angleMode() function.
23
+ # Radian mode for angleMode().
26
24
  #
27
25
  RADIANS = :RADIANS
28
26
 
29
- # Degree mode for angleMode() function.
27
+ # Degree mode for angleMode().
30
28
  #
31
29
  DEGREES = :DEGREES
32
30
 
31
+ # Mode for rectMode(), ellipseMode() and imageMode().
32
+ #
33
33
  CORNER = :CORNER
34
+
35
+ # Mode for rectMode(), ellipseMode() and imageMode().
36
+ #
34
37
  CORNERS = :CORNERS
38
+
39
+ # Mode for rectMode(), ellipseMode() and imageMode().
40
+ #
35
41
  CENTER = :CENTER
42
+
43
+ # Mode for rectMode() and ellipseMode().
44
+ #
36
45
  RADIUS = :RADIUS
37
46
 
47
+ # Mode for strokeCap().
48
+ #
49
+ BUTT = :butt
50
+
51
+ # Mode for strokeJoin().
52
+ #
53
+ MITER = :miter
54
+
55
+ # Mode for strokeCap() and strokeJoin().
56
+ #
57
+ ROUND = :round
58
+
59
+ # Mode for strokeCap() and strokeJoin().
60
+ #
61
+ SQUARE = :square
62
+
38
63
  # @private
39
64
  DEG2RAD__ = PI / 180.0
40
65
 
@@ -43,6 +68,8 @@ module RubySketch
43
68
 
44
69
  # @private
45
70
  def initialize ()
71
+ @loop__ = true
72
+ @redraw__ = false
46
73
  @matrixStack__ = []
47
74
  @styleStack__ = []
48
75
  @frameCount__ = 0
@@ -59,18 +86,67 @@ module RubySketch
59
86
  angleMode RADIANS
60
87
  rectMode CORNER
61
88
  ellipseMode CENTER
89
+ imageMode CORNER
62
90
  end
63
91
 
64
92
  # @private
65
- def on_start__ (window)
93
+ def setup__ (window)
66
94
  @window__ = window
67
- @painter__ = window.canvas_painter
95
+ @painter__ = window.canvas_painter.tap do |o|
96
+ o.miter_limit = 10
97
+ end
98
+
99
+ drawFrame = -> event {
100
+ @painter__ = window.canvas_painter
101
+ @matrixStack__.clear
102
+ @styleStack__.clear
103
+ begin
104
+ push
105
+ @drawBlock__.call event if @drawBlock__
106
+ ensure
107
+ pop
108
+ end
109
+ @frameCount__ += 1
110
+ }
111
+
112
+ updateMouseState = -> x, y, pressed = nil {
113
+ @mouseX__ = x
114
+ @mouseY__ = y
115
+ @mousePressed__ = pressed if pressed != nil
116
+ }
117
+
118
+ updateMousePrevPos = -> {
119
+ @mousePrevX__ = @mouseX__
120
+ @mousePrevY__ = @mouseY__
121
+ }
122
+
123
+ @window__.draw = proc do |e|
124
+ if @loop__ || @redraw__
125
+ @redraw__ = false
126
+ drawFrame.call e
127
+ end
128
+ updateMousePrevPos.call
129
+ end
130
+
131
+ @window__.pointer_down = proc do |e|
132
+ updateMouseState.call e.x, e.y, true
133
+ @mousePressedBlock__.call e if @mousePressedBlock__
134
+ end
135
+
136
+ @window__.pointer_up = proc do |e|
137
+ updateMouseState.call e.x, e.y, false
138
+ @mouseReleasedBlock__.call e if @mouseReleasedBlock__
139
+ end
68
140
 
69
- setupDrawBlock__
70
- setupMousePressedBlock__
71
- setupMouseReleasedBlock__
72
- setupMouseMovedBlock__
73
- setupMouseDraggedBlock__
141
+ @window__.pointer_move = proc do |e|
142
+ updateMouseState.call e.x, e.y
143
+ @mouseMovedBlock__.call e if @mouseMovedBlock__
144
+ end
145
+
146
+ @window__.pointer_drag = proc do |e|
147
+ updateMouseState.call e.x, e.y
148
+ @mouseDraggedBlock__.call e if @mouseDraggedBlock__
149
+ end
74
150
  end
75
151
 
76
152
  # Returns the absolute number of the value.
@@ -293,23 +369,6 @@ module RubySketch
293
369
  nil
294
370
  end
295
371
 
296
- # @private
297
- private def setupDrawBlock__ ()
298
- @window__.draw = proc do |e, painter|
299
- @painter__ = painter
300
- @matrixStack__.clear
301
- @styleStack__.clear
302
- begin
303
- push
304
- @drawBlock__.call e if @drawBlock__
305
- ensure
306
- pop
307
- end
308
- @frameCount__ += 1
309
- updateMousePrevPos__
310
- end
311
- end
312
-
313
372
  # Define draw block.
314
373
  #
315
374
  def draw (&block)
@@ -322,71 +381,26 @@ module RubySketch
322
381
  nil
323
382
  end
324
383
 
325
- # @private
326
- private def setupMousePressedBlock__ ()
327
- @window__.pointer_down = proc do |e|
328
- updateMouseState__ e.x, e.y, true
329
- @mousePressedBlock__.call e if @mousePressedBlock__
330
- end
331
- end
332
-
333
384
  def mousePressed (&block)
334
385
  @mousePressedBlock__ = block if block
335
386
  @mousePressed__
336
387
  end
337
388
 
338
- # @private
339
- private def setupMouseReleasedBlock__ ()
340
- @window__.pointer_up = proc do |e|
341
- updateMouseState__ e.x, e.y, false
342
- @mouseReleasedBlock__.call e if @mouseReleasedBlock__
343
- end
344
- end
345
-
346
389
  def mouseReleased (&block)
347
390
  @mouseReleasedBlock__ = block if block
348
391
  nil
349
392
  end
350
393
 
351
- # @private
352
- private def setupMouseMovedBlock__ ()
353
- @window__.pointer_move = proc do |e|
354
- updateMouseState__ e.x, e.y
355
- @mouseMovedBlock__.call e if @mouseMovedBlock__
356
- end
357
- end
358
-
359
394
  def mouseMoved (&block)
360
395
  @mouseMovedBlock__ = block if block
361
396
  nil
362
397
  end
363
398
 
364
- # @private
365
- private def setupMouseDraggedBlock__ ()
366
- @window__.pointer_drag = proc do |e|
367
- updateMouseState__ e.x, e.y
368
- @mouseDraggedBlock__.call e if @mouseDraggedBlock__
369
- end
370
- end
371
-
372
399
  def mouseDragged (&block)
373
400
  @mouseDraggedBlock__ = block if block
374
401
  nil
375
402
  end
376
403
 
377
- # @private
378
- private def updateMouseState__ (x, y, pressed = nil)
379
- @mouseX__ = x
380
- @mouseY__ = y
381
- @mousePressed__ = pressed if pressed != nil
382
- end
383
-
384
- # @private
385
- private def updateMousePrevPos__ ()
386
- @mousePrevX__ = @mouseX__
387
- @mousePrevY__ = @mouseY__
388
- end
389
-
390
404
  # @private
391
405
  private def size__ (width, height)
392
406
  raise 'size() must be called on startup or setup block' if @started__
@@ -577,6 +591,20 @@ module RubySketch
577
591
  @ellipseMode__ = mode
578
592
  end
579
593
 
594
+ # Sets image mode. Default is CORNER.
595
+ #
596
+ # CORNER -> image(img, left, top, width, height)
597
+ # CORNERS -> image(img, left, top, right, bottom)
598
+ # CENTER -> image(img, center_x, center_y, width, height)
599
+ #
600
+ # @param mode [CORNER, CORNERS, CENTER]
601
+ #
602
+ # @return [nil] nil
603
+ #
604
+ def imageMode (mode)
605
+ @imageMode__ = mode
606
+ end
607
+
580
608
  # @private
581
609
  private def toXYWH__ (mode, a, b, c, d)
582
610
  case mode
@@ -588,6 +616,30 @@ module RubySketch
588
616
  end
589
617
  end
590
618
 
619
+ # Enables calling draw block on every frame.
620
+ #
621
+ # @return [nil] nil
622
+ #
623
+ def loop ()
624
+ @loop__ = true
625
+ end
626
+
627
+ # Disables calling draw block on every frame.
628
+ #
629
+ # @return [nil] nil
630
+ #
631
+ def noLoop ()
632
+ @loop__ = false
633
+ end
634
+
635
+ # Calls draw block to redraw frame.
636
+ #
637
+ # @return [nil] nil
638
+ #
639
+ def redraw ()
640
+ @redraw__ = true
641
+ end
642
+
591
643
  # Clears screen.
592
644
  #
593
645
  # @overload background(str)
@@ -675,6 +727,28 @@ module RubySketch
675
727
  nil
676
728
  end
677
729
 
730
+ # Sets stroke cap mode.
731
+ #
732
+ # @param cap [BUTT, ROUND, SQUARE]
733
+ #
734
+ # @return [nil] nil
735
+ #
736
+ def strokeCap (cap)
737
+ @painter__.stroke_cap cap
738
+ nil
739
+ end
740
+
741
+ # Sets stroke join mode.
742
+ #
743
+ # @param join [MITER, ROUND, SQUARE]
744
+ #
745
+ # @return [nil] nil
746
+ #
747
+ def strokeJoin (join)
748
+ @painter__.stroke_join join
749
+ nil
750
+ end
751
+
678
752
  # Disables filling.
679
753
  #
680
754
  # @return [nil] nil
@@ -732,10 +806,10 @@ module RubySketch
732
806
 
733
807
  # Draws a line.
734
808
  #
735
- # @param x1 [Numeric] horizontal position for first point
736
- # @param y1 [Numeric] vertical position for first point
737
- # @param x2 [Numeric] horizontal position for second point
738
- # @param y2 [Numeric] vertical position for second point
809
+ # @param x1 [Numeric] horizontal position of first point
810
+ # @param y1 [Numeric] vertical position of first point
811
+ # @param x2 [Numeric] horizontal position of second point
812
+ # @param y2 [Numeric] vertical position of second point
739
813
  #
740
814
  # @return [nil] nil
741
815
  #
@@ -865,6 +939,42 @@ module RubySketch
865
939
  nil
866
940
  end
867
941
 
942
+ # Draws a Catmull-Rom spline curve.
943
+ #
944
+ # @param cx1 [Numeric] horizontal position of beginning control point
945
+ # @param cy1 [Numeric] vertical position of beginning control point
946
+ # @param x1 [Numeric] horizontal position of first point
947
+ # @param y1 [Numeric] vertical position of first point
948
+ # @param x2 [Numeric] horizontal position of second point
949
+ # @param y2 [Numeric] vertical position of second point
950
+ # @param cx2 [Numeric] horizontal position of ending control point
951
+ # @param cy2 [Numeric] vertical position of ending control point
952
+ #
953
+ # @return [nil] nil
954
+ #
955
+ def curve (cx1, cy1, x1, y1, x2, y2, cx2, cy2)
956
+ @painter__.curve cx1, cy1, x1, y1, x2, y2, cx2, cy2
957
+ nil
958
+ end
959
+
960
+ # Draws a Bezier spline curve.
961
+ #
962
+ # @param x1 [Numeric] horizontal position of first point
963
+ # @param y1 [Numeric] vertical position of first point
964
+ # @param cx1 [Numeric] horizontal position of first control point
965
+ # @param cy1 [Numeric] vertical position of first control point
966
+ # @param cx2 [Numeric] horizontal position of second control point
967
+ # @param cy2 [Numeric] vertical position of second control point
968
+ # @param x2 [Numeric] horizontal position of second point
969
+ # @param y2 [Numeric] vertical position of second point
970
+ #
971
+ # @return [nil] nil
972
+ #
973
+ def bezier (x1, y1, cx1, cy1, cx2, cy2, x2, y2)
974
+ @painter__.bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2
975
+ nil
976
+ end
977
+
868
978
  # Draws a text.
869
979
  #
870
980
  # @overload text(str)
@@ -883,24 +993,45 @@ module RubySketch
883
993
 
884
994
  # Draws an image.
885
995
  #
886
- # @overload image(img, x, y)
887
- # @overload image(img, x, y, w, h)
996
+ # @overload image(img, a, b)
997
+ # @overload image(img, a, b, c, d)
888
998
  #
889
999
  # @param img [Image] image to draw
890
- # @param x [Numeric] horizontal position of the image
891
- # @param y [Numeric] vertical position of the image
892
- # @param w [Numeric] width of the image
893
- # @param h [Numeric] height of the image
1000
+ # @param a [Numeric] horizontal position of the image
1001
+ # @param b [Numeric] vertical position of the image
1002
+ # @param c [Numeric] width of the image
1003
+ # @param d [Numeric] height of the image
894
1004
  #
895
1005
  # @return [nil] nil
896
1006
  #
897
- def image (img, x, y, w = nil, h = nil)
898
- w ||= img.width
899
- h ||= img.height
900
- @painter__.image img.internal, x, y, w, h
1007
+ def image (img, a, b, c = nil, d = nil)
1008
+ x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
1009
+ @painter__.image img.to_internal__, x, y, w, h
901
1010
  nil
902
1011
  end
903
1012
 
1013
+ # Copies image.
1014
+ #
1015
+ # @overload copy(sx, sy, sw, sh, dx, dy, dw, dh)
1016
+ # @overload copy(img, sx, sy, sw, sh, dx, dy, dw, dh)
1017
+ #
1018
+ # @param img [Image] image for copy source
1019
+ # @param sx [Numrtic] x position of source region
1020
+ # @param sy [Numrtic] y position of source region
1021
+ # @param sw [Numrtic] width of source region
1022
+ # @param sh [Numrtic] height of source region
1023
+ # @param dx [Numrtic] x position of destination region
1024
+ # @param dy [Numrtic] y position of destination region
1025
+ # @param dw [Numrtic] width of destination region
1026
+ # @param dh [Numrtic] height of destination region
1027
+ #
1028
+ # @return [nil] nil
1029
+ #
1030
+ def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
1031
+ src = img&.to_internal__ || @window__.canvas
1032
+ @painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
1033
+ end
1034
+
904
1035
  # Applies translation matrix to current transformation matrix.
905
1036
  #
906
1037
  # @param x [Numeric] horizontal transformation
@@ -944,8 +1075,12 @@ module RubySketch
944
1075
  #
945
1076
  # @return [nil] nil
946
1077
  #
947
- def pushMatrix ()
1078
+ def pushMatrix (&block)
948
1079
  @matrixStack__.push @painter__.matrix
1080
+ if block
1081
+ block.call
1082
+ popMatrix
1083
+ end
949
1084
  nil
950
1085
  end
951
1086
 
@@ -972,18 +1107,25 @@ module RubySketch
972
1107
  #
973
1108
  # @return [nil] nil
974
1109
  #
975
- def pushStyle ()
1110
+ def pushStyle (&block)
976
1111
  @styleStack__.push [
977
1112
  @painter__.fill,
978
1113
  @painter__.stroke,
979
1114
  @painter__.stroke_width,
1115
+ @painter__.stroke_cap,
1116
+ @painter__.stroke_join,
980
1117
  @painter__.font,
981
1118
  @hsbColor__,
982
1119
  @colorMaxes__,
983
1120
  @angleScale__,
984
1121
  @rectMode__,
985
- @ellipseMode__
1122
+ @ellipseMode__,
1123
+ @imageMode__
986
1124
  ]
1125
+ if block
1126
+ block.call
1127
+ popStyle
1128
+ end
987
1129
  nil
988
1130
  end
989
1131
 
@@ -996,12 +1138,15 @@ module RubySketch
996
1138
  @painter__.fill,
997
1139
  @painter__.stroke,
998
1140
  @painter__.stroke_width,
1141
+ @painter__.stroke_cap,
1142
+ @painter__.stroke_join,
999
1143
  @painter__.font,
1000
1144
  @hsbColor__,
1001
1145
  @colorMaxes__,
1002
1146
  @angleScale__,
1003
1147
  @rectMode__,
1004
- @ellipseMode__ = @styleStack__.pop
1148
+ @ellipseMode__,
1149
+ @imageMode__ = @styleStack__.pop
1005
1150
  nil
1006
1151
  end
1007
1152
 
@@ -1009,9 +1154,13 @@ module RubySketch
1009
1154
  #
1010
1155
  # @return [nil] nil
1011
1156
  #
1012
- def push ()
1157
+ def push (&block)
1013
1158
  pushMatrix
1014
1159
  pushStyle
1160
+ if block
1161
+ block.call
1162
+ pop
1163
+ end
1015
1164
  end
1016
1165
 
1017
1166
  # Restore styles and transformations from stack.
@@ -1027,14 +1176,16 @@ module RubySketch
1027
1176
  #
1028
1177
  # @overload noise(x)
1029
1178
  # @overload noise(x, y)
1179
+ # @overload noise(x, y, z)
1030
1180
  #
1031
1181
  # @param x [Numeric] horizontal point in noise space
1032
1182
  # @param y [Numeric] vertical point in noise space
1183
+ # @param z [Numeric] depth point in noise space
1033
1184
  #
1034
1185
  # @return [Numeric] noise value (0.0..1.0)
1035
1186
  #
1036
- def noise (x, y = 0)
1037
- Rays.perlin(x, y) / 2.0 + 1.0
1187
+ def noise (x, y = 0, z = 0)
1188
+ Rays.perlin(x, y, z) / 2.0 + 0.5
1038
1189
  end
1039
1190
 
1040
1191
  # Loads image.
@@ -1042,6 +1193,8 @@ module RubySketch
1042
1193
  # @param filename [String] file name to load image
1043
1194
  # @param extension [String] type of image to load (ex. 'png')
1044
1195
  #
1196
+ # @return [Image] loaded image object
1197
+ #
1045
1198
  def loadImage (filename, extension = nil)
1046
1199
  filename = getImage__ filename, extension if filename =~ %r|^https?://|
1047
1200
  Image.new Rays::Image.load filename
@@ -1050,17 +1203,19 @@ module RubySketch
1050
1203
  # @private
1051
1204
  private def getImage__ (uri, ext)
1052
1205
  ext ||= File.extname uri
1053
- raise "unsupported image type" unless ext =~ /^\.?(png)$/i
1206
+ raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
1054
1207
 
1055
1208
  tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
1056
1209
  path = tmpdir + Digest::SHA1.hexdigest(uri)
1057
1210
  path = path.sub_ext ext
1058
1211
 
1059
- URI.open uri do |input|
1060
- tmpdir.mkdir unless tmpdir.directory?
1061
- path.open('w') do |output|
1062
- while buf = input.read(2 ** 16)
1063
- output.write buf
1212
+ unless path.file?
1213
+ URI.open uri do |input|
1214
+ tmpdir.mkdir unless tmpdir.directory?
1215
+ path.open('w') do |output|
1216
+ while buf = input.read(2 ** 16)
1217
+ output.write buf
1218
+ end
1064
1219
  end
1065
1220
  end
1066
1221
  end
@@ -1074,8 +1229,7 @@ module RubySketch
1074
1229
  #
1075
1230
  class Processing::Image
1076
1231
 
1077
- # Initialize image.
1078
- #
1232
+ # @private
1079
1233
  def initialize (image)
1080
1234
  @image = image
1081
1235
  end
@@ -1096,6 +1250,44 @@ module RubySketch
1096
1250
  @image.height
1097
1251
  end
1098
1252
 
1253
+ # Resizes image.
1254
+ #
1255
+ # @param width [Numeric] width for resized image
1256
+ # @param height [Numeric] height for resized image
1257
+ #
1258
+ # @return [nil] nil
1259
+ #
1260
+ def resize (width, height)
1261
+ @image = Rays::Image.new(width, height).paint do |painter|
1262
+ painter.image @image, 0, 0, width, height
1263
+ end
1264
+ nil
1265
+ end
1266
+
1267
+ # Copies image.
1268
+ #
1269
+ # @overload copy(sx, sy, sw, sh, dx, dy, dw, dh)
1270
+ # @overload copy(img, sx, sy, sw, sh, dx, dy, dw, dh)
1271
+ #
1272
+ # @param img [Image] image for copy source
1273
+ # @param sx [Numrtic] x position of source region
1274
+ # @param sy [Numrtic] y position of source region
1275
+ # @param sw [Numrtic] width of source region
1276
+ # @param sh [Numrtic] height of source region
1277
+ # @param dx [Numrtic] x position of destination region
1278
+ # @param dy [Numrtic] y position of destination region
1279
+ # @param dw [Numrtic] width of destination region
1280
+ # @param dh [Numrtic] height of destination region
1281
+ #
1282
+ # @return [nil] nil
1283
+ #
1284
+ def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
1285
+ img ||= self
1286
+ @image.paint do |painter|
1287
+ painter.image img.to_internal__, sx, sy, sw, sh, dx, dy, dw, dh
1288
+ end
1289
+ end
1290
+
1099
1291
  # Saves image to file.
1100
1292
  #
1101
1293
  # @param filename [String] file name to save image
@@ -1105,7 +1297,7 @@ module RubySketch
1105
1297
  end
1106
1298
 
1107
1299
  # @private
1108
- def internal ()
1300
+ def to_internal__ ()
1109
1301
  @image
1110
1302
  end
1111
1303
 
@@ -1116,10 +1308,7 @@ module RubySketch
1116
1308
  #
1117
1309
  class Processing::Font
1118
1310
 
1119
- # Initialize font.
1120
- #
1121
1311
  # @private
1122
- #
1123
1312
  def initialize (font)
1124
1313
  @font = font
1125
1314
  end
@@ -1165,13 +1354,7 @@ module RubySketch
1165
1354
  #
1166
1355
  attr_reader :h
1167
1356
 
1168
- # Initialize bouding box.
1169
- #
1170
- # @param x [Numeric] horizontal position
1171
- # @param y [Numeric] vertical position
1172
- # @param w [Numeric] width of bounding box
1173
- # @param h [Numeric] height of bounding box
1174
- #
1357
+ # @private
1175
1358
  def initialize (x, y, w, h)
1176
1359
  @x, @y, @w, @h = x, y, w, h
1177
1360
  end
@@ -17,9 +17,7 @@ module RubySketch
17
17
 
18
18
  reset_canvas 1, 1
19
19
 
20
- super *args, size: [width, height] do |_|
21
- start &block if block
22
- end
20
+ super *args, size: [width, height], &block
23
21
  end
24
22
 
25
23
  def start (&block)
@@ -43,8 +41,8 @@ module RubySketch
43
41
  end
44
42
 
45
43
  def on_draw (e)
46
- @canvas_painter.paint do |painter|
47
- call_block @draw, e, painter
44
+ @canvas_painter.paint do |_|
45
+ call_block @draw, e
48
46
  end
49
47
  e.painter.image @canvas
50
48
  end
@@ -80,7 +78,7 @@ module RubySketch
80
78
  old_canvas = @canvas
81
79
  old_painter = @canvas_painter
82
80
 
83
- cs = old_canvas&.color_space || Rays::ColorSpace::RGBA
81
+ cs = old_canvas&.color_space || Rays::RGBA
84
82
  @canvas = Rays::Image.new width, height, cs, painter.pixel_density
85
83
  @canvas_painter = @canvas.painter
86
84
 
@@ -94,6 +92,9 @@ module RubySketch
94
92
  to.fill = from.fill
95
93
  to.stroke = from.stroke
96
94
  to.stroke_width = from.stroke_width
95
+ to.stroke_cap = from.stroke_cap
96
+ to.stroke_join = from.stroke_join
97
+ to.miter_limit = from.miter_limit
97
98
  to.font = from.font
98
99
  end
99
100
 
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.2.2
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-28 00:00:00.000000000 Z
11
+ date: 2020-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -103,6 +103,7 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
105
  - ".yardopts"
106
+ - ChangeLog.md
106
107
  - LICENSE
107
108
  - README.md
108
109
  - Rakefile
@@ -117,7 +118,6 @@ files:
117
118
  - lib/rubysketch/glsl.rb
118
119
  - lib/rubysketch/module.rb
119
120
  - lib/rubysketch/processing.rb
120
- - lib/rubysketch/starter.rb
121
121
  - lib/rubysketch/window.rb
122
122
  - rubysketch.gemspec
123
123
  homepage: https://github.com/xord/rubysketch
@@ -1,67 +0,0 @@
1
- module RubySketch
2
-
3
-
4
- extend module Functions
5
-
6
- def start (context, start_at_exit: false, &block)
7
- window = Window.new do
8
- context.instance_eval &block if block
9
- end
10
-
11
- context.on_start__ window
12
-
13
- start = proc do
14
- Reflex.start {window.show}
15
- end
16
-
17
- if start_at_exit
18
- at_exit {start.call unless $!}
19
- else
20
- start.call
21
- end
22
-
23
- window
24
- end
25
-
26
- private
27
-
28
- def start_on_object_at_exit (object, context)
29
- klass = context.class
30
- methods = klass.instance_methods(false)
31
- .reject {|name| name =~ /__$/}
32
- consts = klass.constants
33
- .reject {|name| name =~ /__$/}
34
- .each_with_object({}) {|name, h| h[name] = klass.const_get name}
35
-
36
- object.class.class_eval do
37
- methods.each do |name|
38
- define_method name do |*args, &block|
39
- context.__send__ name, *args, &block
40
- end
41
- end
42
- consts.each do |(name, value)|
43
- const_set name, value
44
- end
45
- end
46
-
47
- window = start context, start_at_exit: true
48
-
49
- window.canvas_painter.__send__ :begin_paint
50
- at_exit {window.canvas_painter.__send__ :end_paint}
51
- end
52
-
53
- self
54
-
55
- end# Functions
56
-
57
-
58
- module Starter
59
-
60
- def start (*args, &block)
61
- RubySketch.start self.new(*args), start_at_exit: true, &block
62
- end
63
-
64
- end# Starter
65
-
66
-
67
- end# RubySketch