Skip to content

Commit 45201a7

Browse files
committed
Add background to lasso
1 parent 7a0608f commit 45201a7

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## v0.7.7
1+
## v0.8.0
22

3+
- Add background to lasso
34
- Fix horizontally-flipped background image (#13)
45
- Rename the `background` property to `backgroundColor` for clarity
56
- Split the `colors` property into `pointColor`, `pointColorActive`, and `pointColorHover` for clarity

src/index.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const createScatterplot = (initialProperties = {}) => {
129129
let lassoMinDelay = +initialLassoMinDelay;
130130
let lassoMinDist = +initialLassoMinDist;
131131
let lassoPos = [];
132+
let lassoPoints = [];
132133
let lassoScatterPos = [];
133134
let lassoPrevMousePos;
134135
let searchIndex;
@@ -250,14 +251,18 @@ const createScatterplot = (initialProperties = {}) => {
250251
const currMousePos = getMousePos();
251252

252253
if (!lassoPrevMousePos) {
253-
lassoPos = [...getMouseGlPos(currMousePos)];
254+
const point = getMouseGlPos(currMousePos);
255+
lassoPos = [point[0], point[1]];
256+
lassoPoints = [point];
254257
lassoScatterPos = [...getScatterGlPos(currMousePos)];
255258
lassoPrevMousePos = currMousePos;
256259
} else {
257260
const d = dist(...currMousePos, ...lassoPrevMousePos);
258261

259262
if (d > lassoMinDist) {
260-
lassoPos.push(...getMouseGlPos(currMousePos));
263+
const point = getMouseGlPos(currMousePos);
264+
lassoPos.push(point[0], point[1]);
265+
lassoPoints.push(point);
261266
lassoScatterPos.push(...getScatterGlPos(currMousePos));
262267
lassoPrevMousePos = currMousePos;
263268
if (lassoPos.length > 2) {
@@ -322,6 +327,7 @@ const createScatterplot = (initialProperties = {}) => {
322327
// console.log(`found ${pointsInLasso.length} in ${performance.now() - t0} msec`);
323328
select(pointsInLasso);
324329
lassoPos = [];
330+
lassoPoints = [];
325331
lassoScatterPos = [];
326332
lassoPrevMousePos = undefined;
327333
lasso.clear();
@@ -680,6 +686,47 @@ const createScatterplot = (initialProperties = {}) => {
680686
count: 6,
681687
});
682688

689+
const drawPolygon2d = regl({
690+
vert: `
691+
precision mediump float;
692+
attribute vec2 position;
693+
void main () {
694+
gl_Position = vec4(position, 0, 1);
695+
}`,
696+
697+
frag: `
698+
precision mediump float;
699+
uniform vec4 color;
700+
void main () {
701+
gl_FragColor = vec4(color.rgb, 0.2);
702+
}`,
703+
704+
depth: { enable: false },
705+
706+
blend: {
707+
enable: true,
708+
func: {
709+
srcRGB: 'src alpha',
710+
srcAlpha: 'one',
711+
dstRGB: 'one minus src alpha',
712+
dstAlpha: 'one minus src alpha',
713+
},
714+
},
715+
716+
attributes: {
717+
position: () => lassoPoints,
718+
},
719+
720+
uniforms: {
721+
color: () => lassoColor,
722+
},
723+
724+
elements: () =>
725+
Array(lassoPoints.length - 2)
726+
.fill()
727+
.map((_, i) => [0, i + 1, i + 2]),
728+
});
729+
683730
const drawRecticle = () => {
684731
if (!(hoveredPoint >= 0)) return;
685732

@@ -797,6 +844,8 @@ const createScatterplot = (initialProperties = {}) => {
797844
if (hoveredPoint >= 0) drawHoveredPoint();
798845
if (selection.length) drawSelectedPoint();
799846

847+
if (lassoPoints.length > 2) drawPolygon2d();
848+
800849
lasso.draw();
801850

802851
// Publish camera change

0 commit comments

Comments
 (0)