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

Add root(options, binds) function to API #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
### MathBox Changelog
## MathBox Changelog

### [unreleased]

- Add a `root` function to the API with the same 2-argument interface as all
other primitives. The map provided to the first argument is passed to
`mathbox.set`, and the second argument is passed to `mathbox.bind`. `root`
returns the root node.

### 2.2.1

- Add Typescript support for live properties and `bind`. [#43](https://github.com/unconed/mathbox/pull/43)

### 2.2.0
Expand Down
6 changes: 3 additions & 3 deletions docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ view
width: 3,
})
.grid({
width: 2,
width: 2,
divideX: 20,
divideY: 10,
divideY: 10,
});
```

Expand All @@ -110,7 +110,7 @@ mathbox.select('axis').set('color', 'black');
As the on-screen size of elements depends on the position of the camera, we can calibrate our units by setting the `focus` on the `<root>` to match the camera distance:

```javascript
mathbox.set('focus', 3);
mathbox.root({focus: 3});
```

Which gives us:
Expand Down
95 changes: 95 additions & 0 deletions examples/test/root.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>MathBox - Empty</title>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"
></script>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"
></script>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/TrackballControls.js"
></script>

<!--
- a minified version mathbox.min.js is also available;
- recommend using a specific version (not @latest) in public sites
-->
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/mathbox@latest/build/bundle/mathbox.js"
></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/mathbox@latest/build/mathbox.css"
/>
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
</head>
<body>
<script>
var mathbox = MathBox.mathBox({
plugins: ["core", "controls", "cursor", "mathbox"],
controls: {
klass: THREE.OrbitControls,
},
});
if (mathbox.fallback) throw "WebGL not supported";

var three = mathbox.three;
three.renderer.setClearColor(new THREE.Color(0xffffff), 1.0);
var camera = mathbox.camera({
proxy: true,
position: [0, 0, 3],
});
var view = mathbox.cartesian({
range: [
[-2, 2],
[-1, 1],
],
scale: [2, 1],
});
view
.axis({
axis: 1,
width: 3,
})
.axis({
axis: 2,
width: 3,
})
.grid({
width: 2,
divideX: 20,
divideY: 10,
});

// Set a static property and a dynamic property.
mathbox.root(
{ id: "mathbox-root" },
{
focus: (time, delta) => 5 * Math.sin(time * 0.5),
}
);

// Add some data
var data = view.interval({
expr: function (emit, x, i, t) {
emit(x, Math.sin(x + t));
},
width: 64,
channels: 2,
});

// Draw a curve
var curve = view.line({
width: 5,
color: "#3090FF",
});
</script>
</body>
</html>
7 changes: 6 additions & 1 deletion src/stage/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class API {
this[type] = (options, binds) => this.add(type, options, binds);
}
}
this["root"] = (options, binds) => {
root.set(options);
root.bind(binds);
return this._reset();
};
}

select(selector) {
Expand Down Expand Up @@ -166,7 +171,7 @@ export class API {
}
_reset() {
let left;
return (left = this._up != null ? this._up.reset() : undefined) != null
return (left = this._up != null ? this._up._reset() : undefined) != null
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops!

? left
: this;
}
Expand Down
4 changes: 2 additions & 2 deletions test/bind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("live properties", () => {

const size1 = p.get("size")
const opacity1 = p.get("opacity")
expect(Math.abs(size1 - 20.5)).toBeLessThan(0.2)
expect(Math.abs(opacity1 - 0.5)).toBeLessThan(0.2)
expect(Math.abs(size1 - 20.5)).toBeLessThan(0.3)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was flaky on me, so I'm relaxing the bounds again...

expect(Math.abs(opacity1 - 0.5)).toBeLessThan(0.3)
})
});
22 changes: 22 additions & 0 deletions test/primitives/types/base/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as MB from "../../../../src";

describe("primitives.types.base.root", () => {
it("returns the root from any selection", async () => {
const mathbox = MB.mathBox();
const root = mathbox.cartesian().root();

expect(root).toBe(mathbox);
});

it("reacts to changes in properties", async () => {
const mathbox = MB.mathBox();
const root = mathbox.root();

// default focus
expect(mathbox.get("focus")).toBe(1);

// set via root and see the change reflected in mathbox.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had another test that exposed #59, so here I'm only testing the non-binds.

mathbox.root({ focus: 10 });
expect(mathbox.get("focus")).toBe(10);
});
});