Giter Club home page Giter Club logo

mathbox's Introduction

MathBox

NPM Package Build Status License

Presentation-quality WebGL math graphing

MathBox

MathBox is a library for rendering presentation-quality math diagrams in a browser using WebGL. Built on top of Three.js and ShaderGraph it provides a clean API to visualize mathematical relationships and animate them declaratively.

For background, see the article series on Acko.net.

Presentations:

Demos:

And many more at https://mathbox.org.

Installation

You can install MathBox via npm for use with a bundler like Webpack, or include a global MathBox object onto your page by including the library via CDN.

NPM Package

  • Run the following in your project's directory to install MathBox and Three.js via npm:
npm install mathbox three

Import THREE and MathBox (library and stylesheet), along with a controls instance that you'll pass to the MathBox.mathBox constructor:

import "mathbox/mathbox.css"

import * as THREE from "three"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"
import * as MathBox from "mathbox"

Install via CDN

Include the following in your HTML header to load all required libraries and styles:

<!-- Install your choice of three.js version from CDN: -->
<script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"
></script>

<!-- Load a Controls instance, making sure that the version matches the Three.js version above: -->
<script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"
></script>

<!-- Install the latest MathBox, either mathbox.js or mathbos.min.js -->
<script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/mathbox@latest/build/bundle/mathbox.js"
></script>

<!-- Include the MathBox CSS: -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/mathbox@latest/build/mathbox.css"
/>

Basic Usage

Construct a MathBox instance by passing initialization options to the mathBox() constructor:

const options = {
  controls: {
    // Orbit controls, i.e. Euler angles, with gimbal lock
    klass: THREE.OrbitControls
  },
};
const root = MathBox.mathBox(options);

Note See threestrap for all available options.

To spawn inside a specific element, pass an HTMLElement with the element option:

const element = document.querySelector("#my-thing");

const options = {
  element: element,
  controls: {
    klass: THREE.OrbitControls
  },
};
const root = MathBox.mathBox(options);

On initialization, mathBox returns a MathBox API object, wrapping the MathBox <root/>. Insert new MathBox nodes into the component tree by calling the method associated with the primitive you'd like to add.

Note See the Primitives doc for a description of all primitives and their properties.

The following code will set up a 3D Cartesian coordinate system with the specified range and scale for its x, y and z axes, and then insert an x and y axis into the scene:

const view = root
  .cartesian({
    range: [
      [-2, 2],
      [-1, 1],
      [-1, 1],
    ],
    scale: [2, 1, 1],
  })
  .axis({
    axis: 1,
  })
  .axis({
    axis: 2,
  });

Use your mouse to click and drag the camera's orientation, and zoom in and out:

2023-01-19 11 32 59

Each primitive call:

  • creates a new element
  • inserts it into the tree
  • returns a version of the API object with its selection focused on the new element.

Calling print() on some selection will print a representation to the console of the selection and any children. For example, view.print() prints the following:

<cartesian
  range={[
    [-2, 2],
    [-1, 1],
    [-1, 1],
  ]}
  scale={[2, 1, 1]}
>
  <axis axis={1} />
  <axis axis={2} />
</cartesian>

Select objects using .select() and a CSS-like selector to get a jQuery-like selection:

root.select("cartesian > axis");

Next, visit the Quick Start page for a more involved example that builds up an animating, interactive mathematical graph with labeled axes.

Docs & Help

For help, see the following resources:

For more involved questions, or just to say hi, please join us in the MathBox Google Group.

Related Projects

Who's using MathBox?

And the many demos listed on this thread of the MathBox Google group.

License

MIT License.

MathBox and ShaderGraph (c) Steven Wittens 2013-2023.

Libraries and 3rd party shaders (c) their respective authors.

mathbox's People

Contributors

christopherchudzicki avatar jamescrook avatar jroper18 avatar mgold avatar sritchie avatar unconed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mathbox's Issues

mathbox.debug throws an error

Open one of the examples and run mathbox.debug() in the console. Some useful info is printed, but the last piece is:

api.js:218 Uncaught TypeError: Cannot read properties of null (reading '1')
    at getName (api.js:218:1)
    at API.debug (api.js:222:1)
    at <anonymous>:1:9

Note: Probably all of the examples? But definitely curve.html and data.html

camelcase node ids do not work

To reproduce:

  1. In examples/test/curve.html, change id="mySampler" to id="mySampler", and change all #sampler to #mySampler.
  2. Open the file.

