-
Notifications
You must be signed in to change notification settings - Fork 1
/
spikes.js
86 lines (74 loc) · 2.26 KB
/
spikes.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
'use strict'
module.exports = createSpikes2D
function GLSpikes2D(plot) {
this.plot = plot
this.enable = [true, true, false, false]
this.width = [1, 1, 1, 1]
this.color = [[0,0,0,1],
[0,0,0,1],
[0,0,0,1],
[0,0,0,1]]
this.center = [Infinity, Infinity]
}
var proto = GLSpikes2D.prototype
proto.update = function(options) {
options = options || {}
this.enable = (options.enable || [true,true,false,false]).slice()
this.width = (options.width || [1,1,1,1]).slice()
this.color = (options.color || [
[0,0,0,1],
[0,0,0,1],
[0,0,0,1],
[0,0,0,1]]).map(function(x) { return x.slice() })
this.center = (options.center || [Infinity,Infinity]).slice()
this.plot.setOverlayDirty()
}
proto.draw = function() {
var spikeEnable = this.enable
var spikeWidth = this.width
var spikeColor = this.color
var spikeCenter = this.center
var plot = this.plot
var line = plot.line
var dataBox = plot.dataBox
var viewPixels = plot.viewBox
line.bind()
if(dataBox[0] <= spikeCenter[0] && spikeCenter[0] <= dataBox[2] &&
dataBox[1] <= spikeCenter[1] && spikeCenter[1] <= dataBox[3]) {
var centerX = viewPixels[0] + (spikeCenter[0] - dataBox[0]) / (dataBox[2] - dataBox[0]) * (viewPixels[2] - viewPixels[0])
var centerY = viewPixels[1] + (spikeCenter[1] - dataBox[1]) / (dataBox[3] - dataBox[1]) * (viewPixels[3] - viewPixels[1])
if(spikeEnable[0]) {
line.drawLine(
centerX, centerY,
viewPixels[0], centerY,
spikeWidth[0], spikeColor[0])
}
if(spikeEnable[1]) {
line.drawLine(
centerX, centerY,
centerX, viewPixels[1],
spikeWidth[1], spikeColor[1])
}
if(spikeEnable[2]) {
line.drawLine(
centerX, centerY,
viewPixels[2], centerY,
spikeWidth[2], spikeColor[2])
}
if(spikeEnable[3]) {
line.drawLine(
centerX, centerY,
centerX, viewPixels[3],
spikeWidth[3], spikeColor[3])
}
}
}
proto.dispose = function() {
this.plot.removeOverlay(this)
}
function createSpikes2D(plot, options) {
var spikes = new GLSpikes2D(plot)
spikes.update(options)
plot.addOverlay(spikes)
return spikes
}