Skip to content

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

Open
@PWhiddy

Description

@PWhiddy

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions