Getting started with VueJS
September 29th, 2016
VueJS is a hot contender with ReactJS, but includes most of that stuff you loved about Angular and less of the stuff you didnt.
Angular 2.0 has finally been officially released. To help develop my own understanding of it and surrounding technologies, I’ve been working on building a simple little multiplayer card game with it. This got me wondering about how to work with the Canvas API in an Angular2 application. None of the tutorials, articles, or books I’ve read have touched on this particular use case. By experimenting, digging into the Angular 2 source, and scouring the web, I came up with a solution that seems work well. I setup a github repo which you can check out and run yourself: https://github.com/sflahave/angular2-canvas-examples . This repo was copied from the Angular2 QuickStart repo, so if you’ve gone through the QuickStart tutorial, the setup should look familiar.
My first attempt at working with the Canvas involved modifying the example code in the Attribute Directives tutorial from the official Angular2 documentation. Following the example set in that tutorial, I created an attribute directive. The file barchart.component.ts from my repo gives a working example. A screenshot is shown below. The essential structure is that there is a BarchartComponent whose template has a
Why did I do it this way? Well, I had some trouble figuring out what I could do with ElementRef
and it’s nativeElement
property. I wanted to just use the single component, and put the nativeElement
of the ElementRef
passed to the constructor to get a handle on the embedded ElementRef
referred to. By applying an attribute directive directly on the canvas, the ElementRef
passed into BarchartDataDirective's
constructor refers to the canvas, allowing me to get a handle on it.
This setup works, but it’s not very satisfying. Notice that BarchartDataDirective
assumes/expects that it is being applied to a BarchartComponent
and BarchartDataDirective
each have the same set of inputs - BarchartComponent
just forwards them along to BarchartDataDirective
. BarchartComponent
doesn’t really do anything except setup the template and serve as a sort of public interface. We can improve on this by making use of the ViewChild
decorator.
Now, take a look at Barchart2Component. Using the ViewChild
decorator and a template reference variable, we can get rid of the BarchartDataDirective
and just use BarchartComponent
. I apply a template reference variable to the ElementRef
property in BarchartComponent
that is decorated with @ViewChild('canvas')
. Now we have a reference to the child ngAfterViewInit()
method where I can grab the native
I whipped up a PieChartComponent
using the same technique. Here are some screenshots. (By the way - the actual canvas drawing code for these charts were adapted from examples in the online book HTML Canvas Deep Dive by Josh Marinacci ).
And if charts aren’t your thing, I also thew together a
With all of these components, you can specify the dimensions and colors. Play around with them, go ahead, have a good time.
Direct DOM manipulation is generally frowned upon in Angular2 (see the comments for ElementRef in the Angular2 source code) but, as far as I know, there’s no other way to use the Canvas API. Actually I’m not sure if the techniques I’ve shown here really count as “direct DOM manipulation”, since we’re not adding or removing DOM elements, just using one as it is intended to be used.
Finally, if you’re interested in the topic of Canvas+Angular2, you may have run across the article Rendering in Angular2. While it’s an interesting read, it doesn’t address the same “problem” I had in mind. Instead, it describes how you might swap the default DomRenderer
with an alternate renderer, such as (for example) a CanvasRenderer
to render your _entire_application in a canvas element (or even an Electron renderer). That’s my understanding, anyway. Apparently it’s still experimental. It’s definitely something to watch.
VueJS is a hot contender with ReactJS, but includes most of that stuff you loved about Angular and less of the stuff you didnt.
Strategies, tips, and gotchas for migrating a cross-platform mobile app from Cordova to React Native
Exploring Cordova/PhoneGap, React Native, and NativeScript for hybrid/cross platform mobile development with JavaScript
Insert bio here