kame-remocon 0.1.3 → 0.1.4

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: 99d6b147fd3d4c3a15470c2bd251fc24b997fcbd015a5e7bec612fef219cb3ee
4
- data.tar.gz: 9903122a68c67ee226f21dc6a51328f38c1d578145378a93c9f30e8505d78369
3
+ metadata.gz: 0b7b14707f0668e82aee7190351a5e9583489061aa6a77bb3f9c5a1912780899
4
+ data.tar.gz: e741532064feeaa3f1b3c5136d346534d372bef4c5b324f60603228a1d820a11
5
5
  SHA512:
6
- metadata.gz: fe52be152807635bfbb1ec58e8f90c8cac7b9086c0c885ebec051f49a2f8483e3e4ee3c89ef04fb5e68203519c24a21e02e759bee9580152f7d462bb16f65dde
7
- data.tar.gz: ea6301a1a72b06254b494b8adfdf73a1d08eb4469c83b324a67e6872ef59edb72f69ac7a537e54fc7cfbaccfe31f5b5846520a9112745cbcf28530108a885a25
6
+ metadata.gz: 33dd39acb5d7cc0c555d581365f46437472f2564d7e7466c6eff7cda9d63e3609331e932452016aac61f9b2456e0287344e8394d1aa52033e73b75b041d7258a
7
+ data.tar.gz: a1f600cd7d4b25b2e9e22b52a09613addcd50ba34a91ab2cfc4562a6eadc8b43599e93e9d2ac22ba9cd19517a1c6b7208f4496ba08c76a2e39397c28f3e538e7
@@ -1,7 +1,7 @@
1
1
  class Commander
2
2
  attr_reader :commands
3
3
 
4
- METHODS = [:clear, :reset, :pen_up, :pen_down, :turn_left, :turn_right, :forward, :backward, :move_to]
4
+ METHODS = [:clear, :reset, :pen_up, :pen_down, :color, :turn_left, :turn_right, :forward, :backward, :move_to]
5
5
 
6
6
  def initialize(forward = nil)
7
7
  if forward
@@ -43,6 +43,10 @@ class Commander
43
43
  self << [:pen_up]
44
44
  end
45
45
 
46
+ def color(color)
47
+ self << [:color, color]
48
+ end
49
+
46
50
  def forward(dist)
47
51
  self << [:forward, dist]
48
52
  end
@@ -26,6 +26,9 @@ end
26
26
  class AppView
27
27
  include Hyalite::Component
28
28
 
29
+ state :render_image, false
30
+ state :bg_color, "black"
31
+
29
32
  def initialize
30
33
  @program = <<~PROG
31
34
  pen_down
@@ -56,15 +59,30 @@ class AppView
56
59
  @turtle.exec @program, wait
57
60
  end
58
61
 
62
+ def create_image
63
+ set_state(render_image: true)
64
+ end
65
+
66
+ def set_background
67
+ bg_color = @refs[:bg_color][:checked]
68
+ @turtle.default_color = bg_color ? "white" : "black"
69
+ set_state(bg_color: bg_color ? "black" : "white")
70
+ end
71
+
59
72
  def render
60
73
  program = @program
74
+ render_image = @state[:render_image]
75
+ bg_color = @state[:bg_color]
61
76
  div do
62
77
  h2(nil, 'タートルグラフィックスに挑戦!!')
63
- CanvasView.el(onMounted: -> canvas { mounted(canvas) })
78
+ CanvasView.el(onMounted: -> canvas { mounted(canvas) }, render_image: render_image, bg_color: bg_color)
64
79
  textarea({style: {width: "400px", height: "200px"}, ref: :program}, program)
65
80
  input({type: :checkbox, checked: true, id: :wait, ref: :wait})
66
81
  label({for: :wait}, "描く過程を表示する")
67
- button({onClick: -> { exec() }, name: "exec"}, "Exec")
82
+ input({type: :checkbox, checked: true, id: :bg_color, ref: :bg_color, onClick: -> {set_background}})
83
+ label({for: :bg_color}, "黒背景")
84
+ button({onClick: -> { exec }, name: "exec"}, "実行する")
85
+ button({onClick: -> { create_image }, name: "create_image"}, "画像を作成")
68
86
  end
69
87
  end
70
88
  end
@@ -3,6 +3,8 @@ require "kame/remocon/commander"
3
3
  require_relative "./image"
4
4
 
5
5
  class Turtle
6
+ attr_accessor :default_color
7
+
6
8
  class Pos
7
9
  attr_reader :x, :y
8
10
 
@@ -21,9 +23,10 @@ class Turtle
21
23
  end
22
24
  end
23
25
 
24
- def initialize(canvas, wait: 0)
26
+ def initialize(canvas, wait: 0, default_color: "white")
25
27
  @canvas = canvas
26
28
  @wait = wait
29
+ @default_color = default_color
27
30
  @context = @canvas.get_context("2d");
28
31
  clear
29
32
  reset
@@ -35,7 +38,10 @@ class Turtle
35
38
  def new_commander
