rubysketch 0.2.1 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ChangeLog.md +17 -0
- data/VERSION +1 -1
- data/examples/clock.rb +4 -13
- data/examples/image.rb +1 -1
- data/lib/rubysketch-processing.rb +30 -1
- data/lib/rubysketch.rb +5 -1
- data/lib/rubysketch/glsl.rb +0 -2
- data/lib/rubysketch/processing.rb +282 -126
- data/lib/rubysketch/window.rb +7 -6
- metadata +3 -4
- data/examples/rubysketch.png +0 -0
- data/lib/rubysketch/starter.rb +0 -67
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9068a9fe7e5239420f8e32fb2e30912748bf9c90c52f40516604997b1166830a
|
4
|
+
data.tar.gz: 2a75b3ec215aeb15bfba929f561abd2a4f94c4c8093b27bad4de1ee74dd15efa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7a6252c38ab1fdda0c761c390bb5d0bb3583e740d7beea5efe3c5939a1f52840aac93c41aee693870dde4539df3f69dba20476c34b85a2eb5791fb3bcd63498
|
7
|
+
data.tar.gz: ccd6c65ff6b5e0340e7a8e779c2d1da2f277294ae9e03386a7d1317568badade0d4fe51758559680ef22ccc080486a3d2df43dab0501c91c9a6da3cc7144eb1d
|
data/ChangeLog.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# RubySketch ChangeLog
|
2
|
+
|
3
|
+
|
4
|
+
## [0.2.6] - 2020-04-17
|
5
|
+
|
6
|
+
- push(), pushMatrix() and pushStyle() take block to automatic pop
|
7
|
+
- add stroke_cap() and stroke_join()
|
8
|
+
- refine startup process
|
9
|
+
- add curve() and bezier()
|
10
|
+
- add imageMode(), Image#resize(), Image#copy() and copy()
|
11
|
+
- add loop(), noLoop() and redraw()
|
12
|
+
|
13
|
+
|
14
|
+
## [0.2.5] - 2020-03-29
|
15
|
+
|
16
|
+
- delete debug prints
|
17
|
+
- show unsupported image type
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.6
|
data/examples/clock.rb
CHANGED
@@ -20,12 +20,10 @@ end
|
|
20
20
|
draw do
|
21
21
|
background 0
|
22
22
|
|
23
|
-
|
24
|
-
pushMatrix
|
23
|
+
pushMatrix do
|
25
24
|
translate width / 2, height / 2
|
26
25
|
|
27
|
-
|
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
|
-
|
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
|
-
|
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
|
data/examples/image.rb
CHANGED
@@ -1,4 +1,33 @@
|
|
1
1
|
require 'rubysketch'
|
2
2
|
|
3
3
|
|
4
|
-
|
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
|
data/lib/rubysketch.rb
CHANGED
data/lib/rubysketch/glsl.rb
CHANGED
@@ -7,8 +7,6 @@ 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
|
@@ -43,6 +41,8 @@ module RubySketch
|
|
43
41
|
|
44
42
|
# @private
|
45
43
|
def initialize ()
|
44
|
+
@loop__ = true
|
45
|
+
@redraw__ = false
|
46
46
|
@matrixStack__ = []
|
47
47
|
@styleStack__ = []
|
48
48
|
@frameCount__ = 0
|
@@ -59,18 +59,67 @@ module RubySketch
|
|
59
59
|
angleMode RADIANS
|
60
60
|
rectMode CORNER
|
61
61
|
ellipseMode CENTER
|
62
|
+
imageMode CORNER
|
62
63
|
end
|
63
64
|
|
64
65
|
# @private
|
65
|
-
def
|
66
|
+
def setup__ (window)
|
66
67
|
@window__ = window
|
67
|
-
@painter__ = window.canvas_painter
|
68
|
+
@painter__ = window.canvas_painter.tap do |o|
|
69
|
+
o.miter_limit = 10
|
70
|
+
end
|
71
|
+
|
72
|
+
drawFrame = -> event {
|
73
|
+
@painter__ = window.canvas_painter
|
74
|
+
@matrixStack__.clear
|
75
|
+
@styleStack__.clear
|
76
|
+
begin
|
77
|
+
push
|
78
|
+
@drawBlock__.call event if @drawBlock__
|
79
|
+
ensure
|
80
|
+
pop
|
81
|
+
end
|
82
|
+
@frameCount__ += 1
|
83
|
+
}
|
84
|
+
|
85
|
+
updateMouseState = -> x, y, pressed = nil {
|
86
|
+
@mouseX__ = x
|
87
|
+
@mouseY__ = y
|
88
|
+
@mousePressed__ = pressed if pressed != nil
|
89
|
+
}
|
90
|
+
|
91
|
+
updateMousePrevPos = -> {
|
92
|
+
@mousePrevX__ = @mouseX__
|
93
|
+
@mousePrevY__ = @mouseY__
|
94
|
+
}
|
95
|
+
|
96
|
+
@window__.draw = proc do |e|
|
97
|
+
if @loop__ || @redraw__
|
98
|
+
@redraw__ = false
|
99
|
+
drawFrame.call e
|
100
|
+
end
|
101
|
+
updateMousePrevPos.call
|
102
|
+
end
|
103
|
+
|
104
|
+
@window__.pointer_down = proc do |e|
|
105
|
+
updateMouseState.call e.x, e.y, true
|
106
|
+
@mousePressedBlock__.call e if @mousePressedBlock__
|
107
|
+
end
|
108
|
+
|
109
|
+
@window__.pointer_up = proc do |e|
|
110
|
+
updateMouseState.call e.x, e.y, false
|
111
|
+
@mouseReleasedBlock__.call e if @mouseReleasedBlock__
|
112
|
+
end
|
113
|
+
|
114
|
+
@window__.pointer_move = proc do |e|
|
115
|
+
updateMouseState.call e.x, e.y
|
116
|
+
@mouseMovedBlock__.call e if @mouseMovedBlock__
|
117
|
+
end
|
68
118
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
setupMouseDraggedBlock__
|
119
|
+
@window__.pointer_drag = proc do |e|
|
120
|
+
updateMouseState.call e.x, e.y
|
121
|
+
@mouseDraggedBlock__.call e if @mouseDraggedBlock__
|
122
|
+
end
|
74
123
|
end
|
75
124
|
|
76
125
|
# Returns the absolute number of the value.
|
@@ -293,23 +342,6 @@ module RubySketch
|
|
293
342
|
nil
|
294
343
|
end
|
295
344
|
|
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
345
|
# Define draw block.
|
314
346
|
#
|
315
347
|
def draw (&block)
|
@@ -322,71 +354,26 @@ module RubySketch
|
|
322
354
|
nil
|
323
355
|
end
|
324
356
|
|
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
357
|
def mousePressed (&block)
|
334
358
|
@mousePressedBlock__ = block if block
|
335
359
|
@mousePressed__
|
336
360
|
end
|
337
361
|
|
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
362
|
def mouseReleased (&block)
|
347
363
|
@mouseReleasedBlock__ = block if block
|
348
364
|
nil
|
349
365
|
end
|
350
366
|
|
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
367
|
def mouseMoved (&block)
|
360
368
|
@mouseMovedBlock__ = block if block
|
361
369
|
nil
|
362
370
|
end
|
363
371
|
|
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
372
|
def mouseDragged (&block)
|
373
373
|
@mouseDraggedBlock__ = block if block
|
374
374
|
nil
|
375
375
|
end
|
376
376
|
|
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
377
|
# @private
|
391
378
|
private def size__ (width, height)
|
392
379
|
raise 'size() must be called on startup or setup block' if @started__
|
@@ -487,7 +474,7 @@ module RubySketch
|
|
487
474
|
# @return [nil] nil
|
488
475
|
#
|
489
476
|
def colorMode (mode, *maxes)
|
490
|
-
raise ArgumentError, "
|
477
|
+
raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
|
491
478
|
raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)
|
492
479
|
|
493
480
|
@hsbColor__ = mode.upcase == HSB
|
@@ -499,9 +486,9 @@ module RubySketch
|
|
499
486
|
end
|
500
487
|
|
501
488
|
# @private
|
502
|
-
private def
|
489
|
+
private def toRGBA__ (*args)
|
503
490
|
a, b, c, d = args
|
504
|
-
return
|
491
|
+
return parseColor__(a, b || alphaMax__) if a.kind_of?(String)
|
505
492
|
|
506
493
|
rgba = case args.size
|
507
494
|
when 1, 2 then [a, a, a, b || alphaMax__]
|
@@ -514,9 +501,9 @@ module RubySketch
|
|
514
501
|
end
|
515
502
|
|
516
503
|
# @private
|
517
|
-
private def
|
504
|
+
private def parseColor__ (str, alpha)
|
518
505
|
result = str.match /^\s*##{'([0-9a-f]{2})' * 3}\s*$/i
|
519
|
-
raise ArgumentError, "
|
506
|
+
raise ArgumentError, "invalid color code: '#{str}'" unless result
|
520
507
|
|
521
508
|
rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) / 255.0}
|
522
509
|
return *rgb, (alpha / alphaMax__)
|
@@ -537,13 +524,13 @@ module RubySketch
|
|
537
524
|
@angleScale__ = case mode
|
538
525
|
when RADIANS then RAD2DEG__
|
539
526
|
when DEGREES then 1.0
|
540
|
-
else raise ArgumentError, "
|
527
|
+
else raise ArgumentError, "invalid angle mode: #{mode}"
|
541
528
|
end
|
542
529
|
nil
|
543
530
|
end
|
544
531
|
|
545
532
|
# @private
|
546
|
-
private def
|
533
|
+
private def toAngle__ (angle)
|
547
534
|
angle * @angleScale__
|
548
535
|
end
|
549
536
|
|
@@ -577,8 +564,22 @@ module RubySketch
|
|
577
564
|
@ellipseMode__ = mode
|
578
565
|
end
|
579
566
|
|
567
|
+
# Sets image mode. Default is CORNER.
|
568
|
+
#
|
569
|
+
# CORNER -> image(img, left, top, width, height)
|
570
|
+
# CORNERS -> image(img, left, top, right, bottom)
|
571
|
+
# CENTER -> image(img, center_x, center_y, width, height)
|
572
|
+
#
|
573
|
+
# @param mode [CORNER, CORNERS, CENTER]
|
574
|
+
#
|
575
|
+
# @return [nil] nil
|
576
|
+
#
|
577
|
+
def imageMode (mode)
|
578
|
+
@imageMode__ = mode
|
579
|
+
end
|
580
|
+
|
580
581
|
# @private
|
581
|
-
private def
|
582
|
+
private def toXYWH__ (mode, a, b, c, d)
|
582
583
|
case mode
|
583
584
|
when CORNER then [a, b, c, d]
|
584
585
|
when CORNERS then [a, b, c - a, d - b]
|
@@ -588,6 +589,30 @@ module RubySketch
|
|
588
589
|
end
|
589
590
|
end
|
590
591
|
|
592
|
+
# Enables calling draw block on every frame.
|
593
|
+
#
|
594
|
+
# @return [nil] nil
|
595
|
+
#
|
596
|
+
def loop ()
|
597
|
+
@loop__ = true
|
598
|
+
end
|
599
|
+
|
600
|
+
# Disables calling draw block on every frame.
|
601
|
+
#
|
602
|
+
# @return [nil] nil
|
603
|
+
#
|
604
|
+
def noLoop ()
|
605
|
+
@loop__ = false
|
606
|
+
end
|
607
|
+
|
608
|
+
# Calls draw block to redraw frame.
|
609
|
+
#
|
610
|
+
# @return [nil] nil
|
611
|
+
#
|
612
|
+
def redraw ()
|
613
|
+
@redraw__ = true
|
614
|
+
end
|
615
|
+
|
591
616
|
# Clears screen.
|
592
617
|
#
|
593
618
|
# @overload background(str)
|
@@ -607,7 +632,7 @@ module RubySketch
|
|
607
632
|
# @return [nil] nil
|
608
633
|
#
|
609
634
|
def background (*args)
|
610
|
-
rgba =
|
635
|
+
rgba = toRGBA__ *args
|
611
636
|
if rgba[3] == 1
|
612
637
|
@painter__.background *rgba
|
613
638
|
else
|
@@ -637,7 +662,7 @@ module RubySketch
|
|
637
662
|
# @return [nil] nil
|
638
663
|
#
|
639
664
|
def fill (*args)
|
640
|
-
@painter__.fill(*
|
665
|
+
@painter__.fill(*toRGBA__(*args))
|
641
666
|
nil
|
642
667
|
end
|
643
668
|
|
@@ -660,7 +685,7 @@ module RubySketch
|
|
660
685
|
# @return [nil] nil
|
661
686
|
#
|
662
687
|
def stroke (*args)
|
663
|
-
@painter__.stroke(*
|
688
|
+
@painter__.stroke(*toRGBA__(*args))
|
664
689
|
nil
|
665
690
|
end
|
666
691
|
|
@@ -732,10 +757,10 @@ module RubySketch
|
|
732
757
|
|
733
758
|
# Draws a line.
|
734
759
|
#
|
735
|
-
# @param x1 [Numeric] horizontal position
|
736
|
-
# @param y1 [Numeric] vertical position
|
737
|
-
# @param x2 [Numeric] horizontal position
|
738
|
-
# @param y2 [Numeric] vertical position
|
760
|
+
# @param x1 [Numeric] horizontal position of first point
|
761
|
+
# @param y1 [Numeric] vertical position of first point
|
762
|
+
# @param x2 [Numeric] horizontal position of second point
|
763
|
+
# @param y2 [Numeric] vertical position of second point
|
739
764
|
#
|
740
765
|
# @return [nil] nil
|
741
766
|
#
|
@@ -763,7 +788,7 @@ module RubySketch
|
|
763
788
|
# @return [nil] nil
|
764
789
|
#
|
765
790
|
def rect (a, b, c, d, *args)
|
766
|
-
x, y, w, h =
|
791
|
+
x, y, w, h = toXYWH__ @rectMode__, a, b, c, d
|
767
792
|
case args.size
|
768
793
|
when 0 then @painter__.rect x, y, w, h
|
769
794
|
when 1 then @painter__.rect x, y, w, h, round: args[0]
|
@@ -783,7 +808,7 @@ module RubySketch
|
|
783
808
|
# @return [nil] nil
|
784
809
|
#
|
785
810
|
def ellipse (a, b, c, d)
|
786
|
-
x, y, w, h =
|
811
|
+
x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
|
787
812
|
@painter__.ellipse x, y, w, h
|
788
813
|
nil
|
789
814
|
end
|
@@ -812,9 +837,9 @@ module RubySketch
|
|
812
837
|
# @return [nil] nil
|
813
838
|
#
|
814
839
|
def arc (a, b, c, d, start, stop)
|
815
|
-
x, y, w, h =
|
816
|
-
start =
|
817
|
-
stop =
|
840
|
+
x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
|
841
|
+
start = toAngle__ start
|
842
|
+
stop = toAngle__ stop
|
818
843
|
@painter__.ellipse x, y, w, h, from: start, to: stop
|
819
844
|
nil
|
820
845
|
end
|
@@ -865,6 +890,42 @@ module RubySketch
|
|
865
890
|
nil
|
866
891
|
end
|
867
892
|
|
893
|
+
# Draws a Catmull-Rom spline curve.
|
894
|
+
#
|
895
|
+
# @param cx1 [Numeric] horizontal position of beginning control point
|
896
|
+
# @param cy1 [Numeric] vertical position of beginning control point
|
897
|
+
# @param x1 [Numeric] horizontal position of first point
|
898
|
+
# @param y1 [Numeric] vertical position of first point
|
899
|
+
# @param x2 [Numeric] horizontal position of second point
|
900
|
+
# @param y2 [Numeric] vertical position of second point
|
901
|
+
# @param cx2 [Numeric] horizontal position of ending control point
|
902
|
+
# @param cy2 [Numeric] vertical position of ending control point
|
903
|
+
#
|
904
|
+
# @return [nil] nil
|
905
|
+
#
|
906
|
+
def curve (cx1, cy1, x1, y1, x2, y2, cx2, cy2)
|
907
|
+
@painter__.curve cx1, cy1, x1, y1, x2, y2, cx2, cy2
|
908
|
+
nil
|
909
|
+
end
|
910
|
+
|
911
|
+
# Draws a Bezier spline curve.
|
912
|
+
#
|
913
|
+
# @param x1 [Numeric] horizontal position of first point
|
914
|
+
# @param y1 [Numeric] vertical position of first point
|
915
|
+
# @param cx1 [Numeric] horizontal position of first control point
|
916
|
+
# @param cy1 [Numeric] vertical position of first control point
|
917
|
+
# @param cx2 [Numeric] horizontal position of second control point
|
918
|
+
# @param cy2 [Numeric] vertical position of second control point
|
919
|
+
# @param x2 [Numeric] horizontal position of second point
|
920
|
+
# @param y2 [Numeric] vertical position of second point
|
921
|
+
#
|
922
|
+
# @return [nil] nil
|
923
|
+
#
|
924
|
+
def bezier (x1, y1, cx1, cy1, cx2, cy2, x2, y2)
|
925
|
+
@painter__.bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2
|
926
|
+
nil
|
927
|
+
end
|
928
|
+
|
868
929
|
# Draws a text.
|
869
930
|
#
|
870
931
|
# @overload text(str)
|
@@ -883,24 +944,45 @@ module RubySketch
|
|
883
944
|
|
884
945
|
# Draws an image.
|
885
946
|
#
|
886
|
-
# @overload image(img,
|
887
|
-
# @overload image(img,
|
947
|
+
# @overload image(img, a, b)
|
948
|
+
# @overload image(img, a, b, c, d)
|
888
949
|
#
|
889
950
|
# @param img [Image] image to draw
|
890
|
-
# @param
|
891
|
-
# @param
|
892
|
-
# @param
|
893
|
-
# @param
|
951
|
+
# @param a [Numeric] horizontal position of the image
|
952
|
+
# @param b [Numeric] vertical position of the image
|
953
|
+
# @param c [Numeric] width of the image
|
954
|
+
# @param d [Numeric] height of the image
|
894
955
|
#
|
895
956
|
# @return [nil] nil
|
896
957
|
#
|
897
|
-
def image (img,
|
898
|
-
w
|
899
|
-
|
900
|
-
@painter__.image img.internal, x, y, w, h
|
958
|
+
def image (img, a, b, c = nil, d = nil)
|
959
|
+
x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
|
960
|
+
@painter__.image img.to_internal__, x, y, w, h
|
901
961
|
nil
|
902
962
|
end
|
903
963
|
|
964
|
+
# Copies image.
|
965
|
+
#
|
966
|
+
# @overload copy(sx, sy, sw, sh, dx, dy, dw, dh)
|
967
|
+
# @overload copy(img, sx, sy, sw, sh, dx, dy, dw, dh)
|
968
|
+
#
|
969
|
+
# @param img [Image] image for copy source
|
970
|
+
# @param sx [Numrtic] x position of source region
|
971
|
+
# @param sy [Numrtic] y position of source region
|
972
|
+
# @param sw [Numrtic] width of source region
|
973
|
+
# @param sh [Numrtic] height of source region
|
974
|
+
# @param dx [Numrtic] x position of destination region
|
975
|
+
# @param dy [Numrtic] y position of destination region
|
976
|
+
# @param dw [Numrtic] width of destination region
|
977
|
+
# @param dh [Numrtic] height of destination region
|
978
|
+
#
|
979
|
+
# @return [nil] nil
|
980
|
+
#
|
981
|
+
def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
|
982
|
+
src = img&.to_internal__ || @window__.canvas
|
983
|
+
@painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
|
984
|
+
end
|
985
|
+
|
904
986
|
# Applies translation matrix to current transformation matrix.
|
905
987
|
#
|
906
988
|
# @param x [Numeric] horizontal transformation
|
@@ -936,7 +1018,7 @@ module RubySketch
|
|
936
1018
|
# @return [nil] nil
|
937
1019
|
#
|
938
1020
|
def rotate (angle)
|
939
|
-
@painter__.rotate
|
1021
|
+
@painter__.rotate toAngle__ angle
|
940
1022
|
nil
|
941
1023
|
end
|
942
1024
|
|
@@ -944,8 +1026,12 @@ module RubySketch
|
|
944
1026
|
#
|
945
1027
|
# @return [nil] nil
|
946
1028
|
#
|
947
|
-
def pushMatrix ()
|
1029
|
+
def pushMatrix (&block)
|
948
1030
|
@matrixStack__.push @painter__.matrix
|
1031
|
+
if block
|
1032
|
+
block.call
|
1033
|
+
popMatrix
|
1034
|
+
end
|
949
1035
|
nil
|
950
1036
|
end
|
951
1037
|
|
@@ -954,7 +1040,7 @@ module RubySketch
|
|
954
1040
|
# @return [nil] nil
|
955
1041
|
#
|
956
1042
|
def popMatrix ()
|
957
|
-
raise "
|
1043
|
+
raise "matrix stack underflow" if @matrixStack__.empty?
|
958
1044
|
@painter__.matrix = @matrixStack__.pop
|
959
1045
|
nil
|
960
1046
|
end
|
@@ -972,18 +1058,25 @@ module RubySketch
|
|
972
1058
|
#
|
973
1059
|
# @return [nil] nil
|
974
1060
|
#
|
975
|
-
def pushStyle ()
|
1061
|
+
def pushStyle (&block)
|
976
1062
|
@styleStack__.push [
|
977
1063
|
@painter__.fill,
|
978
1064
|
@painter__.stroke,
|
979
1065
|
@painter__.stroke_width,
|
1066
|
+
@painter__.stroke_cap,
|
1067
|
+
@painter__.stroke_join,
|
980
1068
|
@painter__.font,
|
981
1069
|
@hsbColor__,
|
982
1070
|
@colorMaxes__,
|
983
1071
|
@angleScale__,
|
984
1072
|
@rectMode__,
|
985
|
-
@ellipseMode__
|
1073
|
+
@ellipseMode__,
|
1074
|
+
@imageMode__
|
986
1075
|
]
|
1076
|
+
if block
|
1077
|
+
block.call
|
1078
|
+
popStyle
|
1079
|
+
end
|
987
1080
|
nil
|
988
1081
|
end
|
989
1082
|
|
@@ -992,16 +1085,19 @@ module RubySketch
|
|
992
1085
|
# @return [nil] nil
|
993
1086
|
#
|
994
1087
|
def popStyle ()
|
995
|
-
raise "
|
1088
|
+
raise "style stack underflow" if @styleStack__.empty?
|
996
1089
|
@painter__.fill,
|
997
1090
|
@painter__.stroke,
|
998
1091
|
@painter__.stroke_width,
|
1092
|
+
@painter__.stroke_cap,
|
1093
|
+
@painter__.stroke_join,
|
999
1094
|
@painter__.font,
|
1000
1095
|
@hsbColor__,
|
1001
1096
|
@colorMaxes__,
|
1002
1097
|
@angleScale__,
|
1003
1098
|
@rectMode__,
|
1004
|
-
@ellipseMode__
|
1099
|
+
@ellipseMode__,
|
1100
|
+
@imageMode__ = @styleStack__.pop
|
1005
1101
|
nil
|
1006
1102
|
end
|
1007
1103
|
|
@@ -1009,9 +1105,13 @@ module RubySketch
|
|
1009
1105
|
#
|
1010
1106
|
# @return [nil] nil
|
1011
1107
|
#
|
1012
|
-
def push ()
|
1108
|
+
def push (&block)
|
1013
1109
|
pushMatrix
|
1014
1110
|
pushStyle
|
1111
|
+
if block
|
1112
|
+
block.call
|
1113
|
+
pop
|
1114
|
+
end
|
1015
1115
|
end
|
1016
1116
|
|
1017
1117
|
# Restore styles and transformations from stack.
|
@@ -1027,24 +1127,52 @@ module RubySketch
|
|
1027
1127
|
#
|
1028
1128
|
# @overload noise(x)
|
1029
1129
|
# @overload noise(x, y)
|
1130
|
+
# @overload noise(x, y, z)
|
1030
1131
|
#
|
1031
1132
|
# @param x [Numeric] horizontal point in noise space
|
1032
1133
|
# @param y [Numeric] vertical point in noise space
|
1134
|
+
# @param z [Numeric] depth point in noise space
|
1033
1135
|
#
|
1034
1136
|
# @return [Numeric] noise value (0.0..1.0)
|
1035
1137
|
#
|
1036
|
-
def noise (x, y = 0)
|
1037
|
-
Rays.perlin(x, y) / 2.0 +
|
1138
|
+
def noise (x, y = 0, z = 0)
|
1139
|
+
Rays.perlin(x, y, z) / 2.0 + 0.5
|
1038
1140
|
end
|
1039
1141
|
|
1040
1142
|
# Loads image.
|
1041
1143
|
#
|
1042
|
-
# @param filename
|
1144
|
+
# @param filename [String] file name to load image
|
1145
|
+
# @param extension [String] type of image to load (ex. 'png')
|
1146
|
+
#
|
1147
|
+
# @return [Image] loaded image object
|
1043
1148
|
#
|
1044
|
-
def loadImage (filename)
|
1149
|
+
def loadImage (filename, extension = nil)
|
1150
|
+
filename = getImage__ filename, extension if filename =~ %r|^https?://|
|
1045
1151
|
Image.new Rays::Image.load filename
|
1046
1152
|
end
|
1047
1153
|
|
1154
|
+
# @private
|
1155
|
+
private def getImage__ (uri, ext)
|
1156
|
+
ext ||= File.extname uri
|
1157
|
+
raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
|
1158
|
+
|
1159
|
+
tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
|
1160
|
+
path = tmpdir + Digest::SHA1.hexdigest(uri)
|
1161
|
+
path = path.sub_ext ext
|
1162
|
+
|
1163
|
+
unless path.file?
|
1164
|
+
URI.open uri do |input|
|
1165
|
+
tmpdir.mkdir unless tmpdir.directory?
|
1166
|
+
path.open('w') do |output|
|
1167
|
+
while buf = input.read(2 ** 16)
|
1168
|
+
output.write buf
|
1169
|
+
end
|
1170
|
+
end
|
1171
|
+
end
|
1172
|
+
end
|
1173
|
+
path.to_s
|
1174
|
+
end
|
1175
|
+
|
1048
1176
|
end# Processing
|
1049
1177
|
|
1050
1178
|
|
@@ -1052,8 +1180,7 @@ module RubySketch
|
|
1052
1180
|
#
|
1053
1181
|
class Processing::Image
|
1054
1182
|
|
1055
|
-
#
|
1056
|
-
#
|
1183
|
+
# @private
|
1057
1184
|
def initialize (image)
|
1058
1185
|
@image = image
|
1059
1186
|
end
|
@@ -1074,6 +1201,44 @@ module RubySketch
|
|
1074
1201
|
@image.height
|
1075
1202
|
end
|
1076
1203
|
|
1204
|
+
# Resizes image.
|
1205
|
+
#
|
1206
|
+
# @param width [Numeric] width for resized image
|
1207
|
+
# @param height [Numeric] height for resized image
|
1208
|
+
#
|
1209
|
+
# @return [nil] nil
|
1210
|
+
#
|
1211
|
+
def resize (width, height)
|
1212
|
+
@image = Rays::Image.new(width, height).paint do |painter|
|
1213
|
+
painter.image @image, 0, 0, width, height
|
1214
|
+
end
|
1215
|
+
nil
|
1216
|
+
end
|
1217
|
+
|
1218
|
+
# Copies image.
|
1219
|
+
#
|
1220
|
+
# @overload copy(sx, sy, sw, sh, dx, dy, dw, dh)
|
1221
|
+
# @overload copy(img, sx, sy, sw, sh, dx, dy, dw, dh)
|
1222
|
+
#
|
1223
|
+
# @param img [Image] image for copy source
|
1224
|
+
# @param sx [Numrtic] x position of source region
|
1225
|
+
# @param sy [Numrtic] y position of source region
|
1226
|
+
# @param sw [Numrtic] width of source region
|
1227
|
+
# @param sh [Numrtic] height of source region
|
1228
|
+
# @param dx [Numrtic] x position of destination region
|
1229
|
+
# @param dy [Numrtic] y position of destination region
|
1230
|
+
# @param dw [Numrtic] width of destination region
|
1231
|
+
# @param dh [Numrtic] height of destination region
|
1232
|
+
#
|
1233
|
+
# @return [nil] nil
|
1234
|
+
#
|
1235
|
+
def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
|
1236
|
+
img ||= self
|
1237
|
+
@image.paint do |painter|
|
1238
|
+
painter.image img.to_internal__, sx, sy, sw, sh, dx, dy, dw, dh
|
1239
|
+
end
|
1240
|
+
end
|
1241
|
+
|
1077
1242
|
# Saves image to file.
|
1078
1243
|
#
|
1079
1244
|
# @param filename [String] file name to save image
|
@@ -1083,7 +1248,7 @@ module RubySketch
|
|
1083
1248
|
end
|
1084
1249
|
|
1085
1250
|
# @private
|
1086
|
-
def
|
1251
|
+
def to_internal__ ()
|
1087
1252
|
@image
|
1088
1253
|
end
|
1089
1254
|
|
@@ -1094,10 +1259,7 @@ module RubySketch
|
|
1094
1259
|
#
|
1095
1260
|
class Processing::Font
|
1096
1261
|
|
1097
|
-
# Initialize font.
|
1098
|
-
#
|
1099
1262
|
# @private
|
1100
|
-
#
|
1101
1263
|
def initialize (font)
|
1102
1264
|
@font = font
|
1103
1265
|
end
|
@@ -1143,13 +1305,7 @@ module RubySketch
|
|
1143
1305
|
#
|
1144
1306
|
attr_reader :h
|
1145
1307
|
|
1146
|
-
#
|
1147
|
-
#
|
1148
|
-
# @param x [Numeric] horizontal position
|
1149
|
-
# @param y [Numeric] vertical position
|
1150
|
-
# @param w [Numeric] width of bounding box
|
1151
|
-
# @param h [Numeric] height of bounding box
|
1152
|
-
#
|
1308
|
+
# @private
|
1153
1309
|
def initialize (x, y, w, h)
|
1154
1310
|
@x, @y, @w, @h = x, y, w, h
|
1155
1311
|
end
|
data/lib/rubysketch/window.rb
CHANGED
@@ -17,9 +17,7 @@ module RubySketch
|
|
17
17
|
|
18
18
|
reset_canvas 1, 1
|
19
19
|
|
20
|
-
super *args, size: [width, height]
|
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 |
|
47
|
-
call_block @draw, e
|
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::
|
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.
|
4
|
+
version: 0.2.6
|
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
|
+
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
|
@@ -111,14 +112,12 @@ files:
|
|
111
112
|
- examples/glsl.rb
|
112
113
|
- examples/hello.rb
|
113
114
|
- examples/image.rb
|
114
|
-
- examples/rubysketch.png
|
115
115
|
- examples/shapes.rb
|
116
116
|
- lib/rubysketch-processing.rb
|
117
117
|
- lib/rubysketch.rb
|
118
118
|
- lib/rubysketch/glsl.rb
|
119
119
|
- lib/rubysketch/module.rb
|
120
120
|
- lib/rubysketch/processing.rb
|
121
|
-
- lib/rubysketch/starter.rb
|
122
121
|
- lib/rubysketch/window.rb
|
123
122
|
- rubysketch.gemspec
|
124
123
|
homepage: https://github.com/xord/rubysketch
|
data/examples/rubysketch.png
DELETED
Binary file
|
data/lib/rubysketch/starter.rb
DELETED
@@ -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
|