@splinetool/runtime 0.9.37 → 0.9.40

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 +148 -2
  2. package/build/runtime.js +221 -229
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,3 +1,149 @@
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/2qM3cW5Cx15m3cJ7/scene.splinecode');
32
+ ```
33
+
34
+ You should be able to see the scene you exported in your canvas.
35
+
36
+ > :warning: Only **.splinecode** files should be loaded through this API. `.spline` files are meant to be used in the editor.
37
+
38
+ ### Read and modify Spline objects
39
+
40
+ You can query any Spline object via `findObjectByName` or `findObjectById`.
41
+
42
+ _(You can get the ID of the object in the `Develop` pane of the right sidebar)._
43
+
44
+ ```js
45
+ import { Application } from '@splinetool/runtime';
46
+
47
+ const canvas = document.getElementById('canvas3d');
48
+ const app = new Application(canvas);
49
+ app
50
+ .load('https://prod.spline.design/2qM3cW5Cx15m3cJ7/scene.splinecode')
51
+ .then(() => {
52
+ const obj = spline.findObjectByName('my object');
53
+ // or
54
+ // const obj = spline.findObjectById('8E8C2DDD-18B6-4C54-861D-7ED2519DE20E');
55
+
56
+ console.log(obj); // Spline Object => { name: 'my object', id: '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E', position: {}, ... }
57
+
58
+ // move the object in 3D space
59
+ obj.position.x += 10;
60
+ });
61
+ ```
62
+
63
+ ### Listen to events
64
+
65
+ You can listen to any Spline Event you set in the Events panel of the editor by attaching a listener to the Spline instance.
66
+
67
+ ```js
68
+ import { Application } from '@splinetool/runtime';
69
+
70
+ const canvas = document.getElementById('canvas3d');
71
+ const app = new Application(canvas);
72
+ app
73
+ .load('https://prod.spline.design/2qM3cW5Cx15m3cJ7/scene.splinecode')
74
+ .then(() => {
75
+ app.addEventListener('mousedown', (e) => {
76
+ if (e.target.name === 'my object') {
77
+ // doSomething();
78
+ }
79
+ });
80
+ });
81
+ ```
82
+
83
+ You can find a list of all of the Spline Event listeners in the [API](#api) section.
84
+
85
+ ### Trigger Spline events from outside
86
+
87
+ You can trigger any animation Event you set in the Events panel in the Spline Editor.
88
+
89
+ You can use the `emitEvent` function, passing the [event type](#spline-events) and the ID of your object.
90
+
91
+ _(You can get the ID of the object in the `Develop` pane of the right sidebar)._
92
+
93
+ ```js
94
+ import { Application } from '@splinetool/runtime';
95
+
96
+ const canvas = document.getElementById('canvas3d');
97
+ const app = new Application(canvas);
98
+ app
99
+ .load('https://prod.spline.design/2qM3cW5Cx15m3cJ7/scene.splinecode')
100
+ .then(() => {
101
+ app.emitEvent('mouseHover', '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E');
102
+ });
103
+ ```
104
+
105
+ Or you can query the spline object first, and then trigger the event:
106
+
107
+ ```js
108
+ import { Application } from '@splinetool/runtime';
109
+
110
+ const canvas = document.getElementById('canvas3d');
111
+ const app = new Application(canvas);
112
+ app
113
+ .load('https://prod.spline.design/2qM3cW5Cx15m3cJ7/scene.splinecode')
114
+ .then(() => {
115
+ const obj = spline.findObjectByName('my object');
116
+ objectToAnimate.emitEvent('mouseHover');
117
+ });
118
+ ```
119
+
120
+ 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.
121
+
122
+ ## API
123
+
124
+ ### Spline Application Methods
125
+
126
+ You can call all these different methods on the Spline `Application` instance.
127
+
128
+ | Name | Type | Description |
129
+ | ------------------ | ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
130
+ | `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. |
131
+ | `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. |
132
+ | `findObjectById` | `(uuid: string) => SPEObject` | Searches through scene's children and returns the object with that uuid. |
133
+ | `findObjectByName` | `(name: string) => SPEObject` | Searches through scene's children and returns the first object with that name |
134
+ | `setZoom` | `(zoom: number) => void` | Sets the initial zoom of the scene. |
135
+
136
+ ### Spline Events
137
+
138
+ These are all the Spline event types that you can pass to the `emitEvent` or `emitEventReverse` function.
139
+
140
+ | Name | Description |
141
+ | ------------ | --------------------------------------------- |
142
+ | `mouseDown` | Refers to the Spline `Mouse Down` event type |
143
+ | `mouseHover` | Refers to the Spline `Mouse Hover` event type |
144
+ | `mouseUp` | Refers to the Spline `Mouse Up` event type |
145
+ | `keyDown` | Refers to the Spline `Key Down` event type |
146
+ | `keyUp` | Refers to the Spline `Key Up` event type |
147
+ | `start` | Refers to the Spline `Start` event type |
148
+ | `lookAt` | Refers to the Spline `Look At` event type |
149
+ | `follow` | Refers to the Spline `Mouse Up` event type |