36
39
  forwarder = Forwarder.new(self, *Commander::METHODS) do
37
40
  @context.clear_rect(0, 0, @canvas.width, @canvas.height)
38
- @context.stroke(@path)
41
+ @paths.each do |path, color|
42
+ @context.stroke_style = color
43
+ @context.stroke(path)
44
+ end
39
45
  draw_kame
40
46
  end
41
47
 
@@ -58,7 +64,7 @@ class Turtle
58
64
 
59
65
  c = Math.cos(@direction / 180 * Math::PI)
60
66
  s = Math.sin(@direction / 180 * Math::PI)
61
- transform = [c, s, -s, c, x - 10 * c + 10 * s, y - 10 * c - 10 * s]
67
+ transform = [c, s, -s, c, x - 10 * c + 10 * s, y - 10 * c - 10 * s]
62
68
  @context.set_transform(*transform)
63
69
  @context.draw_image(@kame, 0, 0, 20, 20)
64
70
  @context.set_transform(1, 0, 0, 1, 0, 0)
@@ -81,7 +87,10 @@ class Turtle
81
87
  else
82
88
  exec_command(commands.shift)
83
89
  @context.clear_rect(0, 0, @canvas.width, @canvas.height)
84
- @context.stroke(@path)
90
+ @paths.each do |path, color|
91
+ @context.stroke_style = color
92
+ @context.stroke(path)
93
+ end
85
94
  draw_kame
86
95
  true
87
96
  end
@@ -94,7 +103,10 @@ class Turtle
94
103
  )
95
104
  else
96
105
  commands.each(&self.method(:exec_command))
97
- @context.stroke(@path)
106
+ @paths.each do |path, color|
107
+ @context.stroke_style = color
108
+ @context.stroke(path)
109
+ end
98
110
  if @kame.complete
99
111
  draw_kame
100
112
  else
@@ -113,6 +125,7 @@ class Turtle
113
125
  def clear
114
126
  @context.clear_rect(0, 0, @canvas.width, @canvas.height)
115
127
  @path = Canvas::Path2D.new
128
+ @paths = [[@path, @default_color]]
116
129
  nil
117
130
  end
118
131
 
@@ -122,7 +135,6 @@ class Turtle
122
135
  @pos = Pos.new(0, 0)
123
136
  @path.move_to(*@pos.canvas_coordinate)
124
137
  @context.move_to(*@pos.canvas_coordinate)
125
- @context.stroke_style = "white"
126
138
  nil
127
139
  end
128
140
 
@@ -146,6 +158,13 @@ class Turtle
146
158
  nil
147
159
  end
148
160
 
161
+ def color(color)
162
+ @path = Canvas::Path2D.new
163
+ @path.move_to(*@pos.canvas_coordinate)
164
+ @paths << [@path, color]
165
+ nil
166
+ end
167
+
149
168
  def forward(dist)
150
169
  @pos = position_to(dist)
151
170
  move
@@ -1,6 +1,10 @@
1
1
  class CanvasView
2
2
  include Hyalite::Component
3
3
 
4
+ def create_image
5
+ canvas = @refs[:canvas].native
6
+ `canvas.toDataURL()`
7
+ end
4
8
 
5
9
  def component_did_mount
6
10
  el = @refs[:canvas]
@@ -10,6 +14,18 @@ class CanvasView
10
14
  end
11
15
 
12
16
  def render
13
- canvas({width: "400", height: "400", ref: :canvas, style: {"background-color": "black"}})
17
+ image = nil
18
+ if @props[:render_image]
19
+ image = create_image
20
+ end
21
+
22
+ bg_color = @props[:bg_color]
23
+
24
+ div do
25
+ canvas(width: "400", height: "400", id: :canvas, ref: :canvas, style: {"background-color": bg_color})
26
+ if image
27
+ img(src: image, style: {"background-color": bg_color})
28
+ end
29
+ end
14
30
  end
15
31
  end
@@ -33,6 +33,10 @@ class TurtleProxy
33
33
  @turtle.pen_up
34
34
  end
35
35
 
36
+ def color(color)
37
+ @turtle.color(color)
38
+ end
39
+
36
40
  def forward(dist)
37
41
  @turtle.forward(dist)
38
42
  end
@@ -1,5 +1,5 @@
1
1
  module Kame
2
2
  module Remocon
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kame-remocon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-29 00:00:00.000000000 Z
11
+ date: 2019-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -195,7 +195,6 @@ files:
195
195
  - lib/kame/remocon/opal/controllers/application_controller.rb
196
196
  - lib/kame/remocon/opal/image.rb
197
197
  - lib/kame/remocon/opal/turtle.rb
198
- - lib/kame/remocon/opal/views/canvas.rb
199
198
  - lib/kame/remocon/opal/views/canvas_view.rb
200
199
  - lib/kame/remocon/remote_object.rb
201
200
  - lib/kame/remocon/server.rb
@@ -1,7 +0,0 @@
1
- class Canvas
2
- include Hyalite::Component
3
-
4
- def render
5
- canvas({width: 480, height: 480})
6
- end
7
- end