My question follows "get average color of image via javascript" which had a great answer in this Fiddle by Ariona Rian.
The problem is: if I get the image through ng-src, then how do I get the document element or the data to pass to the function getAverageRGB(imgEl)
?
Here is a simple example. I am loading images from an array containing URLs. When the images are loaded, upon clicking on one of the images (the surrounding divs in fact), I would like to compute the average RGB value of the image that I click.
<div ng-repeat="url in urlArray">
<div ng-click="trigger('{{$index}}')">
<img id="img_{{$index}}" data-ng-src="{{url}}" >
</div>
</div>
<div>
<p>The average RGB value of the selected image is: {{avgRGB}}</p>
</div>
How do I write the function in the controller so that I can call the function that computes the average RGB?
It looks like I have to pass a canvas or some DOM element like that, but I could not understand how.
// function inside controller
$scope.trigger = function(index) {
// Obtain the variable imgEl <--- How?
$scope.avgRGB = getAverageRGB(imgEl);
};
I tried to pass directly the img element through this call:
// inside the function 'trigger()'
var elID = 'img_' + index
var imgEl = angular.element( document.querySelector( elID ) );
However, the function getAverageRGB()
complains.