mafs
repository·main·Indexed 25 days ago
https://github.com/stevenpetryk/mafsA library of opinionated React components for creating interactive, animated mathematical visualizations. Version 0.21.0 provides tools for rendering Cartesian and Polar coordinate systems, geometric primitives (Line, Circle, Ellipse, Polygon, Polyline), parametric equations, and vector fields. It includes specialized hooks like useStopwatch for animation timing and useMovable for interactive draggable points, as well as support for LaTeX mathematical expressions.
What's inside mafs
- Mafs is a set of opinionated React components designed for creating math visualizations.
Run Mafs tests
mainMafs utilizes unit, end-to-end, and visual regression testing. Visual regression tests compare component screenshots against known baselines. Note that running tests for Safari and iOS Safari may require a Mac.
Use the following commands to manage the test suites:
pnpm test: Runs both unit and end-to-end/visual test suites.pnpm test:unit: Runs only the Jest unit tests.pnpm test:e2e: Runs Playwright (includes end-to-end and visual tests).pnpm test -- --update-snapshots: Updates the visual test baselines.
pnpm test # run both suites pnpm test:unit # to run just the Jest tests pnpm test:e2e # to run Playwright (end-to-end and visual tests) pnpm test -- --update-snapshots # to update the visual test baselinesSet up the Mafs development environment
mainTo develop with Mafs, you can run the Next.js documentation site which imports components directly from
src/. Ensure you usepnpmas specified in thepackageManagerfield ofpackage.json. It is recommended to usecorepackto managepnpmversions automatically.Follow these steps to start the development server:
- Install dependencies using
pnpm install. - Start the server using
pnpm start. - Access the documentation site at
http://localhost:3000.
pnpm install pnpm start- Install dependencies using
Configure Mafs CSS custom properties
mainThe
.MafsViewcomponent uses CSS custom properties (variables) for theming and styling. You can override these variables in your own CSS to customize the appearance of the charts, including background colors, line colors, and specific color palettes used for data points.Key variables include:
--mafs-bg: Background color (default:black).--mafs-fg: Foreground/text color (default:white).--mafs-line-color: Color for grid/axis lines (default:#555).--mafs-line-stroke-dash-style: Dash pattern for lines (default:4, 3).--mafs-axis-stroke-width: Width of axis strokes (default:1px).--grid-line-subdivision-color: Color for grid subdivisions (default:#222).
Predefined color palette variables:
--mafs-red,--mafs-orange,--mafs-yellow,--mafs-green,--mafs-blue,--mafs-indigo,--mafs-violet,--mafs-pink.
Enable panning and zooming in Mafs
mainYou can control user interactions via the
panandzoomprops:pan: A boolean. Iftrue, enables panning with the mouse and keyboard.zoom: Can be abooleanor an object{ min: number; max: number }to set scale limits.minmust be in the range(0, 1].maxmust be in the range[1, ∞).
When
zoomis a boolean, it defaults to a range of0.5to5.Enable debug mode in Mafs
mainSetting thedebugprop totruehelps visualize the boundaries of yourviewBox. It renders a red outline around the viewport and displays viewport information.Configure the viewBox and aspect ratio in Mafs
mainUse the
viewBoxandpreserveAspectRatioprops to define the visible area of your coordinate system.viewBox: Defines the "area of interest". It accepts an object withxandyarrays (representing[min, max]) and an optionalpaddingnumber.preserveAspectRatio: Determines how the graph fills the viewport.- `
Configure Mafs props for layout and interaction
mainThe
Mafscomponent accepts several props to control its behavior:width: The width of the viewport. Can be anumberor `
Convert a matrix to a CSS transform string
mainUsetoCSS(matrix)to convert aMatrixinto a valid CSSmatrix(...)string for use in element styles.Use the `vec` namespace for 2D vector and matrix math
mainThevecnamespace provides a suite of linear algebra functions for 2D operations. It includes types forVector2(a tuple of two numbers[x, y]) andMatrix(a 2x3 representation of a 3x3 matrix used for 2D transformations).Configure VectorFieldProps
mainThe
VectorFieldcomponent accepts the following props:xy: A function(point: vec.Vector2) => vec.Vector2that defines the vector at a specific coordinate.vec.Vector2is an array of two numbers[x, y].xyOpacity?: An optional function(point: vec.Vector2) => numberthat returns an opacity value (0 to 1) for the vector at a specific coordinate.step: A number defining the granularity of the grid (the distance between vector points). Defaults to1.opacityStep?: An optional number that controls the granularity of opacity rendering. A smalleropacityStepincreases fidelity but may impact performance. Defaults to1ifxyOpacityis not provided, or0.2if it is.color?: A string representing the color of the vectors. Defaults toTheme.foreground.
Build matrices using `matrixBuilder`
mainThe
matrixBuilder()function returns a builder object that allows you to chain transformations to create a singleMatrix. You can start with an existing matrix or use the default identity matrix.Available builder methods:
translate(x, y): Applies a translation.rotate(a): Applies a rotation by anglea(radians).scale(x, y): Applies scaling.shear(x, y): Applies a shear transformation.mult(m): Multiplies the current matrix by another matrixm.get(): Returns the finalMatrix.