Expected: Should work fine

Actual Errors with

Could not find source `#mySampler`

Comments

  • Mathbox delegates to an external library for most CSS selections, but NOT for id-based selection, iirc

Add DEVELOPING.md, take a README pass

With the typescript changes, test changes etc it's time that we add a DEVELOPING.md.

@cchudzicki , I would love if we could have notes on

  • how to develop with typescript (how to add types, how to convert a file)
  • how to run the tests
  • how to generate docs

that sort of thing. We might also link in the README to your new react project and the demo project.

clean up "string continuations are not recommended" warnings

The following warnings are emitted when running Mathbox from Clojurescript:

1------ WARNING #1 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/render/meshes/base.js:73:25
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #2 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/render/meshes/base.js:75:26
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #3 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/render/meshes/base.js:83:27
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #4 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/render/meshes/base.js:85:26
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #5 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/splash.js:41:17
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #6 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/splash.js:42:50
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #7 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:68:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #8 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:72:43
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #9 -  -----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:86:12
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #10 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:87:34
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #11 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:91:12
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #12 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:96:19
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #13 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:105:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #14 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:109:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #15 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:121:11
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #16 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:123:25
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #17 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:127:11
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #18 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:129:16
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #19 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:161:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #20 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:162:49
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #21 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:177:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #22 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:178:49
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #23 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:203:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #24 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:205:21
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #25 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:238:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #26 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:240:27
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #27 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:267:9
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #28 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:269:21
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #29 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:282:11
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #30 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:283:21
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #31 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:286:11
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #32 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:287:24
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #33 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:296:34
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
------ WARNING #34 -  ----------------------------------------------------------
 Resource: node_modules/mathbox/src/util/glsl.js:298:15
 String continuations are not recommended. See https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

Formatting not working on CI

It seems that Prettier's CLI exits with code 0 in general. If we want to check for formatting errors, we need to pass --check. So on cI, we should be using that flag.

RGBFormat removed from r137

Tried using recent THREE release (r149), and got the following error:

