@rive-app/react-canvas 3.0.50 → 3.0.51
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.
- package/dist/index.js +1 -1
- package/dist/package.json +1 -1
- package/dist/types/components/Rive.d.ts +5 -1
- package/dist/types/hooks/useContainerSize.d.ts +10 -0
- package/dist/types/hooks/useDevicePixelRatio.d.ts +11 -0
- package/dist/types/hooks/useResizeCanvas.d.ts +52 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/types.d.ts +3 -0
- package/dist/types/utils.d.ts +7 -15
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),n=require("@rive-app/canvas");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=t(e),i=function(){return i=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var i in n=arguments[t])Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i]);return e},i.apply(this,arguments)};function a(e,n){var t={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&n.indexOf(r)<0&&(t[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)n.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(t[r[i]]=e[r[i]])}return t}function o(){var e="undefined"!=typeof window&&"number"==typeof window.devicePixelRatio?window.devicePixelRatio:1;return Math.min(Math.max(1,e),3)}var s=function(){function e(){}return e.prototype.observe=function(){},e.prototype.unobserve=function(){},e.prototype.disconnect=function(){},e}();var u=globalThis.ResizeObserver||s,c=void 0!==globalThis.ResizeObserver,f=!c;function d(n,t){void 0===t&&(t=!0);var r=e.useState({width:0,height:0}),i=r[0],a=r[1];e.useEffect(function(){if("undefined"!=typeof window&&t){var e=function(){a({width:window.innerWidth,height:window.innerHeight})};return f&&(e(),window.addEventListener("resize",e)),function(){return window.removeEventListener("resize",e)}}},[]);var o,s,d,l=e.useRef(new u((o=function(e){c&&a({width:e[e.length-1].contentRect.width,height:e[e.length-1].contentRect.height})},s=0,d=0,function(){for(var e=this,n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];clearTimeout(d),d=window.setTimeout(function(){return o.apply(e,n)},s)})));return e.useEffect(function(){var e=l.current;if(t){var r=n.current;return n.current&&c&&e.observe(n.current),function(){e.disconnect(),r&&c&&e.unobserve(r)}}e.disconnect()},[n,l]),i}var l={useDevicePixelRatio:!0,fitCanvasToArtboardHeight:!1,useOffscreenRenderer:!0,shouldResizeCanvasToContainer:!0};function v(e){return Object.assign({},l,e)}function h(n){var t=n.riveLoaded,r=void 0!==t&&t,i=n.canvasRef,a=n.containerRef,s=n.options,u=void 0===s?{}:s,c=n.onCanvasHasResized,f=n.artboardBounds,l=v(u),h=e.useState({height:0,width:0}),p=h[0],w=p.height,R=p.width,g=h[1],b=e.useState({height:0,width:0}),y=b[0],m=y.height,C=y.width,O=b[1],x=e.useState(!0),E=x[0],P=x[1],T=l.fitCanvasToArtboardHeight,L=l.shouldResizeCanvasToContainer,z=l.useDevicePixelRatio,j=d(a,L),M=function(){var n=o(),t=e.useState(n),r=t[0],i=t[1];return e.useEffect(function(){if("undefined"!=typeof window&&"matchMedia"in window){var e=function(){var e=o();i(e)},n=window.matchMedia("screen and (resolution: ".concat(r,"dppx)"));return n.hasOwnProperty("addEventListener")?n.addEventListener("change",e):n.addListener(e),function(){n.hasOwnProperty("removeEventListener")?n.removeEventListener("change",e):n.removeListener(e)}}},[r]),r}(),S=null!=f?f:{},H=S.maxX,k=S.maxY,D=e.useCallback(function(){var e,n,t,r,i=null!==(n=null===(e=a.current)||void 0===e?void 0:e.clientWidth)&&void 0!==n?n:0,o=null!==(r=null===(t=a.current)||void 0===t?void 0:t.clientHeight)&&void 0!==r?r:0;return T&&f?{width:i,height:i*(f.maxY/f.maxX)}:{width:i,height:o}},[a,T,H,k]);e.useEffect(function(){if(L&&a.current&&r){var e=D(),n=e.width,t=e.height,o=!1;if(i.current){var s=n!==R||t!==w;if(l.fitCanvasToArtboardHeight&&s&&(a.current.style.height=t+"px",o=!0),l.useDevicePixelRatio){if(s||(n*M!==C||t*M!==m)){var u=M*n,f=M*t;i.current.width=u,i.current.height=f,i.current.style.width=n+"px",i.current.style.height=t+"px",O({width:u,height:f}),o=!0}}else s&&(i.current.width=n,i.current.height=t,O({width:n,height:t}),o=!0);g({width:n,height:t})}c&&(E||o)&&c&&c(),E&&P(!1)}},[i,a,j,M,D,E,P,m,C,w,R,c,L,T,z,r])}function p(e){var n=e.setContainerRef,t=e.setCanvasRef,o=e.className,s=void 0===o?"":o,u=e.style,c=e.children,f=a(e,["setContainerRef","setCanvasRef","className","style","children"]),d=i({width:"100%",height:"100%"},u);return r.default.createElement("div",i({ref:n,className:s},!s&&{style:d}),r.default.createElement("canvas",i({ref:t,style:{verticalAlign:"top",width:0,height:0}},f),c))}function w(t,a){void 0===a&&(a={});var o=e.useRef(null),s=e.useRef(null),u=e.useState(null),c=u[0],f=u[1],d=Boolean(t),l=v(a),w=e.useCallback(function(){c&&(c.startRendering(),c.resizeToCanvas())},[c]);h({riveLoaded:!!c,canvasRef:o,containerRef:s,options:l,onCanvasHasResized:w,artboardBounds:null==c?void 0:c.bounds});var R=e.useCallback(function(e){if(e&&t&&d){var r=l.useOffscreenRenderer,a=new n.Rive(i(i({useOffscreenRenderer:r},t),{canvas:e}));a.on(n.EventType.Load,function(){o.current?f(a):a.cleanup()})}else null===e&&o.current&&(o.current.height=0,o.current.width=0);o.current=e},[d]),g=e.useCallback(function(e){s.current=e},[]);e.useEffect(function(){var e=new IntersectionObserver(function(e){e[0].isIntersecting?c&&c.startRendering():c&&c.stopRendering()});return o.current&&e.observe(o.current),function(){e.disconnect()}},[c]),e.useEffect(function(){return function(){c&&(c.cleanup(),f(null))}},[c]);var b=null==t?void 0:t.animations;e.useEffect(function(){c&&b&&(c.isPlaying?(c.stop(c.animationNames),c.play(b)):c.isPaused&&(c.stop(c.animationNames),c.pause(b)))},[b,c]);var y=e.useCallback(function(e){return r.default.createElement(p,i({setContainerRef:g,setCanvasRef:R},e))},[R,g]);return{canvas:o.current,container:s.current,setCanvasRef:R,setContainerRef:g,rive:c,RiveComponent:y}}exports.default=function(e){var n=e.src,t=e.artboard,o=e.animations,s=e.stateMachines,u=e.layout,c=e.useOffscreenRenderer,f=void 0===c||c,d=e.shouldDisableRiveListeners,l=void 0!==d&&d,v=e.shouldResizeCanvasToContainer,h=void 0===v||v,p=e.children,R=a(e,["src","artboard","animations","stateMachines","layout","useOffscreenRenderer","shouldDisableRiveListeners","shouldResizeCanvasToContainer","children"]),g=w({src:n,artboard:t,animations:o,layout:u,stateMachines:s,autoplay:!0,shouldDisableRiveListeners:l},{useOffscreenRenderer:f,shouldResizeCanvasToContainer:h}).RiveComponent;return r.default.createElement(g,i({},R),p)},exports.useResizeCanvas=h,exports.useRive=w,exports.useStateMachineInput=function(t,r,i,a){var o=e.useState(null),s=o[0],u=o[1];return e.useEffect(function(){function e(){if(t&&r&&i||u(null),t&&r&&i){var e=t.stateMachineInputs(r);if(e){var n=e.find(function(e){return e.name===i});void 0!==a&&n&&(n.value=a),u(n||null)}}else u(null)}e(),t&&t.on(n.EventType.Play,function(){e()})},[t]),s},Object.keys(n).forEach(function(e){"default"===e||exports.hasOwnProperty(e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return n[e]}})});
|
package/dist/package.json
CHANGED
|
@@ -30,6 +30,10 @@ export interface RiveProps {
|
|
|
30
30
|
* Specify whether to disable Rive listeners on the canvas, thus preventing any event listeners to be attached to the canvas element
|
|
31
31
|
*/
|
|
32
32
|
shouldDisableRiveListeners?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Specify whether to resize the canvas to its container automatically
|
|
35
|
+
*/
|
|
36
|
+
shouldResizeCanvasToContainer?: boolean;
|
|
33
37
|
}
|
|
34
|
-
declare const Rive: ({ src, artboard, animations, stateMachines, layout, useOffscreenRenderer, shouldDisableRiveListeners, children, ...rest }: RiveProps & ComponentProps<'canvas'>) => React.JSX.Element;
|
|
38
|
+
declare const Rive: ({ src, artboard, animations, stateMachines, layout, useOffscreenRenderer, shouldDisableRiveListeners, shouldResizeCanvasToContainer, children, ...rest }: RiveProps & ComponentProps<'canvas'>) => React.JSX.Element;
|
|
35
39
|
export default Rive;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Dimensions } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to listen for a ref element's resize events being triggered. When resized,
|
|
4
|
+
* it sets state to an object of {width: number, height: number} indicating the contentRect
|
|
5
|
+
* size of the element at the new resize.
|
|
6
|
+
*
|
|
7
|
+
* @param containerRef - Ref element to listen for resize events on
|
|
8
|
+
* @returns - Size object with width and height attributes
|
|
9
|
+
*/
|
|
10
|
+
export default function useSize(containerRef: React.MutableRefObject<HTMLElement | null>, shouldResizeCanvasToContainer?: boolean): Dimensions;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listen for devicePixelRatio changes and set the new value accordingly. This could
|
|
3
|
+
* happen for reasons such as:
|
|
4
|
+
* - User moves window from retina screen display to a separate monitor
|
|
5
|
+
* - User controls zoom settings on the browser
|
|
6
|
+
*
|
|
7
|
+
* Source: https://github.com/rexxars/use-device-pixel-ratio/blob/main/index.ts
|
|
8
|
+
*
|
|
9
|
+
* @returns dpr: Number - Device pixel ratio; ratio of physical px to resolution in CSS pixels for current device
|
|
10
|
+
*/
|
|
11
|
+
export default function useDevicePixelRatio(): number;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
|
+
import { Bounds } from '@rive-app/canvas';
|
|
3
|
+
import { UseRiveOptions } from '../types';
|
|
4
|
+
interface UseResizeCanvasProps {
|
|
5
|
+
/**
|
|
6
|
+
* Whether or not Rive is loaded and renderer is associated with the canvas
|
|
7
|
+
*/
|
|
8
|
+
riveLoaded: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Ref to the canvas element
|
|
11
|
+
*/
|
|
12
|
+
canvasRef: MutableRefObject<HTMLCanvasElement | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Ref to the container element of the canvas
|
|
15
|
+
*/
|
|
16
|
+
containerRef: MutableRefObject<HTMLElement | null>;
|
|
17
|
+
/**
|
|
18
|
+
* (Optional) Callback to be invoked after the canvas has been resized due to a resize
|
|
19
|
+
* of its parent container. This is where you would want to reset the layout
|
|
20
|
+
* dimensions for the Rive renderer to dictate the new min/max bounds of the
|
|
21
|
+
* canvas.
|
|
22
|
+
*
|
|
23
|
+
* Using the high-level JS runtime, this might be a simple call to `rive.resizeToCanvas()`
|
|
24
|
+
* Using the low-level JSruntime, this might be invoking the renderer's `.align()` method
|
|
25
|
+
* with the Layout and min/max X/Y values of the canvas.
|
|
26
|
+
*
|
|
27
|
+
* @returns void
|
|
28
|
+
*/
|
|
29
|
+
onCanvasHasResized?: () => void;
|
|
30
|
+
/**
|
|
31
|
+
* (Optional) Options passed to the useRive hook, including the shouldResizeCanvasToContainer option
|
|
32
|
+
* which prevents the canvas element from resizing to its parent container
|
|
33
|
+
*/
|
|
34
|
+
options?: Partial<UseRiveOptions>;
|
|
35
|
+
/**
|
|
36
|
+
* (Optional) AABB bounds of the artboard. If provided, the canvas will be sized to the artboard
|
|
37
|
+
* height if the fitCanvasToArtboardHeight option is true.
|
|
38
|
+
*/
|
|
39
|
+
artboardBounds?: Bounds;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Helper hook to listen for changes in the <canvas> parent container size and size the <canvas>
|
|
43
|
+
* to match. If a resize event has occurred, a supplied callback (onCanvasHasResized)
|
|
44
|
+
* will be inokved to allow for any re-calculation needed (i.e. Rive layout on the canvas).
|
|
45
|
+
*
|
|
46
|
+
* This hook is useful if you are not intending to use the `useRive` hook yourself, but still
|
|
47
|
+
* want to use the auto-sizing logic on the canvas/container.
|
|
48
|
+
*
|
|
49
|
+
* @param props - Object to supply necessary props to the hook
|
|
50
|
+
*/
|
|
51
|
+
export default function useResizeCanvas({ riveLoaded, canvasRef, containerRef, options, onCanvasHasResized, artboardBounds, }: UseResizeCanvasProps): void;
|
|
52
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import Rive from './components/Rive';
|
|
2
2
|
import useRive from './hooks/useRive';
|
|
3
3
|
import useStateMachineInput from './hooks/useStateMachineInput';
|
|
4
|
+
import useResizeCanvas from './hooks/useResizeCanvas';
|
|
4
5
|
export default Rive;
|
|
5
|
-
export { useRive, useStateMachineInput };
|
|
6
|
+
export { useRive, useStateMachineInput, useResizeCanvas };
|
|
6
7
|
export { RiveState, UseRiveParameters, UseRiveOptions } from './types';
|
|
7
8
|
export * from '@rive-app/canvas';
|
package/dist/types/types.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type UseRiveOptions = {
|
|
|
5
5
|
useDevicePixelRatio: boolean;
|
|
6
6
|
fitCanvasToArtboardHeight: boolean;
|
|
7
7
|
useOffscreenRenderer: boolean;
|
|
8
|
+
shouldResizeCanvasToContainer: boolean;
|
|
8
9
|
};
|
|
9
10
|
export type Dimensions = {
|
|
10
11
|
width: number;
|
|
@@ -13,6 +14,7 @@ export type Dimensions = {
|
|
|
13
14
|
/**
|
|
14
15
|
* @typedef RiveState
|
|
15
16
|
* @property canvas - Canvas element the Rive Animation is attached to.
|
|
17
|
+
* @property container - Container element of the canvas.
|
|
16
18
|
* @property setCanvasRef - Ref callback to be passed to the canvas element.
|
|
17
19
|
* @property setContainerRef - Ref callback to be passed to the container element
|
|
18
20
|
* of the canvas. This is optional, however if not used then the hook will
|
|
@@ -22,6 +24,7 @@ export type Dimensions = {
|
|
|
22
24
|
*/
|
|
23
25
|
export type RiveState = {
|
|
24
26
|
canvas: HTMLCanvasElement | null;
|
|
27
|
+
container: HTMLElement | null;
|
|
25
28
|
setCanvasRef: RefCallback<HTMLCanvasElement>;
|
|
26
29
|
setContainerRef: RefCallback<HTMLElement>;
|
|
27
30
|
rive: Rive | null;
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* - User controls zoom settings on the browser
|
|
9
|
-
*
|
|
10
|
-
* Source: https://github.com/rexxars/use-device-pixel-ratio/blob/main/index.ts
|
|
11
|
-
*
|
|
12
|
-
* @returns dpr: Number - Device pixel ratio; ratio of physical px to resolution in CSS pixels for current device
|
|
13
|
-
*/
|
|
14
|
-
export declare function useDevicePixelRatio(): number;
|
|
15
|
-
export declare function getDevicePixelRatio(): number;
|
|
1
|
+
import { UseRiveOptions } from './types';
|
|
2
|
+
export declare function getOptions(opts: Partial<UseRiveOptions>): {
|
|
3
|
+
useDevicePixelRatio: boolean;
|
|
4
|
+
fitCanvasToArtboardHeight: boolean;
|
|
5
|
+
useOffscreenRenderer: boolean;
|
|
6
|
+
shouldResizeCanvasToContainer: boolean;
|
|
7
|
+
} & Partial<UseRiveOptions>;
|