Skip to content

Commit 98cf997

Browse files
authored
🖼 Update to support the new Responsive size (#38)
* 🖼 Render only the canvas * 🕺 Update example
1 parent e59cec0 commit 98cf997

File tree

6 files changed

+448
-484
lines changed

6 files changed

+448
-484
lines changed

‎example/App.tsx‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import anime from 'animejs';
55

66
function App() {
77
const [spline, setSpline] = useState<Application>(null);
8-
const canvasParent = useRef<HTMLDivElement>(null);
98
const tileRef = useRef<SPEObject>();
109

1110
useEffect(() => {
12-
if (spline) {
13-
const tile = spline.findObjectById(
14-
'7ba78968-2a55-48f2-b14c-5191da3e075e'
15-
);
16-
tileRef.current = tile;
17-
}
11+
if (!spline) return;
12+
13+
const tile = spline.findObjectByName('Rectangle');
14+
tileRef.current = tile;
1815
}, [spline]);
1916

2017
function handleLoad(e: Application) {
@@ -63,9 +60,7 @@ function App() {
6360
</button>
6461
</div>
6562
<Spline
66-
ref={canvasParent}
67-
autoRender
68-
scene="https://prod.spline.design/ft9KFAMYebCiRXbC/scene.spline"
63+
scene="https://prod.spline.design/2fzdsSVagfszNxsd/scene.splinecode"
6964
onLoad={handleLoad}
7065
/>
7166
</>

‎example/index.css‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
body {
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
html,
6+
body,
7+
#root {
8+
width: 100%;
9+
height: 100%;
210
margin: 0;
11+
padding: 0;
12+
-webkit-touch-callout: none;
13+
-webkit-user-select: none;
14+
-moz-user-select: none;
15+
user-select: none;
16+
overflow: hidden;
17+
}
18+
19+
body {
320
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
421
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
522
sans-serif;
623
-webkit-font-smoothing: antialiased;
724
-moz-osx-font-smoothing: grayscale;
825
}
926

10-
code {
11-
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12-
monospace;
13-
}
14-
1527
.buttons {
1628
position: absolute;
17-
}
29+
}

‎example/scene.spline‎

-4.17 KB
Binary file not shown.

‎package.json‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
"react": ">=17.0.0",
2828
"react-dom": ">=17.0.0"
2929
},
30+
"dependencies": {
31+
"react-merge-refs": "^1.1.0"
32+
},
3033
"devDependencies": {
31-
"@splinetool/runtime": "^0.9.35",
34+
"@splinetool/runtime": "link:/Users/marcofugaro/Code/spline/packages/runtime",
3235
"@types/animejs": "^3.1.4",
3336
"@types/node": "^17.0.27",
3437
"@types/react": "^18.0.7",
@@ -41,4 +44,4 @@
4144
"typescript": "^4.6.3",
4245
"vite": "^2.9.5"
4346
}
44-
}
47+
}

‎src/Spline.tsx‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ import type {
55
SplineEvent,
66
SplineEventName,
77
} from '@splinetool/runtime';
8+
import mergeRefs from 'react-merge-refs';
89

910
export type { SPEObject, SplineEvent, SplineEventName };
1011

11-
export interface SplineProps {
12+
type CanvasAttributes = React.CanvasHTMLAttributes<HTMLCanvasElement>;
13+
export interface SplineProps
14+
extends Omit<
15+
CanvasAttributes,
16+
| 'onLoad'
17+
| 'onMouseDown'
18+
| 'onMouseUp'
19+
| 'onMouseHover'
20+
| 'onKeyDown'
21+
| 'onKeyUp'
22+
| 'onWheel'
23+
> {
1224
scene: string;
13-
id?: string;
14-
style?: CSSProperties;
15-
className?: string;
1625
onLoad?: (e: Application) => void;
1726
onMouseDown?: (e: SplineEvent) => void;
1827
onMouseUp?: (e: SplineEvent) => void;
@@ -26,13 +35,11 @@ export interface SplineProps {
2635
autoRender?: boolean;
2736
}
2837

29-
const Spline = forwardRef<HTMLDivElement, SplineProps>(
38+
const Spline = forwardRef<HTMLCanvasElement, SplineProps>(
3039
(
3140
{
3241
scene,
33-
id,
3442
style,
35-
className,
3643
onMouseDown,
3744
onMouseUp,
3845
onMouseHover,
@@ -44,6 +51,7 @@ const Spline = forwardRef<HTMLDivElement, SplineProps>(
4451
onWheel,
4552
onLoad,
4653
autoRender = false,
54+
...props
4755
},
4856
ref
4957
) => {
@@ -128,16 +136,14 @@ const Spline = forwardRef<HTMLDivElement, SplineProps>(
128136
}, [scene]);
129137

130138
return (
131-
<div
139+
<canvas
140+
ref={mergeRefs<HTMLCanvasElement>([ref, canvasRef])}
132141
style={{
133-
display: `${isLoading ? 'none' : 'flex'}`,
142+
display: isLoading ? 'none' : undefined,
134143
...style,
135144
}}
136-
className={className}
137-
ref={ref}
138-
>
139-
<canvas ref={canvasRef} id={id} />
140-
</div>
145+
{...props}
146+
/>
141147
);
142148
}
143149
);

0 commit comments

Comments
 (0)