New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@pixi/react

Package Overview
Dependencies
Maintainers
5
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pixi/react - npm Package Compare versions

Comparing version 8.0.0-dev.0aaf87a to 8.0.0-dev.8ae89f5

dist/index.js

26

lib/helpers/appendChild.js

@@ -1,18 +0,12 @@

/** @typedef {import('pixi.js').Container} Container */
/** @typedef {import('../types/HostContainer.js').HostContainer} HostContainer */
'use strict';
/**
* Adds elements to our scene and attaches geometry and material to meshes.
*
* @param {HostContainer & Container} parentInstance
* @param {HostContainer & Container} child
*/
export function appendChild(parentInstance, child)
{
if (!child)
{
return;
}
"use strict";
function appendChild(parentInstance, child) {
if (!child) {
return;
}
parentInstance.addChild(child);
}
parentInstance.addChild(child);
}
exports.appendChild = appendChild;
//# sourceMappingURL=appendChild.js.map

@@ -1,85 +0,41 @@

import { Graphics } from 'pixi.js';
import { pruneKeys } from './pruneKeys.js';
'use strict';
/**
* Apply properties to Pixi.js instance.
*
* @param {{ [key: string]: any }} instance An instance?
* @param {{ [key: string]: any }} newProps New props.
* @param {{ [key: string]: any }} [oldProps] Old props.
*/
export function applyProps(instance, newProps, oldProps = {})
{
// Filter identical props, event handlers, and reserved keys
const identicalProps = Object
.keys(newProps)
.filter((key) => newProps[key] === oldProps[key]);
var pixi_js = require('pixi.js');
var pruneKeys = require('./pruneKeys.js');
if ((instance instanceof Graphics) && !identicalProps.includes('draw'))
{
newProps.draw?.(instance);
}
const handlers = Object.keys(newProps).filter((key) =>
{
const isFunction = typeof newProps[key] === 'function';
return isFunction && key.startsWith('on');
"use strict";
function applyProps(instance, newProps, oldProps = {}) {
const identicalProps = Object.keys(newProps).filter((key) => newProps[key] === oldProps[key]);
if (instance instanceof pixi_js.Graphics && !identicalProps.includes("draw")) {
newProps.draw?.(instance);
}
const handlers = Object.keys(newProps).filter((key) => {
const isFunction = typeof newProps[key] === "function";
return isFunction && key.startsWith("on");
});
const props = pruneKeys.pruneKeys(newProps, [
...identicalProps,
...handlers,
"children",
"draw",
"key",
"ref"
]);
if (Object.keys(props).length) {
Object.entries(props).forEach(([key, value]) => {
instance[key] = value;
});
}
if (handlers.length) {
instance.__handlers = handlers.reduce(
(acc, key) => ({
...acc,
[key]: newProps[key]
}),
{}
);
}
}
const props = pruneKeys(newProps, [
...identicalProps,
...handlers,
'children',
'draw',
'key',
'ref',
]);
// Mutate our Pixi.js element
if (Object.keys(props).length)
{
Object.entries(props).forEach(([key, value]) =>
{
// const target = instance[key]
// const isColor = target instanceof THREE.Color
// // Prefer to use properties' copy and set methods
// // otherwise, mutate the property directly
// if (target?.set) {
// if (target.constructor.name === value.constructor.name) {
// target.copy(value)
// } else if (Array.isArray(value)) {
// target.set(...value)
// } else if (!isColor && target?.setScalar) {
// // Allow shorthand like scale={1}
// target.setScalar(value)
// } else {
// target.set(value)
// }
// // Auto-convert sRGB colors
// if (isColor) {
// target.convertSRGBToLinear()
// }
// } else {
// instance[key] = value
// }
instance[key] = value;
});
}
// Collect event handlers.
// We put this on an invalid prop so Pixi.js doesn't serialize handlers
// if you do ref.current.clone() or ref.current.toJSON()
if (handlers.length)
{
instance.__handlers = handlers.reduce(
(acc, key) => ({
...acc,
[key]: newProps[key],
}),
{},
);
}
}
exports.applyProps = applyProps;
//# sourceMappingURL=applyProps.js.map

@@ -1,14 +0,14 @@

/**
* Converts a string to PascalCase.
*
* @template {string} S
* @param {S} string The string to be converted.
* @returns {S} The converted string.
*/
export function convertStringToPascalCase(string)
{
const firstChar = string.charAt(0);
const rest = string.substring(1);
'use strict';
return /** @type {S} */ (`${firstChar.toUpperCase()}${rest}`);
"use strict";
function convertStringToPascalCase(string) {
const firstChar = string.charAt(0);
const rest = string.substring(1);
return (
/** @type {S} */
`${firstChar.toUpperCase()}${rest}`
);
}
exports.convertStringToPascalCase = convertStringToPascalCase;
//# sourceMappingURL=convertStringToPascalCase.js.map

@@ -1,38 +0,31 @@

import * as PIXI from 'pixi.js';
import { applyProps } from './applyProps.js';
import { convertStringToPascalCase } from './convertStringToPascalCase.js';
'use strict';
/** @typedef {import('../types/PixiElements.js').PixiElements} PixiElements */
var applyProps = require('./applyProps.js');
var catalogue = require('./catalogue.js');
var convertStringToPascalCase = require('./convertStringToPascalCase.js');
/**
* @param {keyof PixiElements} type
* @param {Record<string, unknown>} props
* @returns
*/
export function createInstance(type, props)
{
const { args } = props;
"use strict";
function createInstance(type, props) {
const { args } = props;
const name = convertStringToPascalCase.convertStringToPascalCase(type);
const TARGET = (
/** @type {new (...args: any[]) => any} */
catalogue.catalogue[name]
);
if (!TARGET) {
throw new Error(
`@react/pixi: ${name} is not part of the PIXI namespace! Did you forget to extend?`
);
}
let instance;
if (Array.isArray(args)) {
instance = new TARGET(...args);
} else {
instance = new TARGET(args);
}
applyProps.applyProps(instance, props);
return instance;
}
// Convert lowercase primitive to PascalCase
const name = convertStringToPascalCase(type);
// Get class from Pixi.js namespace
const TARGET = /** @type {new (...args: any[]) => any} */ (PIXI[name]);
let instance;
// Create instance
if (Array.isArray(args))
{
instance = new TARGET(...args);
}
else
{
instance = new TARGET(args);
}
// Set initial props
applyProps(instance, props);
return instance;
}
exports.createInstance = createInstance;
//# sourceMappingURL=createInstance.js.map

@@ -1,15 +0,12 @@

/**
* Prunes keys from an object.
*
* @param {object} object The object to be pruned.
* @param {...any} keys The keys to prune.
* @returns {object} The pruned object.
*/
export function pruneKeys(object, ...keys)
{
const keysToRemove = new Set(keys.flat());
const objectEntries = Object.entries(object);
const filteredObjectEntries = objectEntries.filter(([key]) => !keysToRemove.has(key));
'use strict';
return Object.fromEntries(filteredObjectEntries);
"use strict";
function pruneKeys(object, ...keys) {
const keysToRemove = new Set(keys.flat());
const objectEntries = Object.entries(object);
const filteredObjectEntries = objectEntries.filter(([key]) => !keysToRemove.has(key));
return Object.fromEntries(filteredObjectEntries);
}
exports.pruneKeys = pruneKeys;
//# sourceMappingURL=pruneKeys.js.map

@@ -1,21 +0,14 @@

/** @typedef {import('pixi.js').Container} Container */
/** @typedef {import('../types/HostContainer.js').HostContainer} HostContainer */
'use strict';
/**
* Removes elements from our scene and disposes of them.
*
* @param {HostContainer & Container} _container Unused.
* @param {HostContainer & Container} child The child to be removed.
*/
export function removeChild(_container, child)
{
if (!child)
{
return;
}
"use strict";
function removeChild(_container, child) {
if (!child) {
return;
}
if (child.destroy) {
child.destroy();
}
}
if (child.destroy)
{
child.destroy();
}
}
exports.removeChild = removeChild;
//# sourceMappingURL=removeChild.js.map

@@ -1,56 +0,36 @@

import { Assets } from 'pixi.js';
import {
useEffect,
useState,
} from 'react';
'use strict';
/**
* @typedef {import('pixi.js').ProgressCallback} ProgressCallback
* @typedef {import('pixi.js').Texture} Texture
* @typedef {import('pixi.js').UnresolvedAsset} UnresolvedAsset
*/
var pixi_js = require('pixi.js');
var react = require('react');
/**
* Loads assets, returning a hash of assets once they're loaded.
*
* @param {UnresolvedAsset} options Asset options.
* @param {ProgressCallback} [onProgress] A function to be called when the asset loader reports loading progress.
* @returns {null | Texture} A hash of textures, keyed by their URLs. This will be `null` until the assets are loaded.
*/
export function useAsset(options, onProgress)
{
const [isLoaded, setIsLoaded] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [texture, setTexture] = useState(null);
"use strict";
function useAsset(options, onProgress) {
const [isLoaded, setIsLoaded] = react.useState(false);
const [isLoading, setIsLoading] = react.useState(false);
const [texture, setTexture] = react.useState(null);
react.useEffect(() => {
setIsLoaded(false);
}, [options]);
react.useEffect(() => {
if (!isLoaded && !isLoading) {
setIsLoading(true);
const assetKey = (
/** @type {string} */
options.alias ?? options.src
);
pixi_js.Assets.add(options);
pixi_js.Assets.load(assetKey, onProgress).then((texture2) => {
setTexture(texture2);
setIsLoaded(true);
setIsLoading(false);
}).catch(() => setIsLoading(false));
}
}, [
isLoaded,
isLoading
]);
return texture;
}
useEffect(() =>
{
setIsLoaded(false);
}, [options]);
useEffect(() =>
{
if (!isLoaded && !isLoading)
{
setIsLoading(true);
const assetKey = /** @type {string} */ (options.alias ?? options.src);
Assets.add(options);
Assets
.load(assetKey, onProgress)
.then((texture) =>
{
setTexture(texture);
setIsLoaded(true);
setIsLoading(false);
})
.catch(() => setIsLoading(false));
}
}, [
isLoaded,
isLoading,
]);
return texture;
}
exports.useAsset = useAsset;
//# sourceMappingURL=useAsset.js.map

@@ -1,2 +0,12 @@

export { useAsset } from './hooks/useAsset.js';
export { render } from './render.js';
'use strict';
var useAsset = require('./hooks/useAsset.js');
var useExtend = require('./hooks/useExtend.js');
var render = require('./render.js');
"use strict";
exports.useAsset = useAsset.useAsset;
exports.useExtend = useExtend.useExtend;
exports.render = render.render;
//# sourceMappingURL=index.js.map

@@ -1,121 +0,95 @@

/* eslint-disable no-empty-function */
'use strict';
import Reconciler from 'react-reconciler';
import { DefaultEventPriority } from 'react-reconciler/constants.js';
import { appendChild } from './helpers/appendChild.js';
import { applyProps } from './helpers/applyProps.js';
import { createInstance } from './helpers/createInstance.js';
import { removeChild } from './helpers/removeChild.js';
var Reconciler = require('react-reconciler');
var constants_js = require('react-reconciler/constants.js');
var appendChild = require('./helpers/appendChild.js');
var applyProps = require('./helpers/applyProps.js');
var createInstance = require('./helpers/createInstance.js');
var removeChild = require('./helpers/removeChild.js');
/** @typedef {import('./types/HostConfig.js').HostConfig} HostConfig */
/** @typedef {import('./types/Node.js').Node} Node */
/**
* @type {Reconciler.HostConfig<
* HostConfig['type'],
* HostConfig['props'],
* HostConfig['container'],
* HostConfig['instance'],
* HostConfig['textInstance'],
* HostConfig['suspenseInstance'],
* HostConfig['hydratableInstance'],
* HostConfig['publicInstance'],
* HostConfig['hostContext'],
* HostConfig['updatePayload'],
* HostConfig['childSet'],
* HostConfig['timeoutHandle'],
* HostConfig['noTimeout']
* >}
*/
"use strict";
const reconcilerConfig = {
isPrimaryRenderer: false,
noTimeout: -1,
supportsHydration: false,
supportsMutation: true,
supportsPersistence: false,
appendChild,
appendChildToContainer: appendChild,
appendInitialChild: appendChild,
cancelTimeout: clearTimeout,
createInstance,
removeChild,
removeChildFromContainer: removeChild,
scheduleTimeout: setTimeout,
afterActiveInstanceBlur() {},
beforeActiveInstanceBlur() {},
createTextInstance() {},
detachDeletedInstance() {},
getInstanceFromScope() {},
insertBefore() {},
preparePortalMount() {},
prepareScopeUpdate() {},
clearContainer()
{
return false;
},
finalizeInitialChildren()
{
return false;
},
getChildHostContext()
{
return null;
},
getCurrentEventPriority()
{
return DefaultEventPriority;
},
getInstanceFromNode()
{
return null;
},
/**
* @template T
* @param {T} instance
* @returns {T}
*/
getPublicInstance(instance)
{
return instance;
},
getRootHostContext()
{
return null;
},
prepareForCommit()
{
return {};
},
prepareUpdate()
{
return {};
},
resetAfterCommit()
{
return {};
},
shouldSetTextContent()
{
return false;
},
/**
* @param {Node} instance
* @param {*} _updatePayload Unused.
* @param {*} _type Unused.
* @param {{}} oldProps
* @param {{}} newProps
*/
commitUpdate(instance, _updatePayload, _type, oldProps, newProps)
{
// This is where we mutate Pixi.js objects in the render phase
instance.busy = true;
applyProps(instance, newProps, oldProps);
instance.busy = false;
},
isPrimaryRenderer: false,
noTimeout: -1,
supportsHydration: false,
supportsMutation: true,
supportsPersistence: false,
appendChild: appendChild.appendChild,
appendChildToContainer: appendChild.appendChild,
appendInitialChild: appendChild.appendChild,
cancelTimeout: clearTimeout,
createInstance: createInstance.createInstance,
removeChild: removeChild.removeChild,
removeChildFromContainer: removeChild.removeChild,
scheduleTimeout: setTimeout,
afterActiveInstanceBlur() {
},
beforeActiveInstanceBlur() {
},
createTextInstance() {
},
detachDeletedInstance() {
},
getInstanceFromScope() {
},
insertBefore() {
},
preparePortalMount() {
},
prepareScopeUpdate() {
},
clearContainer() {
return false;
},
finalizeInitialChildren() {
return false;
},
getChildHostContext() {
return null;
},
getCurrentEventPriority() {
return constants_js.DefaultEventPriority;
},
getInstanceFromNode() {
return null;
},
/**
* @template T
* @param {T} instance
* @returns {T}
*/
getPublicInstance(instance) {
return instance;
},
getRootHostContext() {
return null;
},
prepareForCommit() {
return {};
},
prepareUpdate() {
return {};
},
resetAfterCommit() {
return {};
},
shouldSetTextContent() {
return false;
},
/**
* @param {Node} instance
* @param {*} _updatePayload Unused.
* @param {*} _type Unused.
* @param {{}} oldProps
* @param {{}} newProps
*/
commitUpdate(instance, _updatePayload, _type, oldProps, newProps) {
instance.busy = true;
applyProps.applyProps(instance, newProps, oldProps);
instance.busy = false;
}
};
const reconciler = Reconciler(reconcilerConfig);
export const reconciler = Reconciler(reconcilerConfig);
exports.reconciler = reconciler;
//# sourceMappingURL=reconciler.js.map

@@ -1,141 +0,63 @@

// Module imports
import { Application } from 'pixi.js';
import { createContext } from 'react';
import { ConcurrentRoot } from 'react-reconciler/constants.js';
// Local imports
import { reconciler } from './reconciler.js';
'use strict';
/**
* Internal Pixi.js state.
*/
const context = createContext(null);
var pixi_js = require('pixi.js');
var react = require('react');
var constants_js = require('react-reconciler/constants.js');
var reconciler = require('./reconciler.js');
// We store roots here since we can render to multiple canvases
const roots = new Map();
/**
* This renders an element to a canvas, creates a renderer, scene, etc.
*
* @param {import('react').ReactNode} component The component to be rendered.
* @param {HTMLElement | HTMLCanvasElement} target The target element into which the Pixi application will be rendered. Can be any element, but if a <canvas> is passed the application will be rendered to it directly.
* @param {object} [props]
* @param {object} [props.size]
* @param {number} props.size.height
* @param {number} props.size.width
*/
export function render(
component,
target,
{
size,
...props
} = {},
)
{
let canvas;
if (target instanceof HTMLCanvasElement)
{
canvas = target;
"use strict";
const context = react.createContext(null);
const roots = /* @__PURE__ */ new Map();
function render(component, target, props = {}) {
const {
children = null,
...componentProps
} = props;
let canvas;
if (target instanceof HTMLCanvasElement) {
canvas = target;
}
const store = roots.get(target);
let root = store?.root;
const state = Object.assign(store?.state ?? {}, componentProps);
if (!root) {
const applicationProps = {};
if (canvas) {
applicationProps.canvas = canvas;
}
// Get store and init/update Pixi.js state
const store = roots.get(target);
let root = store?.root;
const state = Object.assign((store?.state ?? {}), {
...props,
size,
});
// // If size isn't explicitly defined, we can assume it from the canvas
// if (!size) {
// size = {
// height: canvas.parentElement?.clientHeight || 0,
// width: canvas.parentElement?.clientWidth || 0,
// }
// }
// Get store and init/update Pixi.js state
// const store = roots.get(canvas)
// let root = store?.root
// const state = Object.assign(store?.state || {}, { ...props, size })
// Initiate root
if (!root)
{
/** @type {Partial<import('pixi.js').ApplicationOptions>} */
const applicationProps = {};
if (canvas)
{
applicationProps.canvas = canvas;
}
if (size)
{
applicationProps.height = size.height;
applicationProps.width = size.width;
}
state.app = new Application();
state.app.init(applicationProps);
if (!canvas)
{
target.innerHTML = '';
target.appendChild(state.app.canvas);
canvas = state.app.canvas;
}
if (!state.size)
{
state.size = {
height: canvas.parentElement?.clientHeight ?? 0,
width: canvas.parentElement?.clientWidth ?? 0,
};
}
root = reconciler.createContainer(
state.app.stage,
ConcurrentRoot,
null,
false,
null,
'',
console.error,
null,
);
// // Keep track of elements subscribed to the render loop with useFrame
// state.subscribed = []
// state.subscribe = ref => {
// if (state.subscribed.includes(ref)) {
// state.subscribed = state.subscribed.filter(callback => callback !== ref)
// } else {
// state.subscribed.push(ref)
// }
// }
state.app = new pixi_js.Application();
state.app.init(applicationProps);
if (!canvas) {
target.innerHTML = "";
target.appendChild(state.app.canvas);
canvas = state.app.canvas;
}
// // Handle resize
// state.gl.setSize(size.width, size.height)
// state.camera.aspect = size.width / size.height
// state.camera.updateProjectionMatrix()
// Update root
roots.set(target, { root, state });
// Update fiber and expose Pixi.js state to children
reconciler.updateContainer(
(
<context.Provider value={state}>
{component}
</context.Provider>
),
root,
null,
() => undefined
if (!state.size) {
state.size = {
height: canvas.parentElement?.clientHeight ?? 0,
width: canvas.parentElement?.clientWidth ?? 0
};
}
root = reconciler.reconciler.createContainer(
state.app.stage,
constants_js.ConcurrentRoot,
null,
false,
null,
"",
console.error,
null
);
}
roots.set(target, { root, state });
reconciler.reconciler.updateContainer(
react.createElement(context.Provider, { value: state }, component),
root,
null,
() => void 0
);
return state;
}
return state;
}
exports.render = render;
//# sourceMappingURL=render.js.map
{
"name": "@pixi/react",
"version": "8.0.0-dev.0aaf87a",
"version": "8.0.0-dev.8ae89f5",
"description": "Write PixiJS applications using React declarative style.",

@@ -32,9 +32,10 @@ "keywords": [

"module": "lib/index.mjs",
"types": "lib/index.d.ts",
"types": "types/index.d.ts",
"files": [
"lib/",
"lib",
"dist",
"types/"
],
"scripts": {
"build": "tsc",
"build": "rimraf dist lib && npm run lint:fix && rollup -c && tsc",
"clean": "xs clean",

@@ -62,7 +63,9 @@ "docs": "xs docs",

"dependencies": {
"react-reconciler": "^0.29.2",
"statery": "^0.7.1"
"react-reconciler": "^0.29.2"
},
"devDependencies": {
"@pixi/extension-scripts": "^2.4.1",
"@rollup/plugin-commonjs": "^25.0.8",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/eslint": "^8.56.10",
"@types/react": "^18.3.2",

@@ -74,2 +77,5 @@ "@types/react-reconciler": "^0.28.8",

"react-dom": "^18.3.1",
"rollup": "^4.18.0",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"typescript": "^5.4.5",

@@ -76,0 +82,0 @@ "vitest": "^1.6.0"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc