p5rb 0.0.0 → 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 -48
  3. data/p5rb.gemspec +1 -1
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b004484546d4bd943e52db2b0e8863d3e2a7e2ff
4
- data.tar.gz: 242fcef182c7e183d1a10865d77cb7e9c1f1520b
2
+ SHA256:
3
+ metadata.gz: f5142534f423ae6b2e335259f2b2e949d0c67923caed5257b18536467cd071dc
4
+ data.tar.gz: 2d0fdb1a392ba9601bc82c0ca5ac5f281ea8b09dadf27f8a44800af2c0bf5683
5
5
  SHA512:
6
- metadata.gz: f1ea54e72cee551327a31ee3a186c7bb70ba1851e9d0eba3dffa246e019127ac4e61ede192a12a6e2195c20d8465d62ab9f4d031387a702bcc21ae695ff0302b
7
- data.tar.gz: 0a6a027296c6e7381f3a1e470f530d7c9f724059ab4818c3cf972cd0365362d0cc6f83854652416f80a54a6feab21349f3e4104e6fa3908b891eecb22ba588b3
6
+ metadata.gz: 7e1cf13da82440fc26ef0cf0ac63a022e58c3b9543b78256c04f984e3e5aa3ba3abe37b3a36ed941eb3ab9791dcbfbbe8a4a24a47d2a8ef7044f8d64ed8fd8d7
7
+ data.tar.gz: d1b0f0a06fa8a585f5534ff1d1e8d9071fa06543bebc2bc141fa4b54f8887f199646f1f3cfc727267e4caaad87c12d8977379040309fbe3ac7a9f78655035038
data/lib/p5rb.rb CHANGED
@@ -3,62 +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 c
12
- @buffer.push "background(#{c})"
13
- end
14
- def translate x, y
15
- @buffer.push "translate(#{x}, #{y})"
16
- end
17
- def circle x, y, r
18
- @buffer.push "circle(#{x}, #{y}, #{r})"
19
- end
20
- def ellipse *args
21
- @buffer.push "ellipse(#{args.join ?,})"
22
- end
23
- def noStroke
24
- @buffer.push "noStroke()"
25
- end
26
- def fill color, alpha = nil
27
- @buffer.push "fill(#{color}#{", #{alpha}" if alpha})"
28
- end
29
- def rect x, y, w, h, fill: nil
30
- (@buffer.push "push()"; fill fill) if fill
31
- @buffer.push "rect(#{x}, #{y}, #{w}, #{h})"
32
- @buffer.push "pop()" if fill
33
- end
34
- def textSize size
35
- @buffer.push "textSize(#{size})"
36
- end
37
- def textAlign *args
38
- @buffer.push "textAlign(#{args.join ", "})"
39
- end
40
- def text text, x, y, fill: nil
41
- (@buffer.push "push()"; fill fill) if fill
42
- @buffer.push "text(#{text.inspect}, #{x}, #{y})"
43
- @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
44
61
  end
62
+ end
45
63
 
64
+ class << self
46
65
  def setup &block
47
- module_eval &block
48
- @buffer_setup = @buffer
49
- @buffer = []
66
+ ::P5::Block.buffer = @buffer_setup
67
+ ::P5::Block.module_eval &block
50
68
  end
51
- def draw &block
52
- module_eval &block
53
- @buffer_draw = @buffer
54
- @buffer = []
69
+ def draw &block # not tested yet
70
+ ::P5::Block.buffer = @buffer_draw
71
+ ::P5::Block.module_eval &block
55
72
  end
73
+ end
56
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
57
130
  end
58
131
  end
59
132
 
60
133
  def P5 width, height, &block
61
- P5.module_eval &block
134
+ ::P5.module_eval &block
62
135
  <<~HEREDOC
63
136
  <html>
64
137
  <head>
@@ -67,17 +140,17 @@ def P5 width, height, &block
67
140
  function setup() {
68
141
  createCanvas(#{width}, #{height});
69
142
  #{
70
- P5.buffer_setup.join(";\n").gsub(/^/, ?\s*8)
143
+ ::P5.buffer_setup.join(";\n").gsub(/^/, ?\s*12)
71
144
  }
72
145
  }
73
146
  function draw() {
74
147
  #{
75
- P5.buffer_draw.join(";\n").gsub(/^/, ?\s*8)
148
+ ::P5.buffer_draw.join(";\n").gsub(/^/, ?\s*12)
76
149
  }
77
150
  }
78
151
  </script>
79
152
  </head>
80
- <body><main></main></body>
153
+ <body style="margin: 0"><main></main></body>
81
154
  </html>
82
155
  HEREDOC
83
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.0"
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.0
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-08-06 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