
Ejecta – A Fast, Open Source JavaScript, Canvas and Audio Implementation for iOS - hising
http://impactjs.com/ejecta
======
pcwalton
One of the coolest things I learned from the original blog post [1] on Ejecta
was a trick that the library uses for drawing polygons on the GPU in OpenGL
without triangulating them first [2]. It's one of the coolest graphics
programming tricks I've ever seen and when I understood how it worked my mind
was blown.

[1]:
[http://phoboslab.org/log/2012/09/ejecta](http://phoboslab.org/log/2012/09/ejecta)

[2]:
[http://www.glprogramming.com/red/chapter14.html#name13](http://www.glprogramming.com/red/chapter14.html#name13)

~~~
phoboslab
With a bit more trickery, you can even implement the Non-Zero fill rule
(Canvas2D default), instead of the simpler Even-Odd rule. Ejecta now supports
both:

[https://github.com/phoboslab/Ejecta/blob/master/Source/Eject...](https://github.com/phoboslab/Ejecta/blob/master/Source/Ejecta/EJCanvas/2D/EJPath.mm#L361-L396)

~~~
mattdesl
Could I also use this technique to clip a scene to the path of a complex
polygon?

How does it hold up with many overlapping shapes of varying color; wouldn't it
lead to far more state changes and ultimately much greater fill-rate?

And what are your thoughts on using this for font rendering?

This looks brilliant. Thanks for the info.

------
RyanZAG
Would be nice to see some benchmarks. I can't see how this could be faster
than just running it in Safari. In Safari you get access to far faster
javascript jit than you do in the embedded app js engine. The app itself just
translates the javascript canvas calls into objc calls to the core graphics..
which is what safari's canvas does anyway? Is the extra level of indirection
through safari enough to compensate for slower jit? Plus it's a lot harder to
compile this than it is to put a js game on the web.

Needs benchmarks to be able to make any decisions.

~~~
phoboslab
(Ejecta dev here)

Ejecta uses OpenGL ES 2 for Canvas, not Core Graphics.

The default demo that comes with Ejecta[1] runs with 60fps on the iPhone4S.
Just try it in Mobile Safari. Even Desktop Browsers struggle with this. Of
course it's a contrived benchmark, but it still makes a point, I think :)

There are also a lot of other reason to use Ejecta. If you _want_ to
distribute your App in the AppStore, the alternative is to use a WebView
framework such as PhoneGap, which can't use JIT either. That said, the raw
JavaScript performance is rarely the bottleneck for games - most of the time,
drawing is.

[1] [http://phoboslab.org/crap/bezier/](http://phoboslab.org/crap/bezier/)

~~~
mamcx
Hi, sorry to hijack the thread. What JS chart tool play well with ejecta in
iOS? You know?

------
hopfog
Ejecta is awesome. So is ImpactJS. Dominic (the creator) is experimenting with
making Ejecta cross-platform so hopefully we will see an Android version soon.

------
camus2

        The JavaScriptCore API is private on iOS, 
        which means you're not allowed to link to it. 
        Ejecta therefore comes bundled with its 
        own version of the library to circumvent these 
        restrictions.
    

So you are basically using another VM(a second javascript core) in an IOS app.
Isnt it forbidden to do so,running VMs in IOS apps?

A better approach would be to do like TITANIUM ,except that you expose a
CANVAS like API,so that your javascript canvas framework is compatible with it
=> less work,and no trick that would make your app kicked out of the store
once the scheme is busted.

~~~
Rafert
No, it isn't. It would make the likes of Xamarin and RubyMotion impossible.
You're confused with the fact that App Store policies do not allow downloading
and running new executable code.

~~~
camus2
I thought Xamarin and RM compiled to objc.

~~~
Revex
Yeah, I am pretty sure that Xamarin compiles down to Obj-C on the iPhone, but
on Android it includes a VM in java that runs .net (mono).

~~~
acemarke
Close. According to [http://xamarin.com/how-it-works](http://xamarin.com/how-
it-works) , they do Ahead-Of-Time compilation to go straight to ARM binaries
for iOS.

------
adrnsly
I use Ejecta for almost all my prototyping, super easy to make changes during
a meeting to do live iteration testing.

------
checker659
Why not use v8 and make the runtime run "everywhere"?

Edit: Sorry, just realised the development is done in Objective-C.

~~~
phoboslab
v8 also doesn't have an interpreter mode, just a compiler. On iOS you can't
allocate writable+executable memory, so you can't use v8.

As I mentioned in another thread, I currently have a toy project that
implements most of the Canvas2D API in plain C. Maybe this will amount to
something some day.

~~~
checker659
AFAIK, you can turn off JIT compilation in v8, isn't that true?

~~~
sanxiyn
No, you can't turn off JIT compilation in V8. V8 has no not-JIT execution
engine.

------
al2o3cr
Looks cool, but "works out of the box except for controls" is pretty much
"doesn't work out of the box" for games...

------
adamnemecek
It's not really a "JavaScript implementation" when it appears to be just a
wrapper around JavaScriptCore, innit bruv.

