const BigOrders = ({workerRef, symbol}: BigOrdersProps) => { const [dpiScale,…
- User
const BigOrders = ({workerRef, symbol}: BigOrdersProps) => { const [dpiScale, setDpiScale] = useState(Math.ceil(window.devicePixelRatio)); const [canvasSize, setCanvasSize] = useState({height: 0, width: 0}); const containerRef = useRef<HTMLDivElement|null>(null); const canvasRef = useRef<HTMLCanvasElement|null>(null);
const precision = useSelector((state: AppState) => state.binancePrecision.futures);
const {symbols} = useBinanceSymbols();
const cupSubscribe = useCallback(async(symbol: string, precision: {[key: string]: Precision}, symbols: string[]) => { workerRef.current?.postMessage(JSON.stringify({type: "subscribe", symbol, precision, symbols})); }, []);
const cupUnsubscribe = useCallback(async(pair: string) => { workerRef.current?.postMessage(JSON.stringify({type: "unsubscribe", symbol})); }, []);
const wheelHandler = (e: WheelEvent) => { e.preventDefault(); workerRef.current?.postMessage(JSON.stringify({type: e.deltaY < 0 ? "camera_up" : "camera_down"})); };
useEffect(() => { workerRef.current = new Worker(new URL("/workers/big-order-builder.ts", import.meta.url)); canvasRef.current?.addEventListener("wheel", wheelHandler, {passive: false});
return () => { workerRef.current?.terminate(); canvasRef.current?.removeEventListener("wheel", wheelHandler); };}, []);
useEffect(() => { if (!workerRef.current) return; let animationFrameId: number|null = null;
interface UpdateBigOrderEvent { type: “update_big_order”, id: number, symbol: string, price: number, quantity: number, pricePrecision: number, quantityPrecision: number, } workerRef.current.onmessage = (event: MessageEvent) => { // console.log(event?.data?.id);
if (event?.data?.type === “update_big_order”) { if (null !== animationFrameId) { // console.log(animationFrameId);
cancelAnimationFrame(animationFrameId); }
animationFrameId = requestAnimationFrame(() => { const data = event.data as UpdateBigOrderEvent; // console.log(data);
const context = canvasRef.current?.getContext(“2d”);
if (context) { // cupDrawer.clear(context, canvasSize);
const rowsOnScreenCount = cupTools.getRowsCountOnScreen( canvasSize.height, bigOrdersOptions(darkMode).cell.defaultHeight * dpiScale, ); const realCellHeight = parseInt((canvasSize.height / rowsOnScreenCount).toFixed(0));
if (data.price !== 0) { bigOrdersDrawer.draw( context, canvasSize, dpiScale, data.id, data.symbol, data.price, data.quantity, data.pricePrecision, data.quantityPrecision, realCellHeight, ); } } }); } };
return () => { if (null !== animationFrameId) { cancelAnimationFrame(animationFrameId); } }; }, [workerRef.current, canvasSize, darkMode, dpiScale, isLoaded]); return
const bigOrdersDrawer = { clear: (ctx: CanvasRenderingContext2D, size: CanvasSize) => { ctx.clearRect(0, 0, size.width, size.height); }, draw: ( ctx: CanvasRenderingContext2D, size: CanvasSize, dpiScale: number, id: number, symbol: string, price: number, quantity: number, pricePrecision: number, quantityPrecision: number, cellHeight: number, ) => { const quantityDivider = Math.pow(10, quantityPrecision); const priceDivider = Math.pow(10, pricePrecision);
- Assistant
Чтобы реализовать скроллинг вниз при отрисовке новых данных в канвасе, вам понадобится следующий подход:
- Добавьте состояние
scrollTopв компоненте для отслеживания текущего значения прокрутки.
- Добавьте состояние