glre
TypeScript icon, indicating that this package has built-in type declarations

0.21.0 • Public • Published

🌇 glre

 npm version  downloads  license MIT  docs available  bundle size

glre is a simple glsl Reactive Engine on the web and native via TypeScript, React, Solid and more.

test1 test2 test3 raymarch1 raymarch2 raymarch3 raymarch4 raymarch5 raymarch6

Installation

npm install glre

Documentation

Docs : glre Introduction
API : glre API and feature
Guide : Creating a scene

Ecosystem

⛪️ reev: reactive event state manager
🔮 refr: request animation frame

Staying informed

github discussions welcome✨
@tseijp twitter
tsei.jp articles

PRs

welcome✨

What does it look like?

glre simplifies glsl programming via TypeScript, React, Solid and more (live demo). 4
import { createRoot } from 'react-dom/client'
import { useGL, useFrame } from 'glre/react'

const fragment = `
precision highp float;
uniform vec2 iResolution;
void main() {
  gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
}
`

const App = () => {
        const gl = useGL({ fragment })
        useFrame(() => {
                gl.clear()
                gl.viewport()
                gl.drawArrays()
        })
        return <canvas ref={gl.ref} />
}

createRoot(document.getElementById('root')).render(<App />)

react-native supported (codesandbox demo)

import { GLView } from 'expo-gl'
import { useGL, useFrame } from 'glre/native'
import { registerRootComponent } from 'expo'

const fragment = `
precision highp float;
uniform vec2 iResolution;
void main() {
  gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
}
`

const App = () => {
        const self = useGL({ fragment })
        useFrame(() => {
                self.clear()
                self.viewport()
                self.drawArrays()
                self.gl.flush()
                self.gl.endFrameEXP()
        })
        return <GLView style={{ flex: 1 }} onContextCreate={self.ref} />
}

registerRootComponent(App)

solid js supported (codesandbox demo)

import { render } from 'solid-js/web'
import { onGL, onFrame } from 'glre/solid'

const fragment = `
precision highp float;
uniform vec2 iResolution;
void main() {
  gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
}
`

const App = () => {
        const gl = onGL({ fragment })
        onFrame(() => {
                gl.clear()
                gl.viewport()
                gl.drawArrays()
        })
        return <canvas ref={gl.ref} />
}

render(() => <App />, document.getElementById('root'))

pure js supported (codesandbox demo)

<canvas id="id" style="top: 0; left: 0; position: fixed" />
<script type="module">
        import self from 'https://cdn.skypack.dev/glre@latest'
        const fragment = `
          precision highp float;
          uniform vec2 iResolution;
          void main() {
            gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
          }
        `
        function setup() {
                const el = document.getElementById('id')
                const gl = el.getContext('webgl2')
                self({ el, gl, fragment })
                self.init()
                self.resize()
                draw()
        }
        function draw() {
                requestAnimationFrame(draw)
                self.render()
                self.clear()
                self.viewport()
                self.drawArrays()
        }
        document.addEventListener('DOMContentLoaded', setup)
</script>

Package Sidebar

Install

npm i glre

Homepage

glre.tsei.jp

Weekly Downloads

3

Version

0.21.0

License

MIT

Unpacked Size

256 kB

Total Files

23

Last publish

Collaborators

  • tsei