element-call/src/GridDemo.jsx

353 lines
9 KiB
React
Raw Normal View History

2021-08-11 16:02:40 -07:00
import React, { useCallback, useEffect, useRef, useState } from "react";
import { useDrag } from "react-use-gesture";
2021-08-16 12:57:33 -07:00
import { useSprings, animated, useSpring } from "@react-spring/web";
2021-08-11 16:02:40 -07:00
import styles from "./GridDemo.module.css";
2021-08-12 16:25:10 -07:00
import useMeasure from "react-use-measure";
2021-08-11 16:02:40 -07:00
2021-08-13 16:27:19 -07:00
function isInside([x, y], dragTile, targetTile) {
const cursorX = dragTile.x + x;
const cursorY = dragTile.y + y;
const left = targetTile.x;
const top = targetTile.y;
const bottom = targetTile.y + targetTile.height;
const right = targetTile.x + targetTile.width;
if (cursorX < left || cursorX > right || cursorY < top || cursorY > bottom) {
return false;
}
return true;
}
2021-08-11 16:02:40 -07:00
export function GridDemo() {
2021-08-12 16:25:10 -07:00
const tileKey = useRef(0);
2021-08-11 16:02:40 -07:00
const [stream, setStream] = useState();
2021-08-13 13:02:24 -07:00
const [{ tiles, tilePositions }, setTileState] = useState({
tiles: [],
tilePositions: [],
});
2021-08-13 12:27:00 -07:00
const draggingTileRef = useRef(null);
2021-08-11 16:02:40 -07:00
2021-08-13 16:27:19 -07:00
// Contains tile indices
// Tiles are displayed in the order that they appear
const tileOrderRef = useRef([]);
2021-08-12 16:25:10 -07:00
const [gridRef, gridBounds] = useMeasure();
2021-08-13 13:02:24 -07:00
const getTilePositions = useCallback((tiles, gridBounds) => {
const newTilePositions = [];
const tileCount = tiles.length;
const { width: gridWidth, height: gridHeight } = gridBounds;
const gap = 8;
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
if (tileCount > 0) {
const aspectRatio = gridWidth / gridHeight;
2021-08-12 16:25:10 -07:00
2021-08-13 13:02:24 -07:00
let columnCount, rowCount;
2021-08-12 16:25:10 -07:00
2021-08-13 13:02:24 -07:00
if (aspectRatio < 1) {
if (tileCount <= 4) {
columnCount = 1;
rowCount = tileCount;
} else if (tileCount <= 12) {
columnCount = 2;
rowCount = Math.ceil(tileCount / 2);
}
} else {
if (tileCount === 1) {
columnCount = 1;
rowCount = 1;
} else if (tileCount === 2) {
columnCount = 2;
rowCount = 1;
} else if (tileCount <= 4) {
columnCount = 2;
rowCount = 2;
} else if (tileCount <= 6) {
columnCount = 3;
rowCount = 2;
} else if (tileCount <= 8) {
columnCount = 4;
rowCount = 2;
} else if (tileCount <= 10) {
columnCount = 5;
rowCount = 2;
} else if (tileCount <= 12) {
columnCount = 4;
rowCount = 3;
}
}
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
let tileHeight = Math.round(
(gridHeight - gap * (rowCount + 1)) / rowCount
);
let tileWidth = Math.round(
(gridWidth - gap * (columnCount + 1)) / columnCount
);
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
const tileAspectRatio = tileWidth / tileHeight;
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
if (tileAspectRatio > 16 / 9) {
tileWidth = (16 * tileHeight) / 9;
}
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
for (let i = 0; i < tiles.length; i++) {
const verticalIndex = Math.floor(i / columnCount);
const top = verticalIndex * tileHeight + (verticalIndex + 1) * gap;
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
let rowItemCount;
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
if (verticalIndex + 1 === rowCount && tileCount % rowCount !== 0) {
rowItemCount = Math.floor(tileCount / rowCount);
2021-08-12 16:25:10 -07:00
} else {
2021-08-13 13:02:24 -07:00
rowItemCount = Math.ceil(tileCount / rowCount);
2021-08-12 16:25:10 -07:00
}
2021-08-13 13:02:24 -07:00
const horizontalIndex = i % columnCount;
const totalRowGapWidth = (rowItemCount + 1) * gap;
const totalRowTileWidth = rowItemCount * tileWidth;
const rowLeftMargin = Math.round(
(gridWidth - (totalRowTileWidth + totalRowGapWidth)) / 2
2021-08-12 16:25:10 -07:00
);
2021-08-13 13:02:24 -07:00
const left =
tileWidth * horizontalIndex +
rowLeftMargin +
(horizontalIndex + 1) * gap;
newTilePositions.push({
width: tileWidth,
height: tileHeight,
x: left,
y: top,
});
}
}
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
return newTilePositions;
}, []);
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
const startWebcam = useCallback(async () => {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
setStream(stream);
2021-08-13 16:27:19 -07:00
tileOrderRef.current.push(tileOrderRef.current.length);
2021-08-13 13:02:24 -07:00
setTileState(() => {
2021-08-16 12:57:33 -07:00
const tiles = [{ stream, key: tileKey.current++, remove: false }];
2021-08-13 13:02:24 -07:00
const tilePositions = getTilePositions(tiles, gridBounds);
return { tiles, tilePositions };
});
}, [gridBounds]);
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
const addTile = useCallback(() => {
const newStream = stream.clone();
2021-08-13 12:27:00 -07:00
2021-08-13 16:27:19 -07:00
tileOrderRef.current.push(tileOrderRef.current.length);
2021-08-13 13:02:24 -07:00
setTileState(({ tiles }) => {
const newTiles = [
...tiles,
2021-08-16 12:57:33 -07:00
{ stream: newStream, key: tileKey.current++, remove: false },
2021-08-13 13:02:24 -07:00
];
const tilePositions = getTilePositions(newTiles, gridBounds);
return { tiles: newTiles, tilePositions };
});
}, [stream, gridBounds]);
2021-08-13 12:27:00 -07:00
2021-08-16 12:57:33 -07:00
const removeTile = useCallback(
(tile) => {
const tileKey = tile.key;
setTileState(({ tiles, tilePositions }) => {
return {
tiles: tiles.map((tile) => ({
...tile,
remove: tile.key === tileKey,
})),
tilePositions,
};
});
setTimeout(() => {
setTileState(({ tiles }) => {
const newTiles = tiles.filter((t) => t.key !== tileKey);
const tilePositions = getTilePositions(newTiles, gridBounds);
return { tiles: newTiles, tilePositions };
});
}, 250);
},
[gridBounds]
);
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
useEffect(() => {
setTileState(({ tiles }) => ({
tiles,
tilePositions: getTilePositions(tiles, gridBounds),
}));
}, [gridBounds]);
2021-08-13 12:27:00 -07:00
2021-08-13 13:02:24 -07:00
const animate = useCallback(
2021-08-13 16:27:19 -07:00
(order) => (index) => {
const tileIndex = order[index];
const tilePosition = tilePositions[tileIndex];
2021-08-13 12:27:00 -07:00
const draggingTile = draggingTileRef.current;
const dragging =
2021-08-13 16:27:19 -07:00
draggingTileRef.current && tileIndex === draggingTileRef.current.index;
2021-08-13 12:27:00 -07:00
if (dragging) {
return {
width: tilePosition.width,
height: tilePosition.height,
x: tilePosition.x + draggingTile.x,
y: tilePosition.y + draggingTile.y,
scale: 1.1,
zIndex: 1,
shadow: 15,
immediate: (key) => key === "zIndex" || key === "x" || key === "y",
};
} else {
return {
...tilePosition,
scale: 1,
zIndex: 0,
shadow: 1,
immediate: false,
2021-08-12 16:25:10 -07:00
};
}
2021-08-13 12:27:00 -07:00
},
2021-08-13 13:02:24 -07:00
[tilePositions]
2021-08-13 12:27:00 -07:00
);
2021-08-12 16:25:10 -07:00
2021-08-13 16:27:19 -07:00
const [springs, api] = useSprings(
tiles.length,
animate(tileOrderRef.current),
[tilePositions]
);
const bind = useDrag(({ args: [index], active, movement }) => {
let order = tileOrderRef.current;
let dragIndex = index;
// const tileIndex = tileOrderRef.current[index];
// const tilePosition = tilePositions[tileIndex];
// for (let i = 0; i < tileOrderRef.current.length; i++) {
// if (i === index) {
// continue;
// }
// const hoverTileIndex = tileOrderRef.current[i];
// const hoverTilePosition = tilePositions[hoverTileIndex];
// if (isInside(movement, tilePosition, hoverTilePosition)) {
// order = [...tileOrderRef.current];
// const [toBeMoved] = order.splice(i, 1);
// order.splice(index, 0, toBeMoved);
// dragIndex = i;
// break;
// }
// }
2021-08-13 12:27:00 -07:00
if (active) {
draggingTileRef.current = {
2021-08-13 16:27:19 -07:00
index: dragIndex,
x: movement[0],
y: movement[1],
2021-08-12 16:25:10 -07:00
};
2021-08-13 12:27:00 -07:00
} else {
draggingTileRef.current = null;
2021-08-13 16:27:19 -07:00
//tileOrderRef.current = order;
2021-08-13 12:27:00 -07:00
}
2021-08-13 16:27:19 -07:00
api.start(animate(order));
2021-08-11 16:02:40 -07:00
});
return (
<div className={styles.gridDemo}>
<div className={styles.buttons}>
{!stream && <button onClick={startWebcam}>Start Webcam</button>}
2021-08-13 16:27:19 -07:00
{stream && tiles.length < 12 && (
<button onClick={addTile}>Add Tile</button>
)}
{stream && tiles.length > 0 && (
2021-08-16 12:57:33 -07:00
<button onClick={() => removeTile(tiles[tiles.length - 1])}>
Remove Tile
</button>
2021-08-13 16:27:19 -07:00
)}
2021-08-11 16:02:40 -07:00
</div>
2021-08-12 16:25:10 -07:00
<div className={styles.grid} ref={gridRef}>
2021-08-13 13:05:21 -07:00
{springs.map(({ shadow, ...style }, i) => {
2021-08-13 16:27:19 -07:00
const tileIndex = tileOrderRef.current[i];
const tile = tiles[tileIndex];
2021-08-12 16:25:10 -07:00
return (
<ParticipantTile
{...bind(i)}
key={tile.key}
2021-08-13 13:05:21 -07:00
style={{
boxShadow: shadow.to(
(s) => `rgba(0, 0, 0, 0.5) 0px ${s}px ${2 * s}px 0px`
),
...style,
}}
2021-08-12 16:25:10 -07:00
{...tile}
/>
);
})}
2021-08-11 16:02:40 -07:00
</div>
</div>
);
}
2021-08-16 12:57:33 -07:00
function ParticipantTile({
style,
stream,
remove,
finishRemovingTile,
tileKey,
...rest
}) {
2021-08-11 16:02:40 -07:00
const videoRef = useRef();
useEffect(() => {
if (stream) {
videoRef.current.srcObject = stream;
videoRef.current.play();
} else {
videoRef.current.srcObject = null;
}
}, [stream]);
2021-08-16 12:57:33 -07:00
const [springStyles, api] = useSpring(() => ({
from: {
scale: 0,
opacity: 0,
},
to: {
scale: 1,
opacity: 1,
},
}));
useEffect(() => {
if (remove) {
api.start({
scale: 0,
opacity: 0,
});
}
}, [remove]);
2021-08-11 16:02:40 -07:00
return (
2021-08-16 12:57:33 -07:00
<animated.div
className={styles.participantTile}
style={{
...style,
...springStyles,
}}
{...rest}
>
2021-08-11 16:02:40 -07:00
<video ref={videoRef} playsInline />
</animated.div>
);
}