https://t.co/HGDzVwRjuC これがmrubyでC++例外を使うパッチですが、Emscriptenの進化により要らない子になり、一年後くらいにC++erに発掘された
— take-cheeze (@take_cheeze) 2014, 2月 24
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
- mrbgem.rake
emscriptenのサイトで手順に従いインストールを行う。 その後、emscriptenを動かせば、.emscriptenが作成されているので、必要な項目は設定しておく。
- LLVM_ROOT
- NODE_JS
以上の設定を事前にしておけば、gem一発で入れたあとすぐに使える
gem install wewbruby
which webrubyしても出てこなかったので、以下を実施
rbenv rehash
webruby new MyFistProject
cd MyFirstProject
プロジェクトフォルダ直下のRakefileの編集して mruby-jsを有効にする。
具体的には以下の様のコメントアウトされている行を 有効にする。
# conf.gem :git => 'git://github.com/xxuejie/mruby-js.git', :branch => 'master'
conf.gem :git => 'git://github.com/xxuejie/mruby-js.git', :branch => 'master'
# This is the entrypoint file for webruby
root_object = MrubyJs::get_root_object
# get jQuery selected object
container = root_object.call("$", "#container")
# appends <p> tag
5.times { container.call("append", "<p>This is inserted in Webruby using WEBRUBY#run()!</p>") }
rake
で、buildフォルダ配下にいろいろできる、webruby.js出来るはず。
<!DOCTYPE html>
<meta charset="UTF-8">
<title>test</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="webruby.js"></script>
<h1>This is a test!</h1>
<div id="container"></div>
<script>
$(function() {
var w = WEBRUBY();
/* Runs embedded source code in mruby.js file. */
w.run();
w.close();
});
</script>
それなりに開発すると.rbファイルをajaxで読む事になりそうなので、 ローカルでもサーバを立てて動かした方が良い気がする。
python -m SimpleHTTPServer
LLVM_ROOT = os.path.expanduser(os.getenv('LLVM') or '~/local/llvm/clang+llvm-3.2-x86-linux-ub
untu-12.04/bin')
/usr/localとかメジャーな場所が必要か? emscriptenは同梱されていた。
設定は~/配下の.emscriptenを参照している NODEに関してもNODE環境変数でnvmな環境でも対応できた。
nvm use 0.10
export NODE=`which node`
RubyからDomをいじる感じ。なので、Three.jsも普通に使える AjaxでロードするつくりにしておいてTry&Errorでやっていく以外は難しそう、ネイティブRubyで動かせない気がする。
$window = MrubyJs::get_root_object
$document = $window.document
$three = $window.THREE
$container = $stats = nil
$camera = $scene = $renderer = nil
$renderer = $three.WebGLRenderer.invoke_new({antialias: true})
$renderer.setSize($window.innerWidth, $window.innerHeight)
$camera = $three.PerspectiveCamera.invoke_new(
45, $window.innerWidth / $window.innerHeight, 1, 2000)
$camera.position.y = 400
$scene = $three.Scene.invoke_new
$container = $document.createElement('div')
$document.body.appendChild($container)
$container.appendChild($renderer.domElement)
def do_render
$renderer.render($scene, $camera)
end
def animate
$window.requestAnimationFrame(:animate.to_js_proc($window, 0))
do_render
$stats.update[]
end