Skip to content

Commit 75a8e77

Browse files
committed
more cleanup; guard unavailable effect
1 parent 2085558 commit 75a8e77

File tree

5 files changed

+28
-42
lines changed

5 files changed

+28
-42
lines changed

src/common/geometry.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ export const angle = (point) => {
113113
return Math.atan2(point.y, point.x)
114114
}
115115

116+
// Convert cartesian to polar coordinates
117+
export const toPolar = (x, y) => ({
118+
r: Math.sqrt(x * x + y * y),
119+
theta: Math.atan2(y, x),
120+
})
121+
116122
// returns whether a point is on the segment defined by start and end
117123
export const onSegment = (start, end, point) => {
118124
return (

src/features/effects/Warp.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Victor from "victor"
44
import Effect from "./Effect"
55
import { effectOptions } from "./EffectLayer"
6-
import { subsample, circle } from "@/common/geometry"
6+
import { subsample, circle, toPolar } from "@/common/geometry"
77
import { evaluate } from "mathjs"
88

99
const options = {
@@ -161,21 +161,10 @@ export default class Warp extends Effect {
161161
return vertices.map((vertex) => {
162162
const originalx = vertex.x - effect.x
163163
const originaly = vertex.y - effect.y
164-
const theta = Math.atan2(originaly, originalx)
165-
const x =
166-
originalx +
167-
scale *
168-
Math.cos(theta) *
169-
Math.cos(
170-
Math.sqrt(originalx * originalx + originaly * originaly) / periodx,
171-
)
172-
const y =
173-
originaly +
174-
scale *
175-
Math.sin(theta) *
176-
Math.cos(
177-
Math.sqrt(originalx * originalx + originaly * originaly) / periody,
178-
)
164+
const { r, theta } = toPolar(originalx, originaly)
165+
const x = originalx + scale * Math.cos(theta) * Math.cos(r / periodx)
166+
const y = originaly + scale * Math.sin(theta) * Math.cos(r / periody)
167+
179168
return new Victor(x + effect.x, y + effect.y)
180169
})
181170
}

src/features/effects/effectFactory.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export const getEffect = (type, ...args) => {
2929
}
3030

3131
export const getDefaultEffectType = () => {
32-
return localStorage.getItem("defaultEffect") || "mask"
32+
const effect = localStorage.getItem("defaultEffect")
33+
34+
// validate that the effect type exists (it may not if switching branches)
35+
return effect && effectFactory[effect] ? effect : "mask"
3336
}
3437

3538
export const getDefaultEffect = () => {

src/features/shapes/chladni/helpers.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const getTrig = (boundary) => (boundary === "free" ? Math.cos : Math.sin)
2+
13
// Default values for superposition modes
24
export const defaultTerms = ({ term1, term2 }) => [term1, term2]
35
export const defaultScale = { rectangular: 1, circular: 2.5 }
@@ -18,12 +20,3 @@ export function getEffectiveValue(
1820

1921
return typeof v === "function" ? v(value) / divisor : v * (value / divisor)
2022
}
21-
22-
// Convert cartesian to polar, returning null if outside unit circle
23-
export function toPolar(x, y) {
24-
const r = Math.sqrt(x * x + y * y)
25-
26-
if (r > 1) return null
27-
28-
return { r, theta: Math.atan2(y, x) }
29-
}

src/features/shapes/chladni/methods.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import BESSEL from "bessel"
22
import { BESSEL_ZEROS, SUPERPOSITION, EXCITATION } from "./config"
3+
import { toPolar } from "@/common/geometry"
34
import {
45
defaultTerms,
56
defaultScale,
67
getEffectiveValue,
7-
toPolar,
8+
getTrig,
89
} from "./helpers"
910

1011
// Rectangular plate interference pattern
1112
// Domain: [0, 1] or [-1, 1]
1213
export function interferenceRectangular(x, y, opts) {
1314
const { m, n, superposition, amplitude, boundary } = opts
14-
const trig = boundary === "free" ? Math.cos : Math.sin
15+
const trig = getTrig(boundary)
1516
const mode = SUPERPOSITION[superposition]
1617
const term1 = trig(n * Math.PI * x) * trig(m * Math.PI * y)
1718
const term2 = trig(m * Math.PI * x) * trig(n * Math.PI * y)
@@ -34,12 +35,10 @@ export function interferenceRectangular(x, y, opts) {
3435
// Domain: x,y in [-1, 1], circular boundary at r=1
3536
export function interferenceCircular(x, y, opts) {
3637
const { m, n, radial, superposition, amplitude, boundary } = opts
37-
const polar = toPolar(x, y)
38+
const { r, theta } = toPolar(x, y)
3839

39-
if (!polar) return 1000
40-
41-
const { r, theta } = polar
42-
const trig = boundary === "free" ? Math.cos : Math.sin
40+
if (r > 1) return 1000
41+
const trig = getTrig(boundary)
4342
const mode = SUPERPOSITION[superposition]
4443

4544
// Default terms: two different angular modes, same radial zero
@@ -66,7 +65,7 @@ export function interferenceCircular(x, y, opts) {
6665
// frequency scales all harmonics (zoom), modes controls complexity
6766
export function harmonicRectangular(x, y, opts) {
6867
const { modes, decay, frequency, boundary } = opts
69-
const trig = boundary === "free" ? Math.cos : Math.sin
68+
const trig = getTrig(boundary)
7069
const scaledFreq = frequency * 1.5 - 1
7170
const decayExp = (decay + 1) / 8
7271

@@ -94,11 +93,9 @@ export function harmonicRectangular(x, y, opts) {
9493
// modes controls angular complexity, radial controls radial complexity
9594
export function harmonicCircular(x, y, opts) {
9695
let { modes, radial, decay, frequency } = opts
97-
const polar = toPolar(x, y)
98-
99-
if (!polar) return 1000
96+
const { r, theta } = toPolar(x, y)
10097

101-
const { r, theta } = polar
98+
if (r > 1) return 1000
10299
const scaledFreq = frequency * 1.5
103100
const decayExp = (decay + 1) / 16
104101
let sum = 0
@@ -211,11 +208,9 @@ export function excitationRectangular(x, y, opts) {
211208
// Loops over angular modes (m) and radial modes (n)
212209
export function excitationCircular(x, y, opts) {
213210
const { excitation, spread, position, modes, radial, frequency } = opts
214-
const polar = toPolar(x, y)
215-
216-
if (!polar) return 1000
211+
const { r, theta } = toPolar(x, y)
217212

218-
const { r, theta } = polar
213+
if (r > 1) return 1000
219214
const spreadFactor = spread / 5
220215
const freq = frequency * 2 - 1
221216
let sum = 0

0 commit comments

Comments
 (0)