Check out our Angular Book Series.

Select a Color -- Build a Color Picker in Angular 2 - Part 3

This section will show you how to run Angular code when the canvas is clicked on. Then it will process the image inside the canvas to determine the color and display the results out to the user. It will display results in two different ways; the first is to output the RGB values of the image. The second will be to display another canvas which is colored based on what we selected.

Modify the External Template

Open up the external template, canvas.html. We'll make a few changes here. First, add a cross hair cursor to the canvas using CSS. This will let people know the canvas can be clicked on:

style="border:1px solid #d3d3d3;cursor: crosshair;"

This might be better put in an external CSS file, but for simplicity I defined all styles in-line. The canvas will need to do something when the image is clicked, so add a click event:


We'll create the pickColor() method inside our main app's component in the next section. Notice the click event is passed to the pickColor() method.

Now, create a section to output the selected color:

<h1>Selected Color</h1>
RGBA: {{RGBA}}<br/><br/>
<canvas id="selectedColorCanvas" width="250" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.

The first thing it does is output a variable named RGBA. This includes the red, green, blue, and alpha values that make up the selected the color. We'll create that variable in the pickColor() event method.

The second element in the template is a new canvas. This will do nothing but display the selected color of the canvas.

Modify the Script

Now move to the JavaScript code. All this code will modify the colorpicker-app component. First, we need a hook to get access to the selectedColorCanvas. Modify the queries property of the component:

queries : {
imageCanvas : new ng.core.ViewChild('imageCanvas'),
selectedColorCanvas : new ng.core.ViewChild('selectedColorCanvas')

The imageCanvas was there from previous samples, but the selectedColorCanvas uses the same approach. The selectedColorCanvas variable will be accessible inside the class constructor.

Inside the class constructor, create a variable for the selectedColorCanvas context:

var selectedColorCanvasContext;

Inside the image onload() method, inside the onInit() method, save the selectedColorCanvas 2D context:

selectedColorCanvasContext = selectedColorCanvas.getContext('2d');

Also, create a variable to include the RGBA value:

this.RGBA = ''

This variable is put in the 'this' scope. This is similar to the $scope of an AngularJS 1 application, and using 'this' allows the value to be accessed inside the view template.

Now, implement the pickColor() method. Here is the stub:

this.pickColor = function(event){

The event is passed in so we can get the x and y position that was clicked on the image:

var data = imageCanvasContext.getImageData(event.layerX, event.layerY, 1, 1).data;

This loads all the image data using the getImageData() method on the imageCanvasContext. The values into this method are the x value, the y value, the width, and height. layerX and layerY values from the event object specify the location that was clicked. By specifying 1, 1, for the width and height we get all the information for the single pixel which was clicked.

The data object contains an array with four elements, representing the red, green, blue, and alpha properties on the pixel that was clicked. Now, let's draw that color on the selectedColorCanvas:

selectedColorCanvasContext.fillStyle = "rgba(" + data[0] + "," + data[1] + "," + data[2] + "," + data[3] + ")"

First the fillStyle is defined, as a string, using the rgba() method along with the RGBA data. Then the fullRect() is called. The fullRect() method accepts four arguments, similar to the getImageData() method. The first two values are the x and y values to start the rectangle. The third and fourth values are the height and width, respectively. Our rectangle draws all over the selected canvas.

Finally, set the RGBA value using the same data:

this.RGBA = data[0] + "," + data[1] + "," + data[2] + "," + data[3] ;

Now, run the code and start clicking away:

You can play with the app here, but because of cross domain issues with the image I couldn't upload it to a Plunker.

Final Thoughts

It was nice to delve into the canvas functionality of HTML5 as I don't get to deal with that in normal enterprise level development for most of my clients. I'm starting to get deeper into Angular 2 development.

Sign up for DotComIt's Monthly Technical Newsletter

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version