Using the new TypeScript samples

Hello everyone!

We have new TypeScript-based samples. I wanted to share briefly how to get started:

Download samples

You can download a zip or you can clone the repository from to get ongoing updates

Open a sample and NPM install

Pick one of the samples (like “BunnyMark”) and run npm install to setup dependencies:

cd openfl-samples-ts/BunnyMark
npm install

Start a dev server

npm start

The project should compile, and open a browser window. Hot reloading is enabled, so make changes to the source *.ts files, and the project will automatically rebuild, and the window will reload.

Other notes

Need a code editor? is a good choice with built-in support for TypeScript

Please post if you have any questions!


Thank you for doing this! I’m really excited about this new approach.

Thank you for doing this! I’m really excited about this new approach. :wink:

Unbelievable! This is so cool. I really hope it will get the same love from the type script community as phaser and pixi.

does some of the code run on the server side?

(sorry if it’s a stupid question)

All of the code (in these samples) run on the client, but if users are interested in making code work from a server (such as Node.js) then we may make some improvements to better support that (long-term)

1 Like

Hey guys, more samples added! :slight_smile:

Thanks! Wow, you added a lot of them.

Thanks :grinning: I’m trying to use it.

stage set height bug:

I find a bug, getContext(‘2d’) is null. @singmajesty

The Stage constructor was (window:Window, background:Int), and still is in some code. I plan on unifying this more in the future, but for now, I believe that when using the NPM version of OpenFL, it should not hit the second case unless you pass a bad parameter to the first argument.

I have just updated the OpenFL library on NPM, with support for setting the renderer type in the constructor.

We use OpenGL (WebGL) by default, but now you should be able to choose a different renderer type:

var stage = new Stage (550, 400, 0xFFFFFF, App, { renderer: "canvas" });

var stage = new Stage (550, 400, 0xFFFFFF, App, { renderer: "webgl1" });

var stage = new Stage (550, 400, 0xFFFFFF, App, { renderer: "dom" });

We use "webgl" (or "opengl") by default, which uses WebGL 2 if available, and falls back to canvas if WebGL is not available. Using a different value can force use of WebGL 1 only, or using a 2D canvas (even if WebGL is available).

We also have a DOM-based renderer, which isn’t as heavily used, but allows mixing of DOM elements, such as this sample: :slight_smile:

Thanks , It works! :grinning:

I want to set this canvas to be transparent, but this color can’t be set to transparent. @singmajesty
Whether this parameter can be modified to a string type (‘rgba(0,0,0,0)’). :slightly_smiling_face:

I find some bugs about mask.
1, canvas: ok

2, webgl1: error


import Sprite from "openfl/display/Sprite";
import Stage from "openfl/display/Stage";
import Bitmap from "openfl/display/Bitmap";
import BitmapData from "openfl/display/BitmapData";
import MouseEvent from "openfl/events/MouseEvent";
import Shape from "openfl/display/Shape";
import Event from "openfl/events/Event";

class App extends Sprite {
	constructor () {
		super ();
		BitmapData.loadFromFile ("openfl.png").onComplete ((bitmapData) => {
			var bitmap = new Bitmap (bitmapData);
			this.addChild (bitmap);
			bitmap.scaleX = bitmap.scaleY = 0.5;
			bitmap.x= bitmap.y = 50;

			this.test = new Sprite();;,0,100,100);;
			this.test.x = this.test.y = 200;

			this.mas = new Shape();;,0,25,50);;
			this.mas.x = this.mas.y = 50;

			this.test.mask = this.mas;

			if(this.mas != null)
				this.mas.rotation ++;

var stage = new Stage (550, 400, 0xffffff, App,{renderer: "canvas"});
document.body.appendChild (stage.element);
var canvas = document.getElementsByTagName('canvas')[0];
console.log( canvas.getContext('2d') );  


I’ll take a look at the mask.

Meanwhile, try using null to get a transparent background :slight_smile:

Thanks! I just found the issue

Screenshot from 2018-01-05 11-44-59

I forgot to enable the stencil buffer on our WebGL builds :slight_smile:

(If you use OpenFL from source, I just added an npm run watch command, so it can recompile our CommonJS modules automatically upon changes to the source. It rebuilds all our files for now, but in the future we can probably improve it more)

EDIT: OpenFL has been updated on NPM with this fix :wink:

Thanks. it works :grinning:

1 Like

Thanks very very mush. it also works :slightly_smiling_face:

1 Like