Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rotate wrapper #37

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/rotate.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Polygon } from "../lib"
import { JsCadFixture } from "../lib/components/jscad-fixture"
import { Rotate } from "../lib/jscad-fns"

export default () => (
<JsCadFixture>
<Rotate angles={[0, 0, Math.PI]}>
<Polygon
points={[
[-2, -1],
[2, -1],
[2.5, 2],
[1, 1],
[0, 2],
[-1, 1],
[-2, 2],
]}
/>
</Rotate>
</JsCadFixture>
)
16 changes: 15 additions & 1 deletion lib/create-host-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import type {
GeodesicSphereProps,
PolygonProps,
ProjectProps,
RotateProps,
RoundedCuboidProps,
RoundedCylinderProps,
SphereProps,
TorusProps,
UnionProps,
} from "./jscad-fns"
import type { TranslateProps } from "./jscad-fns/translate"
import type { JSCADModule, JSCADPrimitive } from "./jscad-primitives"

export function createHostConfig(jscad: JSCADModule) {
Expand Down Expand Up @@ -245,6 +245,20 @@ export function createHostConfig(jscad: JSCADModule) {
return jscad.transforms.translate(args, childGeometry)
}

case "rotate": {
const { children, ...rotateProps } =
props as JSX.IntrinsicElements["rotate"]

const childrenGeometry = renderChildren(children)

const rotateGeometry = jscad.transforms.rotate(
rotateProps.angles,
childrenGeometry,
)

return rotateGeometry
}

default:
throw new Error(`Unknown element type: ${type}`)
}
Expand Down
1 change: 1 addition & 0 deletions lib/intrinsic-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare global {
torus: FN.TorusProps
custom: FN.CustomProps
union: FN.UnionProps
rotate: { angles: [number, number, number]; children: any }
translate: { args: [number, number, number]; children: any }
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/jscad-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "./extrude-rotate"
export * from "./geodesic-sphere"
export * from "./polygon"
export * from "./project"
export * from "./rotate"
export * from "./rounded-cuboid"
export * from "./rounded-cylinder"
export * from "./sphere"
Expand Down
13 changes: 13 additions & 0 deletions lib/jscad-fns/rotate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Point3 } from "./translate"

export type RotateProps = {
angles: Point3
children: React.ReactNode
}

export function Rotate({ angles, children }: RotateProps) {
if (!Array.isArray(angles)) {
angles = [angles.x, angles.y, angles.z]
}
return <rotate angles={angles}>{children}</rotate>
}
3 changes: 3 additions & 0 deletions lib/jscad-primitives.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Point3 } from "./jscad-fns"

// Define a type for the JSCAD module structure we expect
export interface JSCADModule {
primitives: {
Expand Down Expand Up @@ -61,6 +63,7 @@ export interface JSCADModule {
}
transforms: {
translate: (vector: [number, number, number], object: any) => any
rotate: (angles: Point3, object: any) => any
}
extrusions: {
extrudeLinear: (
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading