Use.GPU is run via the Live run-time, similar to React.
There are two ways to use it:
Note: If you just want to edit the existing demos, see @use-gpu/app
.
Use.GPU embedded in a basic HTML page.
Examples
/examples/ts-webpack
- Empty canvas with webpack
and babel
.Steps
@use-gpu/workbench
, @use-gpu/webgpu
, @use-gpu/wgsl-loader
).wgsl
using @use-gpu/wgsl-loader
.wasm
modules onReact
shim to make <JSX>
work correctlyrender()
on your <App />
e.g.
import React, { render } from '@use-gpu/live';
window.onload = async () => {
const { App } = await import('./app');
render(<App />);
The await import(…)
on the outside allows synchronous import * from '.wasm'
to work on the inside.
Use.GPU has convenient React ↔︎ Live portals in both directions. See @use-gpu/react
for more info.
Steps
@use-gpu/react
, @use-gpu/workbench
, @use-gpu/webgpu
, @use-gpu/wgsl-loader
)..wgsl
to JS using @use-gpu/wgsl-loader
.wasm
imports<LiveCanvas>
to insert a <canvas>
. Will make a portal to Live.<AutoCanvas canvas={canvas} />
).On the React side:
import React, { FC } from 'react';
import { LiveCanvas } from '@use-gpu/react';
import { Component } from './component';
// This is a React component
const MyCanvas: FC = () => {
return (
<LiveCanvas>
// These are Live children
{(canvas) => <Component canvas={canvas} />}
</LiveCanvas>
);
}
On the Live side:
import type { LC, PropsWithChildren } from '@use-gpu/live';
type ComponentProps = {
canvas: HTMLCanvasElement,
};
// This is a Live component
export const Component: LC<ComponentProps> = (props: PropsWithChildren<ComponentProps>) => {
const {canvas} = props;
// ...
return null;
};
To use @use-gpu/workbench
, your build process must convert the .wgsl
files inside @use-gpu/wgsl
to JS. This can be done using the included @use-gpu/wgsl-loader
, if your build system is compatible (webpack, node or rollup).
e.g. webpack config:
{
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: [/node_modules/],
loader: 'babel-loader',
},
{
test: /\.wgsl$/i,
use: ['@use-gpu/wgsl-loader'],
},
],
},
}
Live has support for hot module reload with webpack-dev-server
.
In your webpack.config.js
:
entry
point:{
entry: [
'./src/index.tsx',
'webpack-dev-server/client/index.js?hot=true&live-reload=true',
],
}
{
devServer: {
// ...
hot: true,
},
}
App
component in hot(..., module)
:import React, { hot } from '@use-gpu/live';
export const App = hot(() => {
// ...
}, module);