Skip to content

Commit 363905c

Browse files
committed
update to new API
1 parent fb055d8 commit 363905c

File tree

4 files changed

+60
-87
lines changed

4 files changed

+60
-87
lines changed

README.md

+37-78
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,66 @@
1-
gl-error-bars
1+
gl-error3d
22
=============
3-
4-
Draws error bars around scatter points.
3+
Draws error bars around data points
54

65
# Example
76

87
```javascript
9-
var shell = require('gl-now')({ clearColor: [0,0,0,0] })
10-
var camera = require('game-shell-orbit-camera')(shell)
11-
var createAxes = require('gl-axes')
12-
var createErrorBars = require('gl-error-bars')
13-
var mat4 = require('gl-mat4')
14-
15-
var bounds = [[-5,-5,-5], [5,5,5]]
16-
var errorbars, axes
17-
18-
shell.on('gl-init', function() {
19-
var gl = shell.gl
20-
21-
camera.lookAt(bounds[1], [0,0,0], [0, 1, 0])
22-
23-
axes = createAxes(gl, {
24-
bounds: bounds
25-
})
26-
27-
errorbars = createErrorBars(gl, {
28-
position: [
29-
[0,0,0],
30-
[0,2,0],
31-
[-2,-3,0]
32-
],
33-
34-
error: [
35-
[[-0.5,-0.5,-0.1], [0.5,0.5,0.5]],
36-
[[0,0,0], [0.5,0.5,0.5]],
37-
[[-0.5,-0.5,0], [0,0,0]]
38-
],
39-
40-
color: [
41-
[1,0,0],
42-
[0,1,0],
43-
[0,0,1]
44-
]
45-
})
8+
var createScene = require('gl-plot3d')
9+
var createScatter = require('gl-scatter3d')
10+
var createErrorBars = require('gl-error3d')
11+
12+
var points = [ [0,0,0], [1,1,1], [-1, 2, -3] ]
13+
var errors = [
14+
[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
15+
[[-0.1,-1,-2],[0,0,0]],
16+
[[-0.1,-0.1,-0.1],[0.1,0.1,0.1]]
17+
]
18+
19+
var scene = createScene()
20+
21+
var scatter = createScatter({
22+
gl: scene.gl,
23+
position: points,
24+
size: 20,
25+
orthographic: true,
26+
lineColor: [0,0,0],
27+
color: [0.7,0.8,0.4],
28+
lineWidth: 1
4629
})
30+
scene.add(scatter)
4731

48-
shell.on('gl-render', function() {
49-
var gl = shell.gl
50-
gl.enable(gl.DEPTH_TEST)
51-
52-
var cameraParameters = {
53-
view: camera.view(),
54-
projection: mat4.perspective(
55-
mat4.create(),
56-
Math.PI/4.0,
57-
shell.width/shell.height,
58-
0.1,
59-
1000.0)
60-
}
6132

62-
axes.draw(cameraParameters)
63-
errorbars.draw(cameraParameters)
33+
var errorBars = createErrorBars({
34+
gl: scene.gl,
35+
position: points,
36+
error: errors,
37+
color: [0.9, 0.3, 0.3]
6438
})
39+
scene.add(errorBars)
6540
```
6641

6742
# API
6843

6944
## Constructor
7045

71-
#### `var errorBars = require('gl-error-bars')(gl, options)`
46+
#### `var errorBars = require('gl-error3d')(options)`
7247
Creates a new error bar object.
7348

7449
* `gl` is a WebGL context
75-
* `object` is a collection of properties which are used to initialize the object
76-
77-
**Returns** A new error bar object
78-
79-
## Methods
80-
81-
#### `errorBars.draw(camera)`
82-
Draws the error bars
83-
84-
* `camera` is an object storing the parameters to draw
85-
86-
+ `camera.model` is the model matrix
87-
+ `camera.view` is the view matrix
88-
+ `camera.projection` is the projection matrix
89-
90-
#### `errorBars.update(options)`
91-
Updates the error bar object
92-
9350
* `position` is the position of each point in the plot
9451
* `error` is an array of error bounds represented as `[lo,hi]` for each point
9552
* `color` a length 3 array of arrays giving the color of the error bars along each axis.
9653
* `lineWidth` is the width of the error bar lines in pixels
9754
* `capSize` is the size of the cap for error bars
9855
* `clipBounds` is a box to which all error bars will be clipped
9956

