@react-three/drei 9.112.1 → 9.114.0
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/core/GradientTexture.d.ts +1 -1
- package/core/GradientTexture.js +0 -1
- package/core/SpriteAnimator.cjs.js +1 -1
- package/core/SpriteAnimator.d.ts +2 -1
- package/core/SpriteAnimator.js +2 -1
- package/core/StatsGl.cjs.js +1 -1
- package/core/StatsGl.d.ts +5 -1
- package/core/StatsGl.js +15 -5
- package/core/useSpriteLoader.cjs.js +1 -1
- package/core/useSpriteLoader.d.ts +1 -1
- package/core/useSpriteLoader.js +16 -8
- package/index.cjs.js +1 -1
- package/materials/MeshRefractionMaterial.d.ts +0 -3
- package/package.json +2 -2
|
@@ -13,6 +13,6 @@ type Props = {
|
|
|
13
13
|
type?: GradientType;
|
|
14
14
|
innerCircleRadius?: number;
|
|
15
15
|
outerCircleRadius?: string | number;
|
|
16
|
-
} & JSX.IntrinsicElements['texture']
|
|
16
|
+
} & Omit<JSX.IntrinsicElements['texture'], 'type'>;
|
|
17
17
|
export declare function GradientTexture({ stops, colors, size, width, type, innerCircleRadius, outerCircleRadius, ...props }: Props): React.JSX.Element;
|
|
18
18
|
export {};
|
package/core/GradientTexture.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),r=require("react"),t=require("@react-three/fiber"),n=require("three"),u=require("./Instances.cjs.js"),a=require("./useSpriteLoader.cjs.js");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function s(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}require("react-composer"),require("../helpers/deprecated.cjs.js");var l=c(e),o=s(r),f=s(n);const i=o.createContext(null);const m=o.forwardRef((({startFrame:e,endFrame:r,fps:n,frameName:c,textureDataURL:s,textureImageURL:m,loop:p,numberOfFrames:d,autoPlay:h,animationNames:y,onStart:
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),r=require("react"),t=require("@react-three/fiber"),n=require("three"),u=require("./Instances.cjs.js"),a=require("./useSpriteLoader.cjs.js");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function s(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}require("react-composer"),require("../helpers/deprecated.cjs.js");var l=c(e),o=s(r),f=s(n);const i=o.createContext(null);const m=o.forwardRef((({startFrame:e,endFrame:r,fps:n,frameName:c,textureDataURL:s,textureImageURL:m,loop:p,numberOfFrames:d,autoPlay:h,animationNames:y,onStart:v,onEnd:E,onLoopEnd:w,onFrame:g,play:x,pause:b,flipX:S,alphaTest:F,children:j,asSprite:R,offset:M,playBackwards:A,resetOnEnd:O,maxItems:z,instanceItems:N,spriteDataset:q,canvasRenderingContext2DSettings:L,...T},k)=>{var D;const I=o.useRef(),P=o.useRef(null),_=o.useRef(),C=o.useRef(),U=o.useRef(window.performance.now()),B=o.useRef(e||0),G=o.useRef(c||""),H=1e3/(n||30),[J,V]=o.useState(new f.Texture),W=o.useRef(0),[X,K]=o.useState([1,1,1]),Q=S?-1:1,[Y,Z]=o.useState(null==R||R),$=o.useRef(b),ee=o.useRef(M),re=o.useRef(!1),te=o.useRef([]),{spriteObj:ne,loadJsonAndTexture:ue}=a.useSpriteLoader(null,null,y,d,void 0,L);function ae(){}const ce=o.useMemo((()=>({current:ee.current,offset:ee.current,imageUrl:m,reset:ae,hasEnded:!1,ref:k})),[m,q]);o.useImperativeHandle(k,(()=>I.current),[]),o.useLayoutEffect((()=>{ee.current=M}),[M]);const se=(e,r)=>{const t=r/e;return C.current&&C.current.scale.set(1,t,1),[1,t,1]};o.useEffect((()=>{var e;q?le(null==q||null==(e=q.spriteTexture)?void 0:e.clone(),q.spriteData):ue(m,s)}),[q]),o.useEffect((()=>{var e;ne&&le(null==ne||null==(e=ne.spriteTexture)?void 0:e.clone(),null==ne?void 0:ne.spriteData)}),[ne]),o.useEffect((()=>{Z(null==R||R)}),[R]),o.useEffect((()=>{ce.hasEnded=!1,P.current&&!0===A?B.current=P.current.frames.length-1:B.current=0}),[A]),o.useLayoutEffect((()=>{oe()}),[J,S]),o.useEffect((()=>{h&&($.current=!1)}),[h]),o.useLayoutEffect((()=>{if(G.current!==c&&c&&(B.current=0,G.current=c,ce.hasEnded=!1,P.current)){const{w:e,h:r}=ie(P.current.frames).sourceSize,t=se(e,r);K(t)}}),[c]);const le=(e,r=null)=>{if(null===r){if(d){const t=e.image.width,n=e.image.height;W.current=d,A&&(B.current=d-1),P.current={frames:[],meta:{version:"1.0",size:{w:t,h:n},scale:"1"}},P.current.frames=r}}else{P.current=r,W.current=P.current.frames.length,A&&(B.current=W.current-1);const{w:t,h:n}=ie(P.current.frames).sourceSize,u=se(t,n);K(u),_.current&&(_.current.map=e)}if(N)for(var t=0;t<N.length;t++){const e=Object.keys(P.current.frames),r=e[Math.floor(Math.random()*e.length)];te.current.push({key:t,frames:P.current.frames,selectedFrame:r,offset:{x:0,y:0}})}V(e)},oe=()=>{if(!P.current)return;const{meta:{size:e},frames:r}=P.current,{w:t,h:n}=Array.isArray(r)?r[0].sourceSize:c&&r[c]?r[c][0].sourceSize:{w:0,h:0};_.current.map.wrapS=_.current.map.wrapT=f.RepeatWrapping,_.current.map.center.set(0,0),_.current.map.repeat.set(1*Q/(e.w/t),1/(e.h/n));const u=1/((e.h-1)/n);_.current.map.offset.x=0,_.current.map.offset.y=1-u,v&&v({currentFrameName:c,currentFrame:B.current})},fe=(e,r,t,n)=>{var u=void 0===M?ce.current:M;const a=B.current;let c=0,s=0;se(e,r);const l=Math.round((t.w-1)/e),o=Math.round((t.h-1)/r);if(!n[a])return;const{frame:{x:f,y:i},sourceSize:{w:m,h:p}}=n[a],d=1/l,h=1/o;if(c=Q>0?d*(f/m):d*(f/m)-_.current.map.repeat.x,s=Math.abs(1-h)-h*(i/p),_.current.map.offset.x=c,_.current.map.offset.y=s,null!=u){let e=Math.floor(u*n.length);e=Math.max(0,Math.min(e,n.length-1)),isNaN(e)&&(e=0),B.current=e}else A?B.current-=1:B.current+=1};t.useFrame(((t,n)=>{var u,a;null!=(u=P.current)&&u.frames&&null!=(a=_.current)&&a.map&&($.current||ce.hasEnded||!h&&!x||((()=>{const t=window.performance.now(),n=t-U.current,{meta:{size:u},frames:a}=P.current,{w:s,h:l}=ie(a).sourceSize,o=Array.isArray(a)?a:c?a[c]:[],f=r||o.length-1;var i=void 0===M?ce.current:M,m=A?B.current<0:B.current>f,d=A?B.current===f:0===B.current,h=A?B.current<0:B.current>=f;if(m){if(B.current=p&&null!=e?e:0,A&&(B.current=f),p?null==w||w({currentFrameName:c,currentFrame:B.current}):(null==E||E({currentFrameName:c,currentFrame:B.current}),ce.hasEnded=!O,O&&($.current=!0)),!p)return}else d&&(null==v||v({currentFrameName:c,currentFrame:B.current}));void 0!==i&&h?!1===re.current&&(null==E||E({currentFrameName:c,currentFrame:B.current}),re.current=!0):re.current=!1,n<=H||(U.current=t-n%H,fe(s,l,u,o))})(),g&&g({currentFrameName:G.current,currentFrame:B.current})))}));const ie=e=>{if(Array.isArray(e))return e[0];if("object"==typeof e&&null!==e){const r=Object.keys(e);return c?e[c][0]:e[r[0]][0]}return{w:0,h:0}};return o.createElement("group",l.default({},T,{ref:I,scale:function(e,r){let t=[];return"number"==typeof r?t=[r,r,r]:Array.isArray(r)?t=r:r instanceof f.Vector3&&(t=[r.x,r.y,r.z]),e.map(((e,r)=>e*t[r]))}(null!=X?X:[1,1,1],null!==(D=T.scale)&&void 0!==D?D:1)}),o.createElement(i.Provider,{value:ce},o.createElement(o.Suspense,{fallback:null},Y&&o.createElement("sprite",{ref:C,scale:1},o.createElement("spriteMaterial",{premultipliedAlpha:!1,toneMapped:!1,ref:_,map:J,transparent:!0,alphaTest:null!=F?F:0})),!Y&&o.createElement(u.Instances,{limit:null!=z?z:1},o.createElement("planeGeometry",{args:[1,1]}),o.createElement("meshBasicMaterial",{premultipliedAlpha:!1,toneMapped:!1,side:f.DoubleSide,ref:_,map:J,transparent:!0,alphaTest:null!=F?F:0}),(null!=N?N:[0]).map(((e,r)=>o.createElement(u.Instance,{key:r,ref:1===(null==N?void 0:N.length)?C:null,position:e,scale:1}))))),j))}));exports.SpriteAnimator=m,exports.useSpriteAnimator=function(){return o.useContext(i)};
|
package/core/SpriteAnimator.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export type SpriteAnimatorProps = {
|
|
|
26
26
|
maxItems?: number;
|
|
27
27
|
instanceItems?: any[];
|
|
28
28
|
spriteDataset?: any;
|
|
29
|
+
canvasRenderingContext2DSettings?: CanvasRenderingContext2DSettings;
|
|
29
30
|
} & JSX.IntrinsicElements['group'];
|
|
30
31
|
type SpriteAnimatorState = {
|
|
31
32
|
current: number | undefined;
|
|
@@ -33,6 +34,6 @@ type SpriteAnimatorState = {
|
|
|
33
34
|
hasEnded: boolean | undefined;
|
|
34
35
|
ref: React.MutableRefObject<any> | undefined | null | ((instance: any) => void);
|
|
35
36
|
};
|
|
36
|
-
export declare function useSpriteAnimator(): SpriteAnimatorState;
|
|
37
|
+
export declare function useSpriteAnimator(): SpriteAnimatorState | null;
|
|
37
38
|
export declare const SpriteAnimator: React.FC<SpriteAnimatorProps>;
|
|
38
39
|
export {};
|
package/core/SpriteAnimator.js
CHANGED
|
@@ -36,6 +36,7 @@ const SpriteAnimator = /* @__PURE__ */React.forwardRef(({
|
|
|
36
36
|
maxItems,
|
|
37
37
|
instanceItems,
|
|
38
38
|
spriteDataset,
|
|
39
|
+
canvasRenderingContext2DSettings,
|
|
39
40
|
...props
|
|
40
41
|
}, fref) => {
|
|
41
42
|
var _props$scale;
|
|
@@ -59,7 +60,7 @@ const SpriteAnimator = /* @__PURE__ */React.forwardRef(({
|
|
|
59
60
|
const {
|
|
60
61
|
spriteObj,
|
|
61
62
|
loadJsonAndTexture
|
|
62
|
-
} = useSpriteLoader(null, null, animationNames, numberOfFrames);
|
|
63
|
+
} = useSpriteLoader(null, null, animationNames, numberOfFrames, undefined, canvasRenderingContext2DSettings);
|
|
63
64
|
//
|
|
64
65
|
|
|
65
66
|
function reset() {}
|
package/core/StatsGl.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@react-three/fiber"),t=require("react"),r=require("stats-gl");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function l(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var n=l(t),u=o(r);const s=n.forwardRef((({className:t,parent:r,id:o,clearStatsGlStyle:l,...s},a)=>{const c=e.useThree((e=>e.gl)),d=n.useMemo((()=>{const e=new u.default({...s});return e.init(c),e}),[c]);return n.useImperativeHandle(a,(()=>d.dom)),n.useEffect((()=>{if(d){const n=r&&r.current||document.body;null==n||n.appendChild(d.dom),d.dom.querySelectorAll("canvas").forEach((e=>{e.style.removeProperty("position")})),o&&(d.dom.id=o),l&&d.dom.removeAttribute("style"),d.dom.removeAttribute("style");const u=(null!=t?t:"").split(" ").filter((e=>e));u.length&&d.dom.classList.add(...u);const s=e.addAfterEffect((()=>d.update()));return()=>{u.length&&d.dom.classList.remove(...u),null==n||n.removeChild(d.dom),s()}}}),[r,d,t,o,l]),null}));s.displayName="StatsGl",exports.StatsGl=s;
|
package/core/StatsGl.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { ForwardRefComponent } from '../helpers/ts-utils';
|
|
1
2
|
import * as React from 'react';
|
|
2
3
|
import Stats from 'stats-gl';
|
|
3
4
|
type Props = Partial<Stats> & {
|
|
5
|
+
id?: string;
|
|
6
|
+
clearStatsGlStyle?: boolean;
|
|
4
7
|
showPanel?: number;
|
|
5
8
|
className?: string;
|
|
6
9
|
parent?: React.RefObject<HTMLElement>;
|
|
10
|
+
ref?: React.RefObject<HTMLElement>;
|
|
7
11
|
};
|
|
8
|
-
export declare
|
|
12
|
+
export declare const StatsGl: ForwardRefComponent<Props, HTMLDivElement>;
|
|
9
13
|
export {};
|
package/core/StatsGl.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
1
|
import { useThree, addAfterEffect } from '@react-three/fiber';
|
|
2
|
+
import * as React from 'react';
|
|
3
3
|
import Stats from 'stats-gl';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const StatsGl = /* @__PURE__ */React.forwardRef(({
|
|
6
6
|
className,
|
|
7
7
|
parent,
|
|
8
|
+
id,
|
|
9
|
+
clearStatsGlStyle,
|
|
8
10
|
...props
|
|
9
|
-
}) {
|
|
11
|
+
}, fref) => {
|
|
10
12
|
const gl = useThree(state => state.gl);
|
|
11
13
|
const stats = React.useMemo(() => {
|
|
12
14
|
const stats = new Stats({
|
|
@@ -15,10 +17,17 @@ function StatsGl({
|
|
|
15
17
|
stats.init(gl);
|
|
16
18
|
return stats;
|
|
17
19
|
}, [gl]);
|
|
20
|
+
React.useImperativeHandle(fref, () => stats.dom);
|
|
18
21
|
React.useEffect(() => {
|
|
19
22
|
if (stats) {
|
|
20
23
|
const node = parent && parent.current || document.body;
|
|
21
24
|
node == null || node.appendChild(stats.dom);
|
|
25
|
+
stats.dom.querySelectorAll('canvas').forEach(canvas => {
|
|
26
|
+
canvas.style.removeProperty('position');
|
|
27
|
+
});
|
|
28
|
+
if (id) stats.dom.id = id;
|
|
29
|
+
if (clearStatsGlStyle) stats.dom.removeAttribute('style');
|
|
30
|
+
stats.dom.removeAttribute('style');
|
|
22
31
|
const classNames = (className !== null && className !== void 0 ? className : '').split(' ').filter(cls => cls);
|
|
23
32
|
if (classNames.length) stats.dom.classList.add(...classNames);
|
|
24
33
|
const end = addAfterEffect(() => stats.update());
|
|
@@ -28,8 +37,9 @@ function StatsGl({
|
|
|
28
37
|
end();
|
|
29
38
|
};
|
|
30
39
|
}
|
|
31
|
-
}, [parent, stats, className]);
|
|
40
|
+
}, [parent, stats, className, id, clearStatsGlStyle]);
|
|
32
41
|
return null;
|
|
33
|
-
}
|
|
42
|
+
});
|
|
43
|
+
StatsGl.displayName = 'StatsGl';
|
|
34
44
|
|
|
35
45
|
export { StatsGl };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("three"),r=require("@react-three/fiber"),t=require("react");function o(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var o=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,o.get?o:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var a=o(e),n=o(t);const s=e=>{if(Array.isArray(e))return e[0];if("object"==typeof e&&null!==e){return e[Object.keys(e)[0]][0]}return{w:0,h:0}},i=e=>{for(let r=3;r<e.length;r+=4)if(0!==e[r])return!1;return!0},u=(e,r,t,o)=>{const n=r*(o.aspect>e/r?o.width/e:o.height/r),s=e*(o.aspect>e/r?o.width/e:o.height/r)*t,i=n*t;let u=Math.min(1,s),c=Math.min(1,i);return s>1&&(u=1,c=i/s*1),new a.Vector3(u,c,1)};function c(o,c,f,l,m){const
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("three"),r=require("@react-three/fiber"),t=require("react");function o(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var o=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,o.get?o:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var a=o(e),n=o(t);const s=e=>{if(Array.isArray(e))return e[0];if("object"==typeof e&&null!==e){return e[Object.keys(e)[0]][0]}return{w:0,h:0}},i=e=>{for(let r=3;r<e.length;r+=4)if(0!==e[r])return!1;return!0},u=(e,r,t,o)=>{const n=r*(o.aspect>e/r?o.width/e:o.height/r),s=e*(o.aspect>e/r?o.width/e:o.height/r)*t,i=n*t;let u=Math.min(1,s),c=Math.min(1,i);return s>1&&(u=1,c=i/s*1),new a.Vector3(u,c,1)};function c(o,c,f,l,m,h){const d=r.useThree((e=>e.viewport)),w=n.useRef(null),p=n.useRef(0),[g,y]=t.useState(null),[x,S]=n.useState(new a.Texture),b=new a.TextureLoader,[v,L]=t.useState(null);function j(e){if(!e&&!o)throw new Error("Either textureUrl or input must be provided");const r=null!=e?e:o;if(!r)throw new Error("A valid texture URL must be provided");new Promise((e=>{b.load(r,e)})).then((e=>{z(null,e)}))}function O(e,r,t){const o=fetch(e).then((e=>e.json())),a=new Promise((e=>{b.load(r,e)}));Promise.all([o,a]).then((e=>{t(e[0],e[1])}))}n.useLayoutEffect((()=>(c&&o?O(c,o,z):o&&j(),()=>{o&&r.useLoader.clear(e.TextureLoader,o)})),[]);const z=(e,r)=>{let t=new a.Vector3(1,1,1);if(null===e){if(r&&l){const e=r.image.width,o=r.image.height;p.current=l;const{rows:a,columns:n,frameWidth:s,frameHeight:i,emptyFrames:c}=A(r,l);w.current={frames:[],meta:{version:"1.0",size:{w:e,h:o},rows:a,columns:n,frameWidth:s,frameHeight:i,scale:"1"}};for(let e=0;e<a;e++)for(let r=0;r<n;r++){(null!=c?c:[]).some((t=>t.row===e&&t.col===r))||w.current.frames.push({frame:{x:r*s,y:e*i,w:s,h:i},rotated:!1,trimmed:!1,spriteSourceSize:{x:0,y:0,w:s,h:i},sourceSize:{w:s,h:i}})}t=u(s,i,.1,d)}w.current.frames=M(w.current.frames)}else if(r){w.current=e,w.current.frames=E(),p.current=Array.isArray(e.frames)?e.frames.length:Object.keys(e.frames).length;const{w:r,h:o}=s(e.frames).sourceSize;t=u(r,o,.1,d)}y(w.current),"encoding"in r?r.encoding=3001:r.colorSpace=a.SRGBColorSpace,S(r),L({spriteTexture:r,spriteData:w.current,aspect:t})},A=(e,r)=>{if(e.image){const t=document.createElement("canvas"),o=t.getContext("2d",h);if(!o)throw new Error("Failed to get 2d context");t.width=e.image.width,t.height=e.image.height,o.drawImage(e.image,0,0);const a=e.image.width,n=e.image.height,s=Math.round(Math.sqrt(r*(a/n))),u=Math.round(r/s),c=a/s,f=n/u,l=[];for(let e=0;e<u;e++)for(let t=0;t<s;t++){if(e*s+t>=r){l.push({row:e,col:t});continue}const a=o.getImageData(t*c,e*f,c,f).data;i(a)&&l.push({row:e,col:t})}return{rows:u,columns:s,frameWidth:c,frameHeight:f,emptyFrames:l}}return{rows:0,columns:0,frameWidth:0,frameHeight:0,emptyFrames:[]}},E=()=>{const e={},r=w.current,t=f;if(t&&Array.isArray(r.frames)){for(let o=0;o<t.length;o++){e[t[o]]=[];for(const a of r.frames){const r=a.frame,n=r.x,s=r.y,i=r.w,u=r.h,c=a.sourceSize.w,f=a.sourceSize.h;"string"==typeof a.filename&&-1!==a.filename.toLowerCase().indexOf(t[o].toLowerCase())&&e[t[o]].push({x:n,y:s,w:i,h:u,frame:r,sourceSize:{w:c,h:f}})}}for(const r in e)e[r].frame=M(e[r]);return e}if(t&&"object"==typeof r.frames){for(let o=0;o<t.length;o++){e[t[o]]=[];for(const a in r.frames){const n=r.frames[a],s=n.frame,i=s.x,u=s.y,c=s.w,f=s.h,l=n.sourceSize.w,m=n.sourceSize.h;"string"==typeof a&&-1!==a.toLowerCase().indexOf(t[o].toLowerCase())&&e[t[o]].push({x:i,y:u,w:c,h:f,frame:s,sourceSize:{w:l,h:m}})}}for(const r in e)e[r].frame=M(e[r]);return e}{let e=[];for(const t in r.frames)e.push(r.frames[t]);return e=M(e),e}},M=e=>{let r=null;for(const t of e){const{w:e,h:o}=t.frame,a=e*o;(!r||a>r.area)&&(r={...t.frame,area:a})}for(const a of e){var t;const{w:e,h:n}=a.frame,s=e*n;var o;if(s===(null==(t=r)?void 0:t.area))a.scaleRatio=1;else a.scaleRatio=Math.sqrt(s/(null==(o=r)?void 0:o.area))}return e};return n.useLayoutEffect((()=>{null==m||m(x,g)}),[x,g]),{spriteObj:v,loadJsonAndTexture:function(e,r){r&&e?O(r,e,z):j(e)}}}c.preload=t=>r.useLoader.preload(e.TextureLoader,t),c.clear=t=>r.useLoader.clear(e.TextureLoader,t),exports.calculateAspectRatio=u,exports.checkIfFrameIsEmpty=i,exports.getFirstItem=s,exports.useSpriteLoader=c;
|
|
@@ -3,7 +3,7 @@ import * as THREE from 'three';
|
|
|
3
3
|
export declare const getFirstItem: (param: any) => any;
|
|
4
4
|
export declare const checkIfFrameIsEmpty: (frameData: Uint8ClampedArray) => boolean;
|
|
5
5
|
export declare const calculateAspectRatio: (width: number, height: number, factor: number, v: any) => THREE.Vector3;
|
|
6
|
-
export declare function useSpriteLoader<Url extends string>(input?: Url | null, json?: string | null, animationNames?: string[] | null, numberOfFrames?: number | null, onLoad?: (texture: Texture, textureData?: any) => void): any;
|
|
6
|
+
export declare function useSpriteLoader<Url extends string>(input?: Url | null, json?: string | null, animationNames?: string[] | null, numberOfFrames?: number | null, onLoad?: (texture: Texture, textureData?: any) => void, canvasRenderingContext2DSettings?: CanvasRenderingContext2DSettings): any;
|
|
7
7
|
export declare namespace useSpriteLoader {
|
|
8
8
|
var preload: (url: string) => undefined;
|
|
9
9
|
var clear: (input: string) => void;
|
package/core/useSpriteLoader.js
CHANGED
|
@@ -43,7 +43,7 @@ const calculateAspectRatio = (width, height, factor, v) => {
|
|
|
43
43
|
}
|
|
44
44
|
return new THREE.Vector3(finalMaxScaleW, finalMaxScaleH, 1);
|
|
45
45
|
};
|
|
46
|
-
function useSpriteLoader(input, json, animationNames, numberOfFrames, onLoad) {
|
|
46
|
+
function useSpriteLoader(input, json, animationNames, numberOfFrames, onLoad, canvasRenderingContext2DSettings) {
|
|
47
47
|
const v = useThree(state => state.viewport);
|
|
48
48
|
const spriteDataRef = React.useRef(null);
|
|
49
49
|
const totalFrames = React.useRef(0);
|
|
@@ -73,13 +73,18 @@ function useSpriteLoader(input, json, animationNames, numberOfFrames, onLoad) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
function loadStandaloneSprite(textureUrl) {
|
|
76
|
-
if (textureUrl
|
|
77
|
-
new
|
|
78
|
-
textureLoader.load(textureUrl !== null && textureUrl !== void 0 ? textureUrl : input, resolve);
|
|
79
|
-
}).then(texture => {
|
|
80
|
-
parseSpriteData(null, texture);
|
|
81
|
-
});
|
|
76
|
+
if (!textureUrl && !input) {
|
|
77
|
+
throw new Error('Either textureUrl or input must be provided');
|
|
82
78
|
}
|
|
79
|
+
const validUrl = textureUrl !== null && textureUrl !== void 0 ? textureUrl : input;
|
|
80
|
+
if (!validUrl) {
|
|
81
|
+
throw new Error('A valid texture URL must be provided');
|
|
82
|
+
}
|
|
83
|
+
new Promise(resolve => {
|
|
84
|
+
textureLoader.load(validUrl, resolve);
|
|
85
|
+
}).then(texture => {
|
|
86
|
+
parseSpriteData(null, texture);
|
|
87
|
+
});
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
/**
|
|
@@ -183,7 +188,10 @@ function useSpriteLoader(input, json, animationNames, numberOfFrames, onLoad) {
|
|
|
183
188
|
const getRowsAndColumns = (texture, totalFrames) => {
|
|
184
189
|
if (texture.image) {
|
|
185
190
|
const canvas = document.createElement('canvas');
|
|
186
|
-
const ctx = canvas.getContext('2d');
|
|
191
|
+
const ctx = canvas.getContext('2d', canvasRenderingContext2DSettings);
|
|
192
|
+
if (!ctx) {
|
|
193
|
+
throw new Error('Failed to get 2d context');
|
|
194
|
+
}
|
|
187
195
|
canvas.width = texture.image.width;
|
|
188
196
|
canvas.height = texture.image.height;
|
|
189
197
|
ctx.drawImage(texture.image, 0, 0);
|