Skip to content

Commit

Permalink
Fix VectorFieldProps, add tests (#33)
Browse files Browse the repository at this point in the history
* Fix type signature and add tests for VectorField

* Add a test for opacity

* Add color to the test
  • Loading branch information
stevenpetryk authored Sep 8, 2021
1 parent f17797c commit ffed384
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/display/VectorField.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react"
import { Theme, VectorField } from ".."
import renderToImage from "../tests/renderToImage"

describe("<VectorField />", () => {
it("Renders", async () => {
const a = { x: 5.1, y: 5.1 }
expect(
await renderToImage(
<VectorField xy={(x, y) => [y - a.y - (x - a.x), -(x - a.x) - (y - a.y)]} step={0.5} />
)
).toMatchImageSnapshot()
})

it("Supports opacity functions", async () => {
expect(
await renderToImage(
<VectorField
xy={() => [0.5, 0.5]}
step={1}
xyOpacity={(x, y) => Math.min(Math.sqrt(x ** 2 + y ** 2) / 10, 1)}
color={Theme.red}
/>
)
).toMatchImageSnapshot()
})
})
9 changes: 4 additions & 5 deletions src/display/VectorField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import clamp from "lodash.clamp"
import React from "react"
import * as vec from "vec-la"

import { Vector2 } from "../math"
import { usePaneContext } from "../view/PaneManager"
import { useScaleContext } from "../view/ScaleContext"
import { theme } from "./Theme"

export interface VectorFieldProps {
xy: (x: number, y: number) => [number, number]
xyOpacity: (x: number, y: number) => number
xyOpacity?: (x: number, y: number) => number
step: number
opacityStep: number
opacityStep?: number
color?: string
}

export type Vector = [number, number]

const xyOpacityDefault = () => 1

const VectorField: React.VFC<VectorFieldProps> = ({
Expand All @@ -38,7 +37,7 @@ const VectorField: React.VFC<VectorFieldProps> = ({
function fieldForRegion(xMin: number, xMax: number, yMin: number, yMax: number) {
for (let x = Math.floor(xMin); x <= Math.ceil(xMax); x += step) {
for (let y = Math.floor(yMin); y <= Math.ceil(yMax); y += step) {
const tail: Vector = [x, y]
const tail: Vector2 = [x, y]
const trueOffset = xy(x, y)
const trueMag = vec.mag(trueOffset)
const scaledOffset = vec.scale(vec.norm(trueOffset), Math.min(trueMag, step * 0.75))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ffed384

Please sign in to comment.