p5rb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/p5rb.rb +121 -54
  3. data/p5rb.gemspec +1 -1
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2aeb9b49c2eeadc91b80d5c207e65e71d510ec41
4
- data.tar.gz: cb3054c7c311fa1862f9395b5ff1762e055421e3
2
+ SHA256:
3
+ metadata.gz: f5142534f423ae6b2e335259f2b2e949d0c67923caed5257b18536467cd071dc
4
+ data.tar.gz: 2d0fdb1a392ba9601bc82c0ca5ac5f281ea8b09dadf27f8a44800af2c0bf5683
5
5
  SHA512:
6
- metadata.gz: '074992392feb37241a790014a43b600f30f513c4307fcfddd24c0db3820eb7d69c97088fd9e44ec65ca18315133b076897ceae2487af321e38f8e2417bbff3cb'
7
- data.tar.gz: 9f1a3b74222b8d23a38cedaae63ad2d078f1e402039cbe5f767d368081a386d5dbbbeeb1a28b0db65041b5ccec5d1b2aa6b68ecfe087be9e4c41c35ec788df09
6
+ metadata.gz: 7e1cf13da82440fc26ef0cf0ac63a022e58c3b9543b78256c04f984e3e5aa3ba3abe37b3a36ed941eb3ab9791dcbfbbe8a4a24a47d2a8ef7044f8d64ed8fd8d7
7
+ data.tar.gz: d1b0f0a06fa8a585f5534ff1d1e8d9071fa06543bebc2bc141fa4b54f8887f199646f1f3cfc727267e4caaad87c12d8977379040309fbe3ac7a9f78655035038
data/lib/p5rb.rb CHANGED
@@ -3,68 +3,135 @@ module P5
3
3
  attr_reader :buffer_setup
4
4
  attr_reader :buffer_draw
5
5
  end
6
- instance_variable_set :@buffer_setup, []
7
- instance_variable_set :@buffer_draw, []
6
+ @buffer_setup = []
7
+ @buffer_draw = []
8
8
 
9
- @buffer = []
10
- class << self
11
- def background color
12
- @buffer.push "background(#{color})"
13
- end
14
- def noStroke
15
- @buffer.push "noStroke()"
16
- end
17
- def stroke color
18
- @buffer.push "stroke(#{color})"
19
- end
20
- def translate x, y
21
- @buffer.push "translate(#{x}, #{y})"
22
- end
23
- def circle x, y, r
24
- @buffer.push "circle(#{x}, #{y}, #{r})"
25
- end
26
- def ellipse *args
27
- @buffer.push "ellipse(#{args.join ?,})"
28
- end
29
- def fill color, alpha = nil
30
- @buffer.push "fill(#{color}#{", #{alpha}" if alpha})"
31
- end
32
- def rect x, y, w, h, fill: nil
33
- (@buffer.push "push()"; fill fill) if fill
34
- @buffer.push "rect(#{x}, #{y}, #{w}, #{h})"
35
- @buffer.push "pop()" if fill
36
- end
37
- def point x, y
38
- @buffer.push "point(#{x}, #{y})"
39
- end
40
- def textSize size
41
- @buffer.push "textSize(#{size})"
42
- end
43
- def textAlign *args
44
- @buffer.push "textAlign(#{args.join ", "})"
45
- end
46
- def text text, x, y, fill: nil
47
- (@buffer.push "push()"; fill fill) if fill
48
- @buffer.push "text(#{text.inspect}, #{x}, #{y})"
49
- @buffer.push "pop()" if fill
9
+ module Block
10
+ class << self
11
+ attr_writer :buffer
12
+ def raw _
13
+ @buffer.push _
14
+ end
15
+ def background color
16
+ @buffer.push "background(#{color})"
17
+ end
18
+ def noStroke
19
+ @buffer.push "noStroke()"
20
+ end
21
+ def stroke color
22
+ @buffer.push "stroke(#{color})"
23
+ end
24
+ def translate x, y
25
+ @buffer.push "translate(#{x}, #{y})"
26
+ end
27
+ def line x1, y1, x2, y2
28
+ @buffer.push "line(#{x1}, #{y1}, #{x2}, #{y2})"
29
+ end
30
+ def circle x, y, r
31
+ @buffer.push "circle(#{x}, #{y}, #{r})"
32
+ end
33
+ def ellipse *args # not tested yet
34
+ @buffer.push "ellipse(#{args.join ?,})"
35
+ end
36
+ def fill color, alpha = nil
37
+ @buffer.push "fill(#{color.inspect}#{", #{alpha}" if alpha})"
38
+ end
39
+ def rect x, y, w, h, fill: nil
40
+ (@buffer.push "push()"; fill fill) if fill
41
+ @buffer.push "rect(#{x}, #{y}, #{w}, #{h})"
42
+ @buffer.push "pop()" if fill
43
+ end
44
+ def point x, y
45
+ @buffer.push "point(#{x}, #{y})"
46
+ end
47
+ def textSize size
48
+ @buffer.push "textSize(#{size})"
49
+ end
50
+ def textAlign *args
51
+ @buffer.push "textAlign(#{args.join ", "})"
52
+ end
53
+ def text text, x, y, fill: nil
54
+ (@buffer.push "push()"; fill fill) if fill
55
+ @buffer.push "text(#{text.inspect}, #{x}, #{y})"
56
+ @buffer.push "pop()" if fill
57
+ end
58
+ def map *args
59
+ "map(#{args.join ", "})"
60
+ end
50
61
  end
