-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
progress-pie.js
111 lines (97 loc) · 2.9 KB
/
progress-pie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Pie from "@pencil.js/pie";
import Input from "@pencil.js/input";
import Circle from "@pencil.js/circle";
import MouseEvent from "@pencil.js/mouse-event";
import { constrain } from "@pencil.js/math";
/**
* @module ProgressPie
*/
const valueKey = Symbol("_value");
// FIXME: admittedly this is not an input, maybe we need a better name for the super class.
/**
* ProgressPie class
* <br><img src="./media/examples/progress-pie.png" alt="progress-pie demo"/>
* @class
* @extends {module:Input}
*/
export default class ProgressPie extends Input {
/**
* ProgressPie constructor
* @param {PositionDefinition} positionDefinition - Position of the progress-pie center
* @param {ProgressPieOptions} [options] - Specific options
*/
constructor (positionDefinition, options) {
super(positionDefinition, Circle, options);
this.removeListener([MouseEvent.events.hover, MouseEvent.events.leave, MouseEvent.events.click]);
this.progress = new Pie(this.getOrigin(), this.radius - (this.options.strokeWidth / 2), 0, 0, {
fill: this.options.foreground,
});
this.add(this.progress);
if (this.options.speed < 1) {
this.progress.on(ProgressPie.events.draw, () => {
this.progress.endAngle += (this.value - this.progress.endAngle) * (this.options.speed ** 2);
}, true);
}
/**
* @type {Number}
*/
this[valueKey] = null;
}
/**
* @inheritDoc
*/
click () { // eslint-disable-line class-methods-use-this
// Do nothing
}
/**
* Return this size
* @return {Number}
*/
get radius () {
return this.options.radius;
}
/**
* Change this size
* @param {Number} newRadius - A new size in pixels
*/
set radius (newRadius) {
this.options.radius = newRadius;
this.progress.radius = newRadius - 1;
}
/**
* Returns this current value
* @return {Number}
*/
get value () {
return this[valueKey];
}
/**
* Change this current value
* @param {Number} newValue - A new value to be set (between 0 and 1)
*/
set value (newValue) {
this[valueKey] = constrain(newValue, 0, 1);
if (this.options.speed >= 1) {
this.progress.endAngle = this.value;
}
}
/**
* @typedef {Object} ProgressPieOptions
* @extends InputOptions
* @prop {Number} [value=0] - Initial value
* @prop {Number} [radius=100] - Size of the progress-pie
* @prop {Number} [speed=0.3] - Transition speed between two value (0 mean no change, 1 mean instant change)
*/
/**
* @type {ProgressPieOptions}
*/
static get defaultOptions () {
return {
...super.defaultOptions,
cursor: null,
value: 0,
radius: 100,
speed: 0.3,
};
}
}