Skip to content

Commit

Permalink
Solving geometry to launch confetti bc middle age crisis issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nacmartin committed Mar 12, 2023
1 parent f757f67 commit c5e84d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 14 additions & 5 deletions examples/ui-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { GestureEvent, AirfingerEvent } from "manitas";
import { useEffect, useRef } from "react";
import { useSprings, animated, to } from "@react-spring/web";
import styles from "./styles.module.css";
import { contains, inScreen, rectRelativeToParent } from "./geometry";
import {
confettiPosition,
contains,
inScreen,
rectRelativeToParent,
} from "./geometry";
import confetti from "canvas-confetti";
import { Instructions } from "./Instructions";

Expand Down Expand Up @@ -125,15 +130,19 @@ function App() {
});
}
if (gesture === "Victory") {
if (!containerRef.current) {
return;
}
const position = confettiPosition(
e.detail.airpoint,
containerRef.current.getBoundingClientRect()
);
confetti({
particleCount: 100,
startVelocity: 30,
spread: 360,
scalar: 2,
origin: {
x: ((1 - e.detail.airpoint.x) * AREA_WIDTH) / window.innerWidth,
y: e.detail.airpoint.y + 0.2,
},
origin: position,
});
}
};
Expand Down
9 changes: 9 additions & 0 deletions examples/ui-react/src/geometry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Point3D } from "manitas";
export function confettiPosition(point: Point3D, container: DOMRect): Point2D {
const { x: xRect, y: yRect } = container;
const canvasPos = inScreen({ x: 1 - point.x, y: point.y, z: point.z });
return {
x: (xRect + canvasPos.x) / window.innerWidth,
y: (yRect + canvasPos.y) / window.innerHeight,
};
}

export function inScreen(point: Point3D) {
return {
x: point.x * 1280,
Expand Down

0 comments on commit c5e84d8

Please sign in to comment.