class Kame::Remocon::Opal::Turtle
Attributes
code[R]
default_color[RW]
Public Class Methods
new(canvas, wait: 0, default_color: "white")
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 27 def initialize(canvas, wait: 0, default_color: "white") @canvas = canvas @wait = wait @default_color = default_color @context = @canvas.get_context("2d"); clear reset @kame = Kame::Remocon::Opal::Image.new @kame.src = "/assets/images/kame.png" end
Public Instance Methods
clear()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 137 def clear @context.clear_rect(0, 0, @canvas.width, @canvas.height) @path = Kame::Remocon::Opal::Canvas::Path2D.new @paths = [[@path, @default_color]] nil end
color(color)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 173 def color(color) @path = Kame::Remocon::Opal::Canvas::Path2D.new @path.move_to(*@pos.canvas_coordinate) @paths << [@path, color] nil end
draw_kame()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 67 def draw_kame (x, y) = @pos.canvas_coordinate c = Math.cos(@direction / 180 * Math::PI) s = Math.sin(@direction / 180 * Math::PI) transform = [c, s, -s, c, x - 10 * c + 10 * s, y - 10 * c - 10 * s] @context.set_transform(*transform) @context.draw_image(@kame, 0, 0, 20, 20) @context.set_transform(1, 0, 0, 1, 0, 0) end
exec(code, wait=0, &callback)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 78 def exec(code, wait=0, &callback) @code = code clear reset commander = Commander.new commander.instance_eval code exec_commands(commander.commands, wait: wait, &callback) end
exec_command(command)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 132 def exec_command(command) self.method(command.first).call(*command[1..-1]) nil end
exec_commands(commands, wait: nil, &callback)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 87 def exec_commands(commands, wait: nil, &callback) callback ||= Proc.new {} wait ||= @wait if wait > 0 exec = Proc.new do if commands.length == 0 false else exec_command(commands.shift) @context.clear_rect(0, 0, @canvas.width, @canvas.height) @paths.each do |path, color| @context.stroke_style = color @context.stroke(path) end draw_kame true end end interval = wait * 1000 %x( var timer = setInterval(function() { if (!exec()) { callback.$call(); clearInterval(timer); } }, interval); ) else commands.each(&self.method(:exec_command)) @paths.each do |path, color| @context.stroke_style = color @context.stroke(path) end if @kame.complete draw_kame callback.call else @kame.onload do draw_kame callback.call end end end end
forward(dist)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 180 def forward(dist) @pos = position_to(dist) move nil end
image_data()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 63 def image_data @canvas.image_data end
move()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 192 def move if @pen_down @path.line_to(*@pos.canvas_coordinate) else @path.move_to(*@pos.canvas_coordinate) end end
move_to(x, y)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 186 def move_to(x, y) @pos = Pos.new(x, y) move nil end
new_commander()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 39 def new_commander forwarder = Forwarder.new(self, *Commander::METHODS) do @context.clear_rect(0, 0, @canvas.width, @canvas.height) @paths.each do |path, color| @context.stroke_style = color @context.stroke(path) end draw_kame end DRb::DRbObject.new(Commander.new(forwarder)) end
pen_down()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 163 def pen_down @pen_down = true nil end
pen_up()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 168 def pen_up @pen_down = false nil end
position_to(dist)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 200 def position_to(dist) dx = Math.sin(Math::PI * @direction / 180) * dist dy = - Math.cos(Math::PI * @direction / 180) * dist Pos.new(@pos.x + dx, @pos.y + dy) end
reset()
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 144 def reset @pen_down = false @direction = 0 @pos = Pos.new(0, 0) @path.move_to(*@pos.canvas_coordinate) @context.move_to(*@pos.canvas_coordinate) nil end
turn_left(digree)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 153 def turn_left(digree) @direction = (@direction - digree) % 360 nil end
turn_right(digree)
click to toggle source
# File lib/kame/remocon/opal/turtle.rb, line 158 def turn_right(digree) @direction = (@direction + digree) % 360 nil end