|
1 |
| -(function () { |
| 1 | +(function (angular) { |
2 | 2 |
|
3 |
| -var TAU = Math.PI * 2; |
4 |
| -var ANGLE_OFFSET = -Math.PI / 2; |
5 |
| -var ANGLE_MAX = TAU * 0.9; |
| 3 | + 'use strict'; |
6 | 4 |
|
7 |
| -var polarToCartesian = function (x, y, radius, radians) { |
8 |
| - return [ |
9 |
| - x + radius * Math.cos(radians), |
10 |
| - y + radius * Math.sin(radians) |
11 |
| - ]; |
12 |
| -}; |
| 5 | + var TAU = Math.PI * 2; |
| 6 | + var ANGLE_OFFSET = -Math.PI / 2; |
| 7 | + var ANGLE_MAX = TAU * 0.9; |
13 | 8 |
|
14 |
| -var decimalToRadian = function (decimal) { |
15 |
| - return (decimal * TAU); |
16 |
| -}; |
| 9 | + var polarToCartesian = function (x, y, radius, radians) { |
| 10 | + return [ |
| 11 | + x + radius * Math.cos(radians), |
| 12 | + y + radius * Math.sin(radians) |
| 13 | + ]; |
| 14 | + }; |
17 | 15 |
|
18 |
| -var app = angular.module('angular-progress-arc', []); |
| 16 | + var decimalToRadian = function (decimal) { |
| 17 | + return (decimal * TAU); |
| 18 | + }; |
19 | 19 |
|
20 |
| -app.directive('progressArc', function () { |
21 |
| - return { |
22 |
| - restrict: 'E', |
23 |
| - scope: { |
24 |
| - width: '@', |
25 |
| - height: '@', |
26 |
| - strokeWidth: '@', |
27 |
| - stroke: '@', |
28 |
| - progress: '@', |
29 |
| - counterClockwise: '@' |
30 |
| - }, |
31 |
| - link: function (scope, element, attr) { |
| 20 | + var app = angular.module('angular-progress-arc', []); |
32 | 21 |
|
33 |
| - scope.getDescription = function () { |
| 22 | + app.directive('progressArc', function () { |
| 23 | + return { |
| 24 | + restrict: 'E', |
| 25 | + scope: { |
| 26 | + width: '@', |
| 27 | + height: '@', |
| 28 | + strokeWidth: '@', |
| 29 | + stroke: '@', |
| 30 | + progress: '@', |
| 31 | + counterClockwise: '@' |
| 32 | + }, |
| 33 | + link: function (scope, element, attr) { |
34 | 34 |
|
35 |
| - if (scope.progress <= 0) { |
36 |
| - return 'M 0 0'; |
37 |
| - } |
| 35 | + scope.getDescription = function () { |
38 | 36 |
|
39 |
| - var centerX = scope.width / 2; |
40 |
| - var centerY = scope.height / 2; |
41 |
| - var minDimension = Math.min(scope.width, scope.height); |
| 37 | + if (scope.progress <= 0) { |
| 38 | + return 'M 0 0'; |
| 39 | + } |
42 | 40 |
|
43 |
| - // Cap stroke width so that it's not larger than the radius. |
44 |
| - if (scope.strokeWidth * 2 > minDimension) { |
45 |
| - scope.strokeWidth = minDimension / 2; |
46 |
| - } |
| 41 | + var centerX = scope.width / 2; |
| 42 | + var centerY = scope.height / 2; |
| 43 | + var minDimension = Math.min(scope.width, scope.height); |
47 | 44 |
|
48 |
| - var radius = (minDimension - scope.strokeWidth) / 2; |
49 |
| - var largeArc = scope.progress > 0.5 ? 1 : 0; |
50 |
| - var p1 = polarToCartesian(centerX, centerY, radius, ANGLE_OFFSET); |
51 |
| - var clockwise = parseInt(scope.counterClockwise) ? 0 : 1; |
52 |
| - var angle = scope.progress >= 1.0 |
53 |
| - ? ANGLE_MAX |
54 |
| - : decimalToRadian(scope.progress); |
55 |
| - var p2 = polarToCartesian( |
56 |
| - centerX, |
57 |
| - centerY, |
58 |
| - radius, |
59 |
| - clockwise ? ANGLE_OFFSET + angle : ANGLE_OFFSET - angle |
60 |
| - ); |
| 45 | + // Cap stroke width so that it's not larger than the radius. |
| 46 | + if (scope.strokeWidth * 2 > minDimension) { |
| 47 | + scope.strokeWidth = minDimension / 2; |
| 48 | + } |
| 49 | + // -1 from radius to prevent clipping edge of svg element. |
| 50 | + var radius = (minDimension - scope.strokeWidth) / 2 - 1; |
| 51 | + var largeArc = scope.progress > 0.5 ? 1 : 0; |
| 52 | + var p1 = polarToCartesian(centerX, centerY, radius, ANGLE_OFFSET); |
| 53 | + var clockwise = parseInt(scope.counterClockwise) ? 0 : 1; |
| 54 | + var angle = scope.progress >= 1.0 ? ANGLE_MAX : decimalToRadian(scope.progress); |
| 55 | + var p2 = polarToCartesian( |
| 56 | + centerX, |
| 57 | + centerY, |
| 58 | + radius, |
| 59 | + clockwise ? ANGLE_OFFSET + angle : ANGLE_OFFSET - angle |
| 60 | + ); |
61 | 61 |
|
62 |
| - var d = [ |
63 |
| - 'M', p1[0], p1[1], |
64 |
| - 'A', radius, radius, 0, largeArc, clockwise, p2[0], p2[1] |
65 |
| - ]; |
| 62 | + var d = [ |
| 63 | + 'M', p1[0], p1[1], |
| 64 | + 'A', radius, radius, 0, largeArc, clockwise, p2[0], p2[1] |
| 65 | + ]; |
66 | 66 |
|
67 |
| - // A complete circle is not possible with a path and a single |
68 |
| - // arc call. So an additional arc is created here to join back |
69 |
| - // to the initial point. |
70 |
| - if (scope.progress >= 1.0) { |
71 |
| - d = d.concat([ |
72 |
| - 'A', radius, radius, 0, 0, clockwise, p1[0], p1[1] |
73 |
| - ]); |
74 |
| - } |
| 67 | + // A complete circle is not possible with a path and a single |
| 68 | + // arc call. So an additional arc is created here to join back |
| 69 | + // to the initial point. |
| 70 | + if (scope.progress >= 1.0) { |
| 71 | + d = d.concat([ |
| 72 | + 'A', radius, radius, 0, 0, clockwise, p1[0], p1[1] |
| 73 | + ]); |
| 74 | + } |
75 | 75 |
|
76 |
| - return d.join(' '); |
77 |
| - }; |
78 |
| - }, |
79 |
| - template: |
80 |
| - '<svg ng-attr-width="{{ width }}" ng-attr-height="{{ height }}">' + |
81 |
| - '<path fill="none" ng-attr-stroke="{{ stroke }}" ng-attr-stroke-width="{{ strokeWidth }}" ng-attr-d="{{ getDescription() }}">' + |
82 |
| - '</svg>' |
83 |
| - }; |
84 |
| -}); |
| 76 | + return d.join(' '); |
| 77 | + }; |
| 78 | + }, |
| 79 | + template: |
| 80 | + '<svg ng-attr-width="{{ width }}" ng-attr-height="{{ height }}">' + |
| 81 | + '<path fill="none" ng-attr-stroke="{{ stroke }}" ng-attr-stroke-width="{{ strokeWidth }}" ng-attr-d="{{ getDescription() }}">' + |
| 82 | + '</svg>' |
| 83 | + }; |
| 84 | + }); |
85 | 85 |
|
86 |
| -})(); |
| 86 | +})(window.angular); |
0 commit comments