OLD | NEW |
| (Empty) |
1 angular.module('diff_viewer', []). | |
2 config(['$routeProvider', function($routeProvider) { | |
3 // Show the list of differences by default | |
4 $routeProvider. | |
5 otherwise({ templateUrl: '/diff_list.html', controller: DiffListController})
; | |
6 }]). | |
7 directive('swapImg', function() { | |
8 // Custom directive for showing an image that gets swapped my mouseover. | |
9 return { | |
10 restrict: 'E', // The directive can be used as an element name | |
11 replace: true, // The directive replaces itself with the template | |
12 template: '<canvas ng-mouseenter="swap()" ng-mouseleave="swap()"></canva
s>', | |
13 scope: { // The attributes below are bound to the scope | |
14 leftSrc: '@', | |
15 rightSrc: '@', | |
16 side: '@' | |
17 }, | |
18 link: function(scope, elm, attrs, ctrl) { | |
19 var leftImage = new Image(); | |
20 var rightImage = new Image(); | |
21 var ctx = elm[0].getContext('2d'); | |
22 | |
23 scope.render = function() { | |
24 var image; | |
25 if (scope.side == "left") { | |
26 image = leftImage; | |
27 } else { | |
28 image = rightImage; | |
29 } | |
30 | |
31 // Make it so the maximum size of an image is 500, and the image
s are scaled | |
32 // down in halves. | |
33 var divisor = 1; | |
34 while ((image.width / divisor) > 500) { | |
35 divisor *= 2; | |
36 } | |
37 | |
38 // Set canvas to correct size and draw the image into it | |
39 elm[0].width = image.width / divisor; | |
40 elm[0].height = image.height / divisor; | |
41 ctx.drawImage(image, 0, 0, elm[0].width, elm[0].height); | |
42 }; | |
43 | |
44 // When the leftSrc attribute changes, load the image and then reren
der | |
45 attrs.$observe('leftSrc', function(value) { | |
46 leftImage.src = value; | |
47 leftImage.onload = function() { | |
48 if (scope.side == "left") { | |
49 scope.render(); | |
50 } | |
51 }; | |
52 }); | |
53 | |
54 // When the rightSrc attribute changes, load the image and then rere
nder | |
55 attrs.$observe('rightSrc', function(value) { | |
56 rightImage.src = value; | |
57 rightImage.onload = function() { | |
58 if (scope.side == "right") { | |
59 scope.render(); | |
60 } | |
61 }; | |
62 }); | |
63 | |
64 // Swap which side to draw onto the canvas and then rerender | |
65 scope.swap = function() { | |
66 if (scope.side == "left") { | |
67 scope.side = "right"; | |
68 } else { | |
69 scope.side = "left"; | |
70 } | |
71 scope.render(); | |
72 }; | |
73 } | |
74 }; | |
75 }); | |
76 | |
77 function DiffListController($scope) { | |
78 // Label each kind of differ for the sort buttons. | |
79 $scope.differs = [ | |
80 { | |
81 "title": "Different Pixels" | |
82 }, | |
83 { | |
84 "title": "Perceptual Difference" | |
85 } | |
86 ]; | |
87 | |
88 // Puts the records within AngularJS scope | |
89 $scope.records = SkPDiffRecords.records; | |
90 | |
91 // Indicates which diff metric is used for sorting | |
92 $scope.sortIndex = 1; | |
93 | |
94 // Called by the sort buttons to adjust the metric used for sorting | |
95 $scope.setSortIndex = function(idx) { | |
96 $scope.sortIndex = idx; | |
97 }; | |
98 | |
99 // A predicate for pulling out the number used for sorting | |
100 $scope.sortingDiffer = function(record) { | |
101 return record.diffs[$scope.sortIndex].result; | |
102 }; | |
103 } | |
OLD | NEW |