@@ -8,6 +8,8 @@ import RiveComponent, {
8
8
EventType ,
9
9
useRive ,
10
10
useStateMachineInput ,
11
+ decodeImage ,
12
+ decodeFont ,
11
13
} from ' ../../src' ;
12
14
import { Button } from ' ./components/Button' ;
13
15
import ' ./rive-overview.css' ;
@@ -196,3 +198,90 @@ To listen for Rive Events reported during state machine play, use the `on` API t
196
198
}}
197
199
</Story >
198
200
</Canvas >
201
+
202
+ ## Out of Bound Asset Loading
203
+
204
+ Loading out of band assets for images and fonts will involve passing an ` assetLoader ` prop to the ` useRive ` params object.
205
+
206
+ Use the ` decodeImage ` and ` decodeAsset ` functions, which are named exports on the package, to create appropriate RenderImage
207
+ and Rive Font objects to set on the individual Rive Asset. You can set these objects with ` .setRenderImage ` and ` .setFont ` APIs
208
+ on the Asset respectively.
209
+
210
+ <Canvas withSource = " open" >
211
+ <Story name = " Out of Bound Asset Loading" >
212
+ { () => {
213
+ const randomImageAsset = (asset , rive ) => {
214
+ fetch (' https://picsum.photos/1000/1500' ).then (async (res ) => {
215
+ const image = await decodeImage (
216
+ new Uint8Array (await res .arrayBuffer ())
217
+ );
218
+ asset .setRenderImage (image );
219
+ });
220
+ };
221
+ const randomFontAsset = (asset , rive ) => {
222
+ const urls = [
223
+ ' https://cdn.rive.app/runtime/flutter/IndieFlower-Regular.ttf' ,
224
+ ' https://cdn.rive.app/runtime/flutter/comic-neue.ttf' ,
225
+ ' https://cdn.rive.app/runtime/flutter/inter.ttf' ,
226
+ ' https://cdn.rive.app/runtime/flutter/inter-tight.ttf' ,
227
+ ' https://cdn.rive.app/runtime/flutter/josefin-sans.ttf' ,
228
+ ' https://cdn.rive.app/runtime/flutter/send-flowers.ttf' ,
229
+ ];
230
+ fetch (urls [(fontIndex + 1 ) % urls .length ]).then (async (res ) => {
231
+ const font = await decodeFont (
232
+ new Uint8Array (await res .arrayBuffer ())
233
+ );
234
+ asset .setFont (font );
235
+ });
236
+ setFontIndex (fontIndex + 1 );
237
+ };
238
+ const [imageAsset, setImageAsset] = useState (null );
239
+ const [fontAsset, setFontAsset] = useState (null );
240
+ const [fontIndex, setFontIndex] = useState (0 );
241
+ const { rive, RiveComponent } = useRive ({
242
+ src: ' asset_load_check.riv' ,
243
+ autoplay: true ,
244
+ automaticallyHandleEvents: true ,
245
+ loadCDNAssets: false ,
246
+ assetLoader : (asset , bytes ) => {
247
+ console .log (
248
+ ' Tell our asset importer if we are going to load the asset contents' ,
249
+ {
250
+ name: asset .name ,
251
+ fileExtension: asset .fileExtension ,
252
+ cdnUuid: asset .cdnUuid ,
253
+ isFont: asset .isFont ,
254
+ isImage: asset .isImage ,
255
+ bytes ,
256
+ }
257
+ );
258
+ if (asset .cdnUuid .length > 0 || bytes .length > 0 ) {
259
+ return false ;
260
+ }
261
+ if (asset .isImage ) {
262
+ setImageAsset (asset );
263
+ randomImageAsset (asset , rive );
264
+ return true ;
265
+ } else if (asset .isFont ) {
266
+ setFontAsset (asset );
267
+ randomFontAsset (asset , rive );
268
+ return true ;
269
+ }
270
+ return false ;
271
+ },
272
+ });
273
+ return (
274
+ <div className = " center" >
275
+ <RiveComponent
276
+ className = " large-canvas-size"
277
+ onClick = { () => {
278
+ randomImageAsset (imageAsset );
279
+ randomFontAsset (fontAsset );
280
+ }}
281
+ />
282
+ <p >Click on the canvas to load new assets!</p >
283
+ </div >
284
+ );
285
+ }}
286
+ </Story >
287
+ </Canvas >
0 commit comments