62
+ end
51
63
 
64
+ class << self
52
65
  def setup &block
53
- module_eval &block
54
- @buffer_setup = @buffer
55
- @buffer = []
66
+ ::P5::Block.buffer = @buffer_setup
67
+ ::P5::Block.module_eval &block
56
68
  end
57
- def draw &block
58
- module_eval &block
59
- @buffer_draw = @buffer
60
- @buffer = []
69
+ def draw &block # not tested yet
70
+ ::P5::Block.buffer = @buffer_draw
71
+ ::P5::Block.module_eval &block
61
72
  end
73
+ end
62
74
 
75
+ class << self
76
+ def plot_scatter data, reverse_y: false
77
+ size = 1000
78
+ max = nil
79
+ (x_range, x_from, x_to, x_enum, x_f),
80
+ (y_range, y_from, y_to, y_enum, y_f) = data.transpose.map do |axis|
81
+ min, max = axis.minmax
82
+ range = (min - max).abs
83
+ division = 10**Math::log10(range).floor
84
+ from = min.div(division)*division
85
+ to = -(-max).div(division)*division
86
+ [
87
+ to - from,
88
+ from, to,
89
+ from.step(to, division),
90
+ ]
91
+ end
92
+ max = [x_range, y_range].max
93
+ x_f = ->_{ 20 + (size-40.0) * (_ - x_from) / max }
94
+ y_f = ->_{ 20 + (size-40.0) * (reverse_y ? y_to - _ : _ - y_from) / max }
95
+ P5 40 + (size-40.0) * x_range / max + 50, # TODO: properly fix the issue that with wide labels the right end of the plot may be cut off
96
+ 40 + (size-40.0) * y_range / max do
97
+ setup do
98
+ textSize 15
99
+ raw "const w = max([#{y_enum.map{ |_| "textWidth(#{_})" }.join ?,}])"
100
+ raw "translate(w, 15)"
101
+ stroke 200
102
+ textAlign :CENTER, :BOTTOM; x_enum.each{ |_| line x_f[_], y_f[y_from], x_f[_], y_f[y_to]; text _, x_f[_], 20-5 }
103
+ textAlign :RIGHT, :CENTER; y_enum.each{ |_| line x_f[x_from], y_f[_], x_f[x_to], y_f[_]; text _, x_f[x_from]-5, y_f[_] }
104
+ stroke 0
105
+ data.each{ |x,y| point x_f[x], y_f[y] }
106
+ end
107
+ end
108
+ end
109
+ def plot_bar data
110
+ cls = data.values.flat_map(&:keys).uniq.sort
111
+ size = cls.size + 1
112
+ from, to = data.keys.minmax
113
+ max = data.values.flat_map(&:values).max
114
+ P5 500, 500 do
115
+ setup do
116
+ noStroke
117
+ textAlign :CENTER
118
+ cls.each_with_index{ |_, i| text _, 50+400/(size-2)*i, 25, fill: %w{ red green blue }[i] }
119
+ textAlign :RIGHT, :TOP
120
+ (from..to).each do |date|
121
+ y = map date.ajd.to_i, from.ajd.to_i, to.ajd.to_i, 50, 450
122
+ text date.strftime("%m-%d"), 45, y
123
+ rect 50, y, map(data.fetch(date,{})[cls[0]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "red"
124
+ rect 50, "#{y}+#{400/(to-from)/size}", map(data.fetch(date,{})[cls[1]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "green"
125
+ rect 50, "#{y}+#{800/(to-from)/size}", map(data.fetch(date,{})[cls[2]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "blue"
126
+ end
127
+ end
128
+ end
129
+ end
63
130
  end
64
131
  end
65
132
 
66
133
  def P5 width, height, &block
67
- P5.module_eval &block
134
+ ::P5.module_eval &block
68
135
  <<~HEREDOC
69
136
  <html>
70
137
  <head>
@@ -73,17 +140,17 @@ def P5 width, height, &block
73
140
  function setup() {
74
141
  createCanvas(#{width}, #{height});
75
142
  #{
76
- P5.buffer_setup.join(";\n").gsub(/^/, ?\s*8)
143
+ ::P5.buffer_setup.join(";\n").gsub(/^/, ?\s*12)
77
144
  }
78
145
  }
79
146
  function draw() {
80
147
  #{
81
- P5.buffer_draw.join(";\n").gsub(/^/, ?\s*8)
148
+ ::P5.buffer_draw.join(";\n").gsub(/^/, ?\s*12)
82
149
  }
83
150
  }
84
151
  </script>
85
152
  </head>
86
- <body><main></main></body>
153
+ <body style="margin: 0"><main></main></body>
87
154
  </html>
88
155
  HEREDOC
89
156
  end
data/p5rb.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "p5rb"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.0.2"
4
4
  spec.summary = "Ruby DSL for p5.js"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p5rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-10 00:00:00.000000000 Z
11
+ date: 2022-12-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: nakilon@gmail.com
@@ -39,8 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.5.2.3
42
+ rubygems_version: 3.3.25
44
43
  signing_key:
45
44
  specification_version: 4
46
45
  summary: Ruby DSL for p5.js