quadratic icon indicating copy to clipboard operation
quadratic copied to clipboard

Drawing Grid Lines Dynamically

Open davidkircos opened this issue 3 years ago • 2 comments

The code that renders grid lines generates a static number of lines, many of which are not visible at any given point.

https://github.com/quadratichq/quadratic/blob/main/src/core/gridGL/graphics/drawGridLines.ts

Rendering grid lines should be dynamic based on the location of the viewport.

Ex grid lines stop being rendered at y = 500 Screen Shot 2022-08-11 at 10 55 50 AM

davidkircos avatar Aug 11 '22 16:08 davidkircos

Hi, I tried an interesting solution for this issue. Instead of rerendering the lines, I moved the grid around!

// moves the grid around to match the viewport
export const moveGrid  = function (viewport:  Viewport, grid:  Graphics)  {
	const offsets  =  getOffsets(viewport)
	grid.transform.position.set(offsets.x,  offsets.y);
}
// Quadratic Render Loop, render when dirty.
// Remember when anything changes on the stage to either set viewport.dirty = true
// Or use a react component that is a child of viewport (React Pixi will trigger a render)

ticker.add(
	()  =>  {
		if  (viewport.dirty)  {
		    // render
		    props.app.renderer.render(props.app.stage);
		    viewport.dirty  =  false;
		    moveGrid(viewport,  grid_ui);
		}
	},
	null,
	PIXI.UPDATE_PRIORITY.LOW  // unsure why but this is recommended to be LOW https://pixijs.download/dev/docs/PIXI.html#UPDATE_PRIORITY
);

I am not sure if there is a performance hit because its moving the grid around everytime the viewport changes but it seems to work. Let me know if I should do a pull request if you'd like to check the code.

Light2Dark avatar Aug 15 '22 05:08 Light2Dark

Hey @Light2Dark , it would be great to try out your solution. Could you please create a PR?

davidkircos avatar Aug 15 '22 15:08 davidkircos