WARNING in ./node_modules/mathbox/build/esm/render/buffer/texture/datatexture.js 57:12-27 export 'RGBFormat' (imported as 'CONST') was not found in 'three' (possible exports: ACESFilmicToneMapping, ...

Per THREE r137, RGBFormat is removed:

Texture
Remove RGBFormat. mrdoob/three.js#23228, mrdoob/three.js#23201, mrdoob/three.js#23205, mrdoob/three.js#23211, mrdoob/three.js#23223, mrdoob/three.js#23218, mrdoob/three.js#23219, mrdoob/three.js#23367 (@Mugen87)

As this is only used in 1 class, a simple fix could be to just replace it with RGBAFormat, per this recommendation.

Suggestion: scalar/complex fields

Hi there! I found out about math3d recently and I appreciated the work of both of these projects! And I'm excited to contribute. The thing I want to add looks something like this.
https://harryhatchard.com/hydrogen/index.html
this guy made this using three.js fragment shaders. Initially I thought to make everything myself, and make a PR, but I found out that this is hard when you have no experience with shaders. So here I am.
@sritchie @unconed @ChristopherChudzicki
As far as I know, mathbox allows to directly pass a shader to webGL.
At first I want to know are you interested in doing this,
and if so, can you help me with either developing shaders, implementing a proper math3d>mathbox and mathbox>three.js interface?

Surfaces are too dark

Left: v0.0.5 / Right: v2+

Screen Shot 2022-04-07 at 8 30 26 PM

@sritchie I dunno if this is a consequence of changes we've made in Mathbox, or a consequence of the ThreeJS upgrades. Do you have any idea? I think you've looked at more versions of ThreeJS than I have. Otherwise... I'll try to learn something about ThreeJS :)

The build directory should be gitignored

Current situation:

  • the build/ directory contains build mathbox (a bundle for the web, and a esm version transpiled from typescript)
  • the build directory in version control is NOT used by npm** because the prepack script rebuilds.

It would be nice to not have to manually update / deal with the build directory.

  • Option 1: remove the build directory from version control; reference some other build version (e.g., from a CDN) in the examples
  • Option 2: keep the build directory in VC, but somehow automatically build-and-commit when PRs are merged to master.

Are there other options?

Any example code to make a 3D numbered spiral?

Hello,

I am researching Mathbox now along with Mathics and am interested to know if there is any example code to make a 3D spiral with numbers along the curve.

Additionally, I want to try an overlay some other simple curves on the 3D spiral as well. Sort of using the 3D spiral as a type of cone axis to overlay other curves.

Do you have anything that might lead me in the right direction or some type of simple template that I can use to get started?

Thanks and have a good day.

docs are broken

The new types latch introduced in #32 has no entry in src/docs/primitives.js, which seems to have broken docs generation.

Bug when using mathbox + THREE.Color in a bundle

@sritchie brought this up in ChristopherChudzicki/mathbox-react#203, but fundamentally it's an issue with the way Mathbox imports stuff.

Expectation: Users consuming the mathbox library should be able to write code similar to that below, bundle it with some bundler (webpack, vite, turbopack, etc) and have it work

import * as MB from "mathbox"
import * as THREE from "three"

const mathbox = MB.mathBox(/* options... */)
mathbox
  .cartesian()
  .axis({ color: new THREE.Color(0xff4136) })

Actuality: Code like the above does not work because:

  1. This library (mathbox) imports from three/src. For example, import { Color } from "three/src/math/Color.js"; ( example)
  2. This library (mathbox) uses instanceof checks like value instanceof Color (example)
  3. Userland people like above import from three (such as the code above), which means userland people use three/build/three.module.js. Consequently, instanceof checks won't work.

See https://github.com/ChristopherChudzicki/mathbox-color-bug for a full example.

Notes

Readback example is broken

The readback example seems to be broken in the latest release.

If you hover over the points, they don't change colors as expected from older examples. Chrome Debug Tools don't show any errors either.

Long-term, would be great to convert such examples as explicit tests.

mathbox not working on Android

Hi I discovered this initially using mathbox under Emmy-viewers, i.e. a Clojurescript bridge to mathbox.

Subsequently I discovered all mathbox applications behave like this. They behave just fine in most Browsers under Linux/Mac/Windows. However, Android has problems: The initial logo/gimbal is displayed, then maybe a background, Interactive axes are displayed (and work) and also movable points. Nothing else though, no graph whatsoever.

The Browser is Chrome Version 125.06422.165 under Android 11 (tablet)
also Chrome Version 125.06422.053 under Android 12 (phone)

Here is an example from maria.cloud (Small Clojure Interpretter)
https://2.maria.cloud/gist/fa29042a2fd078e2ad8744daf2e19766

And also using Clojure's Clerk:
https://sritchie.github.io/strange-loop-2023/notebooks/stl/phase_portrait

but also mathbox's own examples:
https://mathbox.org/test/network.html
https://mathbox.org/math/hyperbolic.html

All of the above display the problems described on the Android systems I tried them on.

Add "Who is using mathbox?" docs

get(k) on mathbox root ignores bound properties

Add this to some example:

mathbox.bind({
  focus: (time, delta) => {
    console.log(mathbox.get("focus"));
    5 * Math.sin(time * 0.5);
  },
});

And note that "1", the default, is printed many times instead of

  • the function value
  • the current value of "focus" for mathbox

Internal functions occasionally make invalid css queries

Some internal mathbox functions query Mathbox's DOM using invalid CSS selector queries.

For example:

  1. Edit Mathbox source at https://gitgud.io/unconed/mathbox/-/blob/master/src/model/model.js#L322 to console.log its first argument, a css selector query.
  2. Rebuild, and load examples/test/rtt.html

You will find that one of the queries logged to console is the string

vec4 getSample(vec2 xy);
vec4 getFramesSample(vec2 xy) { return getSample(xy); }

which is clearly not a valid css selector query.

Do we care?

Probably not right now.

I suspect Mathbox has always made these invalid queries, and the css-selection library cssauron tolerated them fine: it would just return false since no elements matched these invalid selectors. These invalid queries seem not to be causing any bugs.

I discovered this while working on replacing cssauron with css-select. The new library, css-select, is a bit stricter in that it throws errors when passed invalid css selectors. This error-throwing behavior seems desirable to me. (For one thing, it's consistent with the browser-native Element.querySelector API.)

Embracing the new error-throwing behavior probably requires preventing Mathbox internal functions from making these invalid CSS queries.

add more typedefs

#25 Introduced infrastructure for typings. Remaining TODOs:

To-do:

  • add typings for mathBox options
  • finish manual typings for types.js
  • add more tests to types.spec.ts. Since that test file is written in typescript, if its tests pass, we can be confident in my manual typings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.