Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to make SP work like a regular JS library #59

Open
PWhiddy opened this issue Oct 19, 2021 · 1 comment
Open

Proposal to make SP work like a regular JS library #59

PWhiddy opened this issue Oct 19, 2021 · 1 comment

Comments

@PWhiddy
Copy link
Member

PWhiddy commented Oct 19, 2021

Remove need to use eval in base library, and use and special magic to have functions in scope.
No scope collisions or magic, external data can easily be brought in a predictable way. Multiple shapes could coexist without their namespaces/scopes interfering

API proposal:

//////////// SP library ////////////

// Returns library functions in a dictionary rather than using eval to dump them into global scope
function createShape() {  
  return {
    baseShape: {
      bindToCanvas: (canvas) => console.log('attaching to canvas...'), 
      getGLSL: () => console.log('vec3 p ... '), 
      draw: () => console.log('rendering ...')
    },
    sphere: (rad) => console.log(`drawing sphere radius ${rad}`),
    box: () => console.log('drawing box'),
    mix: () => console.log('setting mode mix')
  }
}




///////////// USER CODE ////////////////

import { createShape } from shaderpark
import externalObject from './otherfile.js'


// Equivalent to importing functions that are bound to a particular object,
// could have multiple shapes in the same file which don't interfere with each other
const {baseShape, sphere, mix} = createShape()

// Can use any javascript variable like you'd expect!
sphere(externalObject.raduis) 

baseShape.bindToCanvas('dummmy canvas')
baseShape.draw()
// object-oriented approach binds all functions to their data so you can do things like just ask a shape for its glsl)
const glslSource = baseShape.getGLSL()
@PWhiddy
Copy link
Member Author

PWhiddy commented Oct 19, 2021

@torinmb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant