Skip to content

Commit bca811e

Browse files
committed
Add example with two instances
1 parent d905fe6 commit bca811e

2 files changed

Lines changed: 150 additions & 11 deletions

File tree

example/two-instances.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* eslint no-console: 0 */
2+
3+
import createScatterplot from '../src';
4+
5+
const canvas1 = document.querySelector('#canvas');
6+
canvas1.style.width = '50%';
7+
8+
const canvasWrapper = canvas1.parentNode;
9+
10+
const canvas2 = document.createElement('canvas');
11+
canvas2.style.position = 'absolute';
12+
canvas2.style.left = '50%';
13+
canvas2.style.width = '50%';
14+
canvas2.style.height = '100%';
15+
canvasWrapper.appendChild(canvas2);
16+
17+
const numPointsEl = document.querySelector('#num-points');
18+
const numPointsValEl = document.querySelector('#num-points-value');
19+
const pointSizeEl = document.querySelector('#point-size');
20+
const pointSizeValEl = document.querySelector('#point-size-value');
21+
const opacityEl = document.querySelector('#opacity');
22+
const opacityValEl = document.querySelector('#opacity-value');
23+
const resetEl = document.querySelector('#reset');
24+
const exampleEl = document.querySelector('#example-background');
25+
26+
exampleEl.setAttribute('class', 'active');
27+
exampleEl.removeAttribute('href');
28+
29+
[canvas1, canvas2].forEach((canvas) => {
30+
let { width, height } = canvas.getBoundingClientRect();
31+
32+
let points = [];
33+
let numPoints = 25000;
34+
let pointSize = 3;
35+
let opacity = 0.66;
36+
let selection = [];
37+
38+
const selectHandler = ({ points: selectedPoints }) => {
39+
console.log('Selected:', selectedPoints);
40+
selection = selectedPoints;
41+
if (selection.length === 1) {
42+
const point = points[selection[0]];
43+
console.log(
44+
`X: ${point[0]}\nY: ${point[1]}\nCategory: ${point[2]}\nValue: ${point[3]}`
45+
);
46+
}
47+
};
48+
49+
const deselectHandler = () => {
50+
console.log('Deselected:', selection);
51+
selection = [];
52+
};
53+
54+
const scatterplot = createScatterplot({
55+
canvas,
56+
width,
57+
height,
58+
pointSize,
59+
});
60+
61+
console.log(`Scatterplot v${scatterplot.get('version')}`);
62+
63+
scatterplot.subscribe('select', selectHandler);
64+
scatterplot.subscribe('deselect', deselectHandler);
65+
66+
const resizeHandler = () => {
67+
({ width, height } = canvas.getBoundingClientRect());
68+
scatterplot.set({ width, height });
69+
};
70+
71+
window.addEventListener('resize', resizeHandler);
72+
73+
const generatePoints = (num) =>
74+
new Array(num).fill().map(() => [
75+
-1 + Math.random() * 2, // x
76+
-1 + Math.random() * 2, // y
77+
Math.round(Math.random()), // category
78+
Math.random(), // value
79+
]);
80+
81+
const setNumPoint = (newNumPoints) => {
82+
numPoints = newNumPoints;
83+
numPointsEl.value = numPoints;
84+
numPointsValEl.innerHTML = numPoints;
85+
points = generatePoints(numPoints);
86+
scatterplot.draw(points);
87+
};
88+
89+
const numPointsInputHandler = (event) => {
90+
numPointsValEl.innerHTML = `${+event.target
91+
.value} <em>release to redraw</em>`;
92+
};
93+
94+
numPointsEl.addEventListener('input', numPointsInputHandler);
95+
96+
const numPointsChangeHandler = (event) => setNumPoint(+event.target.value);
97+
98+
numPointsEl.addEventListener('change', numPointsChangeHandler);
99+
100+
const setPointSize = (newPointSize) => {
101+
pointSize = newPointSize;
102+
pointSizeEl.value = pointSize;
103+
pointSizeValEl.innerHTML = pointSize;
104+
scatterplot.set({ pointSize });
105+
};
106+
107+
const pointSizeInputHandler = (event) => setPointSize(+event.target.value);
108+
109+
pointSizeEl.addEventListener('input', pointSizeInputHandler);
110+
111+
const setOpacity = (newOpacity) => {
112+
opacity = newOpacity;
113+
opacityEl.value = opacity;
114+
opacityValEl.innerHTML = opacity;
115+
scatterplot.set({ opacity });
116+
};
117+
118+
const opacityInputHandler = (event) => setOpacity(+event.target.value);
119+
120+
opacityEl.addEventListener('input', opacityInputHandler);
121+
122+
const resetClickHandler = () => {
123+
scatterplot.reset();
124+
};
125+
126+
resetEl.addEventListener('click', resetClickHandler);
127+
128+
scatterplot.set({ colorBy: 'category', pointColor: ['#3a78aa', '#aa3a99'] });
129+
130+
setPointSize(pointSize);
131+
setOpacity(opacity);
132+
setNumPoint(numPoints);
133+
});

webpack.config.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,43 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
33
module.exports = (env, argv) => ({
44
entry: {
55
index: './example/index.js',
6-
textureBackground: './example/texture-background.js'
6+
textureBackground: './example/texture-background.js',
7+
twoInstances: './example/two-instances.js',
78
},
89
output: {
910
path: `${__dirname}/docs`,
10-
publicPath: argv.mode === 'production' ? './' : '/'
11+
publicPath: argv.mode === 'production' ? './' : '/',
1112
},
1213
devServer: {
13-
contentBase: './example'
14+
contentBase: './example',
1415
},
1516
module: {
1617
rules: [
1718
{
1819
test: /\.(js|fs|vs)$/,
1920
exclude: /node_modules/,
20-
use: ['babel-loader']
21-
}
22-
]
21+
use: ['babel-loader'],
22+
},
23+
],
2324
},
2425
resolve: {
25-
extensions: ['*', '.js', '.fs', '.vs']
26+
extensions: ['*', '.js', '.fs', '.vs'],
2627
},
2728
plugins: [
2829
new HtmlWebpackPlugin({
2930
template: 'example/index.html',
3031
filename: 'index.html',
31-
chunks: ['index']
32+
chunks: ['index'],
3233
}),
3334
new HtmlWebpackPlugin({
3435
template: 'example/index.html',
3536
filename: 'texture-background.html',
36-
chunks: ['textureBackground']
37-
})
38-
]
37+
chunks: ['textureBackground'],
38+
}),
39+
new HtmlWebpackPlugin({
40+
template: 'example/index.html',
41+
filename: 'two-instances.html',
42+
chunks: ['twoInstances'],
43+
}),
44+
],
3945
});

0 commit comments

Comments
 (0)