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 size = useComponentResizeListener(canvasRef);
const draw = (bigOrders: BigOrder[]) => { if (null === canvasRef || !Array.isArray(bigOrders)) return;
const context = canvasRef.current?.getContext(“2d”);
const rowsOnScreenCount = cupTools.getRowsCountOnScreen( canvasSize.height, bigOrdersOptions(darkMode).cell.defaultHeight * dpiScale, ); const realCellHeight = parseInt((canvasSize.height / rowsOnScreenCount).toFixed(0));
if (context) { bigOrdersDrawer.clear(context, canvasSize); bigOrdersDrawer.drawChain( context, canvasSize, bigOrders, realCellHeight, dpiScale, ); } }; useEffect(() => { if (!workerRef.current) return; let animationFrameId: number|null = null;
interface UpdateBigOrderEvent { type: “update_big_order”, bigOrders: BigOrder[], }
workerRef.current.onmessage = (event: MessageEvent) => { if (event?.data?.type === “update_big_order”) { if (null !== animationFrameId) { cancelAnimationFrame(animationFrameId); }
animationFrameId = requestAnimationFrame(() => draw(event.data.bigOrders)); } };
return () => { if (null !== animationFrameId) { cancelAnimationFrame(animationFrameId); } }; }, [workerRef.current, canvasSize, darkMode, dpiScale]); useEffect(() => { if (!size) { return; }
setCanvasSize({ width: Math.floor(size.width) * dpiScale, height: Math.floor(size.height) * dpiScale, }); }, [dpiScale, size]); return
export default BigOrders;
import {BigOrder} from “…/…/…/workers/big-order-builder”; import {CanvasSize} from “./BigOrders”; import bigOrdersOptions from “./BigOrders.options”;
const bigOrdersDrawer = { clear: (ctx: CanvasRenderingContext2D, size: CanvasSize) => { ctx.clearRect(0, 0, size.width, size.height); }, drawChain: ( ctx: CanvasRenderingContext2D, canvasSize: CanvasSize, bigOrders: BigOrder[], cellHeight: number, dpiScale: number, ) => { bigOrders.forEach((order: BigOrder, index: number) => { const yPosition = cellHeight * index; bigOrdersDrawer.drawSymbol( ctx, order.symbol, yPosition, cellHeight, canvasSize.width, dpiScale, true ); drawSymbol: ( ctx: CanvasRenderingContext2D, symbol: string, yPosition: number, cellHeight: number, fullWidth: number, dpiScale: number, darkMode: boolean, ) => { ctx.textBaseline = “middle”; ctx.font = {bigOrdersOptions().font.sizePx * dpiScale}px {bigOrdersOptions().font.color}; ctx.fillText( symbol, 0, yPosition + cellHeight / 2, ); },
- Assistant
To make the canvas scrollable, you can wrap it inside a parent div container with a fixed height and set the CSS property
overflow: auto;. This will create a scrollable area for the canvas.Here's the updated code:
codereturn ( <div ref={containerRef} className={styles.canvasWrapper} style={{ height: canvasSize.height }}> <canvas ref={canvasRef} className={styles.canvas} width={canvasSize?.width} height={canvasSize?.height} /> </div> );And in the CSS file associated with the component:
css.canvasWrapper { height: 200px; /* or any fixed height you desire */ overflow: auto; }This will ensure that the canvas is contained within a scrollable area, allowing you to scroll the canvas vertically if its content exceeds the fixed height.
- User
да ниху так не рабоатет
- Assistant