I'm a Norwegian software developer. Eager to learn and to share knowledge. Sharing is caring!

29 Aug 2016

Making an Ocean with Angular 2.0

I used to dislike Angular a bit. Angular was one of those frameworks that were easy to recognize. I never felt like I was writing "real" JavaScript when I wrote Angular. Your applications became Angular applications. And those applications felt large and clumsy. Good for when you're making something that uses a lot of Angular features, not so good for when only what to use it for a small module. So how is the new Angular 2.0?

Lets create something! As summer comes to an end, we like to remember the good times at sea. So lets make a sail boat and an ocean with randomly generated content like clouds, fish and islands.

TypeScript and modules

I followed the basic guide on the Angular website. I used SystemJS as a module loader. I'm sure you can use browserify or webpack if that's your preferred module loader. Here I'm essentially setting up a watcher on my files, pipe the code through a TypeScript transpiler (tsc) and bundling it all together. We also have a local web server that keeps the application alive on your development computer. It requires some setup, but that is relatively normal in concurrent applications. You can also set Angular 2.0 up without any transpiling or magical jazz.

Animating the ocean

Probably the least complex part of this solution. I draw a sky and an ocean in CSS. The ocean has some "parallax" effects with waves. Here's the demo with CSS highlighted.

Creating some models

One of the clear benefits of TypeScript is declaring types. And with larger application it becomes an advantage to introduce a type system to your JavaScript. Here we specify a couple of types.

Here we have some types. Spawn will tell the application what to spawn and how to spawn it. For example an island will not spawn as often as a cloud. The Direction of an island would be on the lower part of the screen, where the water is.

Element is the result of the Spawn. A Spawn will create many elements. An element is what Angular will parse as HTML and animate. An Element will get altered during the runtime of the application.

Starting point of the application

We need to initialize the code! And with a bootstrap method, that is fairly easy.

Alas! Here we have some interesting stuff. The way Angular declares a view, is with a decorator called @Component. Here you declare the selector you want the application to render in, the template and other directives you might want to include in the component. The template can also be put in to a different file with templateUrl if that's preferable. The class AppComponent contains all the code that the application needs to render. By decorating the component you can focus on writing logic specific for your application without worrying about Angular itselves. We also have some logic to render the scene as night or day based on the time.

Spawning stuff

Now we need to spawn some stuff. Islands and clouds... among other things. Some things spawn more frequently and quicker than others. Our AppComponent consists of a few tags. I've defined these as <spawner>. This module will spawn a type of element with a class name. In the CSS we'll postfix our class names with numbers declaring the total number of classes that we want to randomize from. So the class name island that has 4 total classes will have the CSS classes "island1, island2, island3, island4". Our stuff will be styled and animated with CSS and CSS transitions.

This code will progress the animation of a spawn. It will decide a velocity, position and animate that fixture until it runs off screen. After it runs off the screen, it will be destroyed. The animation is looped with requestAnimationFrame and rendered by Angular 2.0. The interesting bits here are the OnInit and how Angular treats styles with ngStyles. OnInit will inherit a method called ngOnInit(). This will run when the component is initialized. A useful and obvious method! ngStyles is the way Angular processes styles and is treated with a JSON. Everything is built up on modules and that's wonderful!

I like it!

All in all, I like the new Angular so far! It's somewhat cleaner than the previous installment. It allows me to to write more modular code and it all feels like it's less dependent of Angular on its own.