Body
Bodies are very simple. They are just the AraraJS way of outputting animated signals. In case you are interested in the speed in which your signal is changing, for example, you can access the velocity
and acceleration
properties of the Body
object.
type Body = {
position: number
velocity: number
acceleration: number
}
2D and 3D variants Section titled 2D and 3D variants
For your convenience, AraraJS provides primitives for 2D and 3D animations as well. For example createSpring2D
will return an accessor for a Body2D
const [body2d] = createSineWave2D({
phase: [Math.PI / 2, 0],
})
import { cn } from '@lib/cn'
import { createElementSize } from '@solid-primitives/resize-observer'
import { createSignal } from 'solid-js'
import { createSineWave2D } from 'ararajs'
export function SineWave2DDemo(props: { class?: string; ballClass?: string }) {
const [container, setContainer] = createSignal<HTMLDivElement>()
const containerSize = createElementSize(container)
const [ball, setBall] = createSignal<HTMLDivElement>()
const ballSize = createElementSize(ball)
const radius = 90
const freq = 1
const [body] = createSineWave2D({
amplitude: [radius, radius],
frequency: [freq, freq],
phase: [Math.PI / 2, 0],
})
const xOffset = () => {
return (
body.position[0] +
-(ballSize.width ?? 0) / 2 +
(containerSize.width ?? 0) / 2
)
}
const yOffset = () => {
return (
body.position[1] +
-(ballSize.height ?? 0) / 2 +
(containerSize.height ?? 0) / 2
)
}
const paddingY = 32
return (
<div
class={cn(
'h-full w-full overflow-hidden rounded-lg shadow-inner',
props.class,
)}
style={{
'min-height': `${2 * radius + 2 * paddingY + (ballSize.height ?? 0)}px`,
}}
ref={setContainer}
>
<div
class={cn(
'size-10 rounded-full bg-arara-text/70 shadow-xl',
props.ballClass,
)}
style={{
transform: `translate(${xOffset()}px, ${yOffset()}px)`,
}}
ref={setBall}
/>
</div>
)
}
The 2D and 3D variants use vectors from the gl-matrix library.
These types
vec2
andvec3
are exposed to you by directly importing from AraraJS.
import { vec2, vec3 } from 'ararajs'
// Equilalent to
import { vec2, vec3 } from 'gl-matrix'
type Body2D = {
position: vec2
velocity: vec2
acceleration: vec2
}
type Body3D = {
position: vec3
velocity: vec3
acceleration: vec3
}