const OrderFeed = ({workerRef, symbol, settings}: OrderFeedProps) => { const cupParams =…
- User
const OrderFeed = ({workerRef, symbol, settings}: OrderFeedProps) => { const cupParams = useSelector((state: AppState) => state.cupSlice); 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 = (trades: TradeItem[], camera: number) => { if (null === canvasRef || !Array.isArray(trades)) return;
const context = canvasRef.current?.getContext("2d"); if (context) { orderFeedDrawer.clear(context, canvasSize); orderFeedDrawer.drawLine( context, canvasSize, trades, camera, cupParams.aggregatedPriceStep, cupParams.cellHeight, cupParams.quantityDivider, !settings.minQuantity ? 0 : parseFloat(settings.minQuantity), dpiScale, theme, );} };
useEffect(() => { if (!workerRef.current) return; draw(event.data.trades, event.data.camera) }, [workerRef.current, canvasRef.current ]);
return <Box ref={containerRef} className={
${styles.canvasWrapper} scroll-block}> ; };export default OrderFeed;
const clamp = (value: number, min: number, max: number): number => { return Math.min(Math.max(value, min), max); }; const orderFeedDrawer = { clear: (ctx: CanvasRenderingContext2D, size: CanvasSize) => { ctx.clearRect(0, 0, size.width, size.height); }, drawLine: ( ctx: CanvasRenderingContext2D, canvasSize: CanvasSize, trades: TradeItem[], camera: number, aggregatedPriceStep: number, cupCellHeight: number, quantityDivider: number, minQuantity: number, dpiScale: number, theme: Theme, ) => { let startX = canvasSize.width - 10 * dpiScale; const fullHeightRowsCount = parseInt((canvasSize.height / cupCellHeight).toFixed(0)); const startY = (fullHeightRowsCount % 2 ? fullHeightRowsCount - 1 : fullHeightRowsCount) / 2 * cupCellHeight; const halfCupCellHeight = cupCellHeight / 2;
ctx.beginPath(); ctx.strokeStyle = orderFeedOptions(theme).line.color; ctx.lineWidth = 2 * dpiScale; trades.forEach(trade => { const yModifier = (trade.price - (trade.price - (trade?.secondPrice || trade.price)) / 2 - camera) / aggregatedPriceStep * cupCellHeight + halfCupCellHeight; const width = 1.75 * clamp((trade.quantity / quantityDivider < minQuantity ? 0 : trade.radius), 5, 50) * dpiScale; ctx.lineTo(startX - width / 2, startY - yModifier); startX = startX - width; }); ctx.stroke();}, };
export default orderFeedDrawer;
https://www.npmjs.com/package/react-pixi-fiber нужно переписать отрисовку линий через библиотеку react-pixi-fiber
- Assistant
- User
нету draw в Graphics
- Assistant
I apologize for the confusion. The
drawprop does not exist in theGraphicscomponent provided byreact-pixi-fiber. Instead, you can use thedrawfunction provided byreact-pixi-fiberto define a custom rendering function for drawing the line.Here's an updated version of the
OrderFeedLinecomponent using thedrawfunction:import { Graphics } from "react-pixi-fiber"; import { useApp } from "react-pixi-fiber/lib/PixiAppProvider"; const OrderFeedLine = ({ startX, startY, trade, aggregatedPriceStep, cupCellHeight, quantityDivider, minQuantity, dpiScale, theme, }) => { const { stage } = useApp(); const yModifier = (trade.price - (trade.price - (trade?.secondPrice || trade.price)) / 2 - camera) / aggregatedPriceStep * cupCellHeight + halfCupCellHeight; const width = 1.75 * clamp((trade.quantity / quantityDivider < minQuantity ? 0 : trade.radius), 5, 50) * dpiScale; const customDraw = (g) => { g.lineStyle(2 * dpiScale, orderFeedOptions(theme).line.color); g.moveTo(startX - width / 2, startY - yModifier); g.lineTo(...); // continue drawing the line }; // Add the custom rendering function to the PIXI stage stage.addChild(<Graphics draw={customDraw} />); return null; // The OrderFeedLine component does not need to render anything }; export default OrderFeedLine;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.