@splinetool/runtime 0.9.38 → 0.9.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +146 -2
  2. package/build/runtime.js +173 -173
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,3 +1,147 @@
1
- # Runtime
1
+ # Spline Runtime
2
2
 
3
- The code for the smaller spline build needed only to run the HTML export.
3
+ **runtime** allows you to run Spline scenes in javascript.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ yarn add @splinetool/runtime
9
+ ```
10
+
11
+ or
12
+
13
+ ```bash
14
+ npm install @splinetool/runtime
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ To use runtime, first you have to go to the Spline editor, click on the **Export** button, select "**Code**" and then "**Vanilla JS**".
20
+
21
+ You can copy the URL there and pass it to the `.load()` function:
22
+
23
+ ```js
24
+ import { Application } from '@splinetool/runtime';
25
+
26
+ // make sure you have a canvas in the body
27
+ const canvas = document.getElementById('canvas3d');
28
+
29
+ // start the application and load the scene
30
+ const app = new Application(canvas);
31
+ app.load('https://prod.spline.design/TRfTj83xgjIdHPmT/scene.spline');
32
+ ```
33
+
34
+ You should be able to see the scene you exported in your canvas.
35
+
36
+ ### Read and modify Spline objects
37
+
38
+ You can query any Spline object via `findObjectByName` or `findObjectById`.
39
+
40
+ _(You can get the ID of the object in the `Develop` pane of the right sidebar)._
41
+
42
+ ```js
43
+ import { Application } from '@splinetool/runtime';
44
+
45
+ const canvas = document.getElementById('canvas3d');
46
+ const app = new Application(canvas);
47
+ app
48
+ .load('https://prod.spline.design/TRfTj83xgjIdHPmT/scene.spline')
49
+ .then(() => {
50
+ const obj = spline.findObjectByName('my object');
51
+ // or
52
+ // const obj = spline.findObjectById('8E8C2DDD-18B6-4C54-861D-7ED2519DE20E');
53
+
54
+ console.log(obj); // Spline Object => { name: 'my object', id: '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E', position: {}, ... }
55
+
56
+ // move the object in 3D space
57
+ obj.position.x += 10;
58
+ });
59
+ ```
60
+
61
+ ### Listen to events
62
+
63
+ You can listen to any Spline Event you set in the Events panel of the editor by attaching a listener to the Spline instance.
64
+
65
+ ```js
66
+ import { Application } from '@splinetool/runtime';
67
+
68
+ const canvas = document.getElementById('canvas3d');
69
+ const app = new Application(canvas);
70
+ app
71
+ .load('https://prod.spline.design/TRfTj83xgjIdHPmT/scene.spline')
72
+ .then(() => {
73
+ app.addEventListener('mousedown', (e) => {
74
+ if (e.target.name === 'my object') {
75
+ // doSomething();
76
+ }
77
+ });
78
+ });
79
+ ```
80
+
81
+ You can find a list of all of the Spline Event listeners in the [API](#api) section.
82
+
83
+ ### Trigger Spline events from outside
84
+
85
+ You can trigger any animation Event you set in the Events panel in the Spline Editor.
86
+
87
+ You can use the `emitEvent` function, passing the [event type](#spline-events) and the ID of your object.
88
+
89
+ _(You can get the ID of the object in the `Develop` pane of the right sidebar)._
90
+
91
+ ```js
92
+ import { Application } from '@splinetool/runtime';
93
+
94
+ const canvas = document.getElementById('canvas3d');
95
+ const app = new Application(canvas);
96
+ app
97
+ .load('https://prod.spline.design/TRfTj83xgjIdHPmT/scene.spline')
98
+ .then(() => {
99
+ app.emitEvent('mouseHover', '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E');
100
+ });
101
+ ```
102
+
103
+ Or you can query the spline object first, and then trigger the event:
104
+
105
+ ```js
106
+ import { Application } from '@splinetool/runtime';
107
+
108
+ const canvas = document.getElementById('canvas3d');
109
+ const app = new Application(canvas);
110
+ app
111
+ .load('https://prod.spline.design/TRfTj83xgjIdHPmT/scene.spline')
112
+ .then(() => {
113
+ const obj = spline.findObjectByName('my object');
114
+ objectToAnimate.emitEvent('mouseHover');
115
+ });
116
+ ```
117
+
118
+ You can find a list of all of the Spline Events you can pass to the `emitEvent` function in the [Spline Events](#spline-events) section.
119
+
120
+ ## API
121
+
122
+ ### Spline Application Methods
123
+
124
+ You can call all these different methods on the Spline `Application` instance.
125
+
126
+ | Name | Type | Description |
127
+ | ------------------ | ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
128
+ | `emitEvent` | `(eventName: SplineEventName, uuid: string) => void` | Triggers a Spline event associated to an object with provided uuid in reverse order. Starts from first state to last state. |
129
+ | `emitEventReverse` | `(eventName: SplineEventName, uuid: string) => void` | Triggers a Spline event associated to an object with provided uuid in reverse order. Starts from last state to first state. |
130
+ | `findObjectById` | `(uuid: string) => SPEObject` | Searches through scene's children and returns the object with that uuid. |
131
+ | `findObjectByName` | `(name: string) => SPEObject` | Searches through scene's children and returns the first object with that name |
132
+ | `setZoom` | `(zoom: number) => void` | Sets the initial zoom of the scene. |
133
+
134
+ ### Spline Events
135
+
136
+ These are all the Spline event types that you can pass to the `emitEvent` or `emitEventReverse` function.
137
+
138
+ | Name | Description |
139
+ | ------------ | --------------------------------------------- |
140
+ | `mouseDown` | Refers to the Spline `Mouse Down` event type |
141
+ | `mouseHover` | Refers to the Spline `Mouse Hover` event type |
142
+ | `mouseUp` | Refers to the Spline `Mouse Up` event type |
143
+ | `keyDown` | Refers to the Spline `Key Down` event type |
144
+ | `keyUp` | Refers to the Spline `Key Up` event type |
145
+ | `start` | Refers to the Spline `Start` event type |
146
+ | `lookAt` | Refers to the Spline `Look At` event type |
147
+ | `follow` | Refers to the Spline `Mouse Up` event type |