Click here to Skip to main content
15,867,308 members
Articles / DevOps / TFS
Article

Experiment with ECMAScript 6 on Babylon.js with TypeScript 1.5

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Oct 2015CPOL5 min read 15.3K   4  
Since releasing babylon.js, the WebGL open-source gaming framework, a couple of years ago, we (with help from the community) are constantly exploring ways to make it even better.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

Since releasing babylon.js, the WebGL open-source gaming framework, a couple of years ago, we (with help from the community) are constantly exploring ways to make it even better. I’m definitely more than happy that we decided more than one year ago to switch over to TypeScript. For more on that decision, read why we decided to move from plain JavaScript to TypeScript for Babylon.js

Thanks to TypeScript, we’ve been able to improve the quality of our code, improve our productivity and create our fabulous Playground we’re so proud of: http://www.babylonjs-playground.com/, which provides auto-completion in the browser! We’ve also been able to welcome some new team members coming from a C# background and few JS skills with no pain. But thanks to the TypeScript compiler, we can also test the future without rewriting a single line of code!

We are still coding babylon.js using Visual Studio and TFS while pushing in a regular manner our code to the github repo. By upgrading our project to Visual Studio 2015 RTM, we’ve been able to upgrade it to TypeScript 1.5.

Once done, let me show you how quickly I’ve upgraded Babylon.js from ES5 to ES6. Right-click on your project, navigate to "TypeScript Build" and switch the "ECMAScript version" from ES5 to ES6:

Image 1

And that’s it!

Recompile the solution and you can test the future of ES6 today.

I’ve published an ES6 version of babylonjs.com here: http://www.babylonjs.com/indexES6.html/ to let you play with it.

Microsoft Edge and ES6

If you’re interested in ES6, I recommend watching this BUILD session: What’s New in JavaScript for Fast and Scalable Apps.

You’ll note you need a very recent modern browser such as Microsoft Edge to be able to execute this demo and code. Microsoft Edge and Firefox 41 are currently the most advanced browsers in ES6 support. You can check your current browser ES6 support here.

Launching it in Microsoft Edge on Windows 10 (build 10240), you’ll have those results:

Image 2

67% of ES6 features supported in MS Edge and 68% in Firefox 41 out of the box. Those results are impressive!

Still, if you’re launching the ES6 version of Babylon.js in MS Edge, you’ll see some errors in F12:

Image 3

Let’s navigate to babylon.math.js to check the error. We’re falling here:

Image 4

The "class" keyword is not supported yet for production. This is because the spec has changed recently and all browsers are thus putting it behind a flag.

To enable it, navigate to "about:flags" in MS Edge and "Enable experimental JavaScript features"

Image 5

If you now run again the ES6 compatibility tool: http://kangax.github.io/compat-table/es6/, you’ll see now that MS Edge is jumping to 81% of ES6 features supported. It now supports class, super & generators:

Image 6

To make this demo works in Firefox or Chrome, you’ll probably need a nightly build.

Time to play with it in F12

Now that Microsoft Edge is properly configured, navigate to: http://www.babylonjs.com/indexES6.html/ and open F12 in a separate window. In the "Debugger" tab, open "babylon.gamepadCamera.js" and set a breakpoint on the "super" line of code:

Image 7

Launch the "Mansion" demo and switch the camera to "Gamepad Camera":

Image 8

You’ll properly break into the code as expected:

Image 9

Press F11 to jump into the extended class (BABYLON.FreeCamera):

Image 10

You’re currently debugging ES6 code! Isn’t that cool?

In the "Debugger" tab, open "babylon.virtualJoystick.js" and set a breakpoint on line 78 inside the arrow function:

Image 11

Switch the camera to "Virtual joysticks camera", touch the screen or left click it and you’ll be able to debug the arrow function:

Image 12

Now, imagine you’d like to edit your ECMAScript 6 code to improve your debugging experience. Go to the "Experiments" tab and enable the "Edit JavaScript" option:

Image 13

Restart the browser. Now, add this line of code on the previous handle "let foo = ‘test’" and do again the previous action:

Image 14

Ok, let’s recap. This demo is using: ECMAScript 6 with classes, super & arrow functions, WebGL, Web Audio, Gamepad API and Pointer Events. Thank you Babylon.js, thank you TypeScript & thank you Microsoft Edge!

If you’re interested about other improvements we’ve done in F12, have a look to this article: Announcing the latest improvements for the F12 developer tools in Windows 10

More hands-on with JavaScript

This article is part of the web development series from Microsoft tech evangelists on practical JavaScript learning, open source projects, and interoperability best practices including Microsoft Edge browser and the new EdgeHTML rendering engine.

We encourage you to test across browsers and devices including Microsoft Edge – the default browser for Windows 10 – with free tools on dev.modern.IE:

In-depth tech learning on Microsoft Edge and the Web Platform from our engineers and evangelists:

More free cross-platform tools & resources for the Web Platform:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
David Rousset is a Senior Program Manager working for Microsoft Corp, in charge of driving adoption of HTML5 standards. He was a speaker in several famous web conferences such as Paris Web, CodeMotion, ReasonsTo or jQuery UK. He’s the co-author of the WebGL Babylon.js open-source engine. Read his blog on MSDN or follow him @davrous on Twitter.

Comments and Discussions

 
-- There are no messages in this forum --