Skip to content

Commit

Permalink
Add Rotate wrapper (#37)
Browse files Browse the repository at this point in the history
* implement rotate wrapper

* formatbot: Automatically format code

* Implement Rotate wrapper

* formatbot: Automatically format code

---------

Co-authored-by: tscircuitbot <tscircuitbot@users.noreply.github.com>
  • Loading branch information
Slaviiiii and tscircuitbot authored Jul 23, 2024
1 parent cf94e19 commit 4eb556a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
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.

0 comments on commit 4eb556a

Please sign in to comment.