57+
**Returns** A new error bar object
58+
59+
#### `errorBars.update(options)`
60+
Updates the error bar object
61+
10062
#### `errorBars.dispose()`
10163
Destroy the error bars and release all associated resources
10264

103-
#### `errorBars.bounds`
104-
Bounds on the error bar object for display purposes
105-
10665
# Credits
107-
(c) 2014 Mikola Lysenko. MIT License
66+
(c) 2014-2015 Mikola Lysenko. MIT License

errorbars.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,23 @@ function ErrorBars(gl, buffer, vao, shader) {
2424
this.bounds = [[ Infinity, Infinity, Infinity], [-Infinity,-Infinity,-Infinity]]
2525
this.clipBounds = [[-Infinity,-Infinity,-Infinity], [ Infinity, Infinity, Infinity]]
2626
this.lineWidth = [1,1,1]
27-
this.capSize = [0.1,0.1,0.1]
27+
this.capSize = [0.01,0.01,0.01]
2828
this.lineCount = [0,0,0]
2929
this.lineOffset = [0,0,0]
30+
this.opacity = 1
3031
}
3132

3233
var proto = ErrorBars.prototype
3334

34-
proto.draw = function(cameraParams) {
35+
proto.isOpaque = function() {
36+
return this.opacity >= 1
37+
}
38+
39+
proto.isTransparent = function() {
40+
return this.opacity < 1
41+
}
42+
43+
proto.drawTransparent = proto.draw = function(cameraParams) {
3544
var gl = this.gl
3645
var uniforms = this.shader.uniforms
3746

@@ -40,6 +49,7 @@ proto.draw = function(cameraParams) {
4049
uniforms.view = cameraParams.view || IDENTITY
4150
uniforms.projection = cameraParams.projection || IDENTITY
4251
uniforms.clipBounds = this.clipBounds
52+
uniforms.opacity = this.opacity
4353

4454
this.vao.bind()
4555
for(var i=0; i<3; ++i) {
@@ -89,9 +99,6 @@ function emitFace(verts, x, c, d) {
8999
proto.update = function(options) {
90100
options = options || {}
91101

92-
if('clipBounds' in options) {
93-
this.clipBounds = options.clipBounds
94-
}
95102
if('lineWidth' in options) {
96103
this.lineWidth = options.lineWidth
97104
if(!Array.isArray(this.lineWidth)) {
@@ -104,6 +111,9 @@ proto.update = function(options) {
104111
this.capSize = [this.capSize, this.capSize, this.capSize]
105112
}
106113
}
114+
if('opacity' in options) {
115+
this.opacity = opacity
116+
}
107117

108118
var color = options.color || [[0,0,0],[0,0,0],[0,0,0]]
109119
var position = options.position
@@ -175,6 +185,8 @@ i_loop:
175185
}
176186

177187
this.buffer.update(verts)
188+
189+
console.log(this.bounds)
178190
}
179191
}
180192

@@ -184,7 +196,8 @@ proto.dispose = function() {
184196
this.vao.dispose()
185197
}
186198

187-
function createErrorBars(gl, options) {
199+
function createErrorBars(options) {
200+
var gl = options.gl
188201
var buffer = createBuffer(gl)
189202
var vao = createVAO(gl, [
190203
{

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "gl-error-bars",
3-
"version": "2.2.2",
2+
"name": "gl-error3d",
3+
"version": "1.0.0",
44
"description": "Draws error bars around points",
55
"main": "errorbars.js",
66
"directories": {

shaders/fragment.glsl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
precision mediump float;
22
uniform vec3 clipBounds[2];
3+
uniform float opacity;
34
varying vec3 fragPosition;
45
varying vec4 fragColor;
56

67
void main() {
78
if(any(lessThan(fragPosition, clipBounds[0])) || any(greaterThan(fragPosition, clipBounds[1]))) {
89
discard;
910
}
10-
gl_FragColor = fragColor;
11+
gl_FragColor = opacity * fragColor;
1112
}

0 commit comments

Comments
 (0)