-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
143 lines (113 loc) · 3.46 KB
/
test.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict'
/**
* Test multiple points
*/
require('enable-mobile')
const random = require('gauss-random')
var fit = require('canvas-fit')
var mouseWheel = require('mouse-wheel')
var mouseChange = require('mouse-change')
var createScatter = require('./')
var createPlot = require('gl-plot2d')
var fps = require('fps-indicator')({
position: 'bottom-left'
})
var POINT_COUNT = 1e3
var positions = new Float32Array(2 * POINT_COUNT)
var errors = new Float32Array(4 * POINT_COUNT)
for(var i=0; i<2*POINT_COUNT; ++i) {
positions[i] = random()
errors[i*2] = Math.random()
errors[i*2+1] = Math.random()
}
setup({
positions: positions,
errors: errors,
color: [0,0,.5,.8],
lineWidth: .8,
capSize: 4
})
function setup (options) {
var canvas = document.createElement('canvas')
document.body.appendChild(canvas)
window.addEventListener('resize', fit(canvas, null, +window.devicePixelRatio), false)
var gl = canvas.getContext('webgl', {
depth: false,
// alpha: true,
// premultipliedAlpha: true
})
var aspect = gl.drawingBufferWidth / gl.drawingBufferHeight
var dataBox = [-10,-10/aspect,10,10/aspect]
function makeTicks(lo, hi) {
var result = []
for(var i=lo; i<=hi; ++i) {
result.push({
x: i,
text: i + ''
})
}
return result
}
var plot = createPlot({
gl: gl,
pixelRatio: 1,
borderLineEnable: [false,false,false,false],
labelEnable: [false, false],
tickEnable: [false, false],
gridLineEnable: [false, false],
zeroLineEnable: [false, false],
viewBox: [0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight],
})
var scatter = createScatter(plot, options)
var lastX = 0, lastY = 0
var boxStart = [0,0]
var boxEnd = [0,0]
var boxEnabled = false
mouseChange(function(buttons, x, y, mods) {
y = window.innerHeight - y
x *= plot.pixelRatio
y *= plot.pixelRatio
if(buttons & 1) {
if(mods.shift) {
var dataX = (x - plot.viewBox[0]) / (plot.viewBox[2]-plot.viewBox[0]) * (dataBox[2] - dataBox[0]) + dataBox[0]
var dataY = (y - plot.viewBox[1]) / (plot.viewBox[3]-plot.viewBox[1]) * (dataBox[3] - dataBox[1]) + dataBox[1]
if(!boxEnabled) {
boxStart[0] = dataX
boxStart[1] = dataY
}
boxEnd[0] = dataX
boxEnd[1] = dataY
boxEnabled = true
} else {
var dx = (lastX - x) * (dataBox[2] - dataBox[0]) / (plot.viewBox[2]-plot.viewBox[0])
var dy = (lastY - y) * (dataBox[3] - dataBox[1]) / (plot.viewBox[3] - plot.viewBox[1])
dataBox[0] += dx
dataBox[1] += dy
dataBox[2] += dx
dataBox[3] += dy
plot.setDataBox(dataBox)
}
} else {
var result = plot.pick(x/plot.pixelRatio, y/plot.pixelRatio)
}
lastX = x
lastY = y
})
mouseWheel(function(dx, dy, dz) {
var scale = Math.exp(0.1 * dy / gl.drawingBufferHeight)
var cx = (lastX - plot.viewBox[0]) / (plot.viewBox[2] - plot.viewBox[0]) * (dataBox[2] - dataBox[0]) + dataBox[0]
var cy = (plot.viewBox[1] - lastY) / (plot.viewBox[3] - plot.viewBox[1]) * (dataBox[3] - dataBox[1]) + dataBox[3]
dataBox[0] = (dataBox[0] - cx) * scale + cx
dataBox[1] = (dataBox[1] - cy) * scale + cy
dataBox[2] = (dataBox[2] - cx) * scale + cx
dataBox[3] = (dataBox[3] - cy) * scale + cy
plot.setDataBox(dataBox)
return true
})
function render() {
requestAnimationFrame(render)
plot.draw()
}
render()
return scatter
}