require 'open-uri' require 'ferrum' RUBY_URL = 'https://cdn.jsdelivr.net/npm/ruby-3_2-wasm-wasi@next/dist/browser.script.iife.js' P5JS_URL = 'https://cdn.jsdelivr.net/npm/p5@1.5.0/lib/p5.js' P5RB_URL = 'https://raw.githubusercontent.com/ongaeshi/p5rb/master/docs/lib/p5.rb' P5RB_SRC = URI.open(P5RB_URL) {|f| f.read} def browser(width, height, headless: true) hash = ($browsers ||= {}) key = [width, height, headless] hash[key] ||= Ferrum::Browser.new headless: headless, window_size: [width, height] end def get_svg_html(width, height, svg_xml) <<~END #{svg_xml} END end def get_p5rb_html(width, height, draw_src, webgl: false) <<~END
END end def sleep_until(try: 3, timeout: 10, &block) now = -> offset = 0 {Time.now.to_f + offset} limit = now[timeout] until block.call if now[] > limit limit = now[timeout] try -= 1 next if try > 0 raise 'Drawing timed out in browser' end sleep 0.1 end end def draw_on_browser(width, height, path, html, headless: true) b = browser width, height, headless: headless unless File.exist? path b.reset b.main_frame.content = html sleep_until do b.evaluate 'document.querySelector("#completed") != null' end b.screenshot path: path end b.device_pixel_ratio end def draw_svg(width, height, svg_xml, path, headless: true) html = get_svg_html width, height, svg_xml draw_on_browser width, height, path, html, headless: headless end def draw_p5rb(width, height, draw_src, path, headless: true, webgl: false) html = get_p5rb_html width, height, draw_src, webgl: webgl draw_on_browser width, height, path, html, headless: headless end