Newer
Older
MyGames / matrix.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>rbCanvas</title>
<!-- rbcCanvas editor v0.8.2 -->
</head>
<body>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/opal/opal_patched.min.js" onload="Opal.load('opal')"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/opal/opal-parser_patched.min.js" onload="Opal.load('opal-parser'); Opal.load('parser/ruby23');"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/opal/native.min.js" onload="Opal.load('native')"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/p5/p5.min.js"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/rbcanvas/monkeypatch_p5.js"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/rbcanvas/rbcanvas_transpiled_helper.js"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/rbcanvas/rbcanvas_transpiled.js"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/rbcanvas/microbit_bleuart.js"></script>
<script type="text/javascript" src="https://rbcanvas.net/0.8.2/runtime/rbcanvas/microbit_bleuart_rb.js"></script>
<script type="text/ruby" section="s00" name="main">
def setup
  puts RBCanvas::CODE_USER
  eval(RBCanvas::CODE_CONSTANT)
  eval(RBCanvas::CODE_USER)
end
module RBCanvas
  CODE_CONSTANT = <<-'EOC_CONSTANT'
    %x{
      cv = document.getElementById("defaultCanvas0");
      if (cv) {
        cv.remove();  // remove default Canvas (100 x 100)
      }
      // document.body.innerHTML = ""
    }
  EOC_CONSTANT
  CODE_USER = <<-'EOC_USER'
include DX
include Console
$PROGRAM_NAME = "main"
Window.width   = 2000
Window.height  = 1600
Window.fps     = 15
Window.bgcolor = C_BLACK

UI.label(Window.width+60, 10, 400, 30, <<EOS)
Click to start/stop.
Change the slider value to set the generation frequency.
EOS

FONTSIZE = 50
WIDTH = Window.width / (FONTSIZE/2)
HEIGHT = Window.height / FONTSIZE
FONT = Font.new(FONTSIZE, 'monospace')
ALPHA_DECAY = 20
FREQ_INIT = 2  # initial generation frequency

poses = []
pos_01 = {}
draw = false
$freq = FREQ_INIT  # generation frequency

LABEL_FREQ = UI.label(Window.width+20, 10, 100, 20, "#{FREQ_INIT}%", fontsize: 20)
SLIDER_FREQ = UI.slider(Window.width+20, 330, 300, [0, 5, FREQ_INIT, 1], angle: -90)

Window.loop do
  $freq = SLIDER_FREQ.value
  LABEL_FREQ.value = "#{$freq}%"
  
  if Input.mouse_push?(M_LBUTTON)
    draw = draw ? false : true
  end
  
  poses.unshift( draw ? Array.new( (WIDTH*$freq/100.0).ceil ) {rand(WIDTH)} : [] )
  
  poses.each.with_index(1) do |x_poses, y|
    x_poses.each do |x|
      255.step(0, -ALPHA_DECAY).with_index do |alpha, i|
        pos_01[[x, y-i]] = pos_01.fetch([x, y-i], rand(2))
        Window.draw_font(x*FONTSIZE/2, (y-i)*FONTSIZE, pos_01[[x, y-i]].to_s, FONT, color: [alpha, 0, 255, 0], bgcolor: C_BLACK)
      end
    end
  end
  
  if poses.all?(&:empty?)
    poses.clear
  end
  
  if poses.length > HEIGHT + (255.0 / ALPHA_DECAY).ceil
    poses.pop
  end
end

$PROGRAM_NAME = "main"
  EOC_USER
end
</script>

</body>
</html>