-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsegment.js
80 lines (68 loc) · 1.42 KB
/
segment.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
/**
* Module dependencies.
*/
var Point = require('./point');
/**
* Expose `Segment`.
*/
module.exports = Segment
/**
* Initialize a new `Segment` with the given dimension and intensity.
*
* @param {Number} x
* @param {Number} y
* @param {Number} w
* @param {Number} h
* @param {Number} intensity
* @api private
*/
function Segment(x, y, w, h, intensity) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.intensity = intensity || 0;
}
/**
* Return the mid-point.
*
* @return {Point}
* @api public
*/
Segment.prototype.midpoint = function(){
return new Point(
this.x + this.w / 2,
this.y + this.h / 2);
};
/**
* Draw the segment.
*
* @param {CanvasContext2d} ctx
* @api public
*/
Segment.prototype.draw = function(ctx){
var n = 255 * this.intensity | 0;
var rad = this.w / 2 * this.intensity;
ctx.save();
ctx.beginPath();
ctx.fillStyle = 'rgba(' + n + ',0,0, .5)';
ctx.arc(this.x + this.w / 2, this.y + this.h / 2, rad, 0, Math.PI * 2, false);
ctx.fill();
ctx.restore();
};
/**
* Draw the focused segment.
*
* @param {CanvasContext2d} ctx
* @api public
*/
Segment.prototype.drawFocus = function(ctx){
var n = 255 * this.intensity | 0;
var rad = this.w / 2 * this.intensity;
ctx.save();
ctx.beginPath();
ctx.fillStyle = 'rgba(0,0,' + n + ', .5)';
ctx.arc(this.x + this.w / 2, this.y + this.h / 2, rad, 0, Math.PI * 2, false);
ctx.fill();
ctx.restore();
};