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

Upgrade mathbox to new npm package, threejs => r137 #330

Merged
merged 9 commits into from
Feb 21, 2022
Merged

Upgrade mathbox to new npm package, threejs => r137 #330

merged 9 commits into from
Feb 21, 2022

Conversation

sritchie
Copy link
Contributor

@sritchie sritchie commented Feb 17, 2022

@ChristopherChudzicki, we're in business! This PR gets us onto the latest Mathbox, which can handle any version of three.js.

The script instantiation of everything is now gone. I didn't do any measuring of the production build size, but I bet it's smaller since mathbox is pulling in only what it needs from threejs now vs relying on the big global import.

The PR has comments inline describing in more detail what I had to change.

here are the new packages:

@sritchie
Copy link
Contributor Author

Scratch that, my typo. It works great now, except for mouse interaction! Tackling that will be next, probably three.js upgrade / OrbitControls related.

Copy link
Contributor Author

@sritchie sritchie left a comment

Choose a reason for hiding this comment

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

Some notes on the change.


<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, my linter tidied this up. I'll back all of the linter changes out.

@@ -17,7 +17,6 @@
<script src="/mathquill.min.js"></script>
<link rel="stylesheet" href="/mathquill.css">
<!-- MathBox -->
<script src="/mathbox-bundle.min.js"></script>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woohoo!

* Some Comments:
* - sincce there isn't a (relaiable, documented) mathbox npm package,
* we load it as a script.
* - Initializing mathbox inside a React Component led to some issues
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't make this change, so I'm not sure what this is about. Maybe there is a better way to initialize mathbox inside the react component that we can try.

Choose a reason for hiding this comment

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

I'm not sure either. I did not run into this issue when playing around with mathbox 2.1.0 today.

@@ -93,11 +94,11 @@ export default class ScrollWithOverflow extends React.PureComponent<Props, State
coverRef: { current: null | HTMLDivElement }

eventNames = [
'mousedown', 'mousemove', 'mouseup', 'wheel',
'touchstart', 'touchmove', 'touchend'
'pointerdown', 'pointermove', 'pointerup',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change occurred in Three.js r120: https://github.com/mrdoob/three.js/wiki/Migration-Guide#r119--r120

Here's an example issue suggesting the fix that worked: mrdoob/three.js#20294 (comment)

@@ -1,6 +1,6 @@
// @flow

const THREE = window.THREE
import { Color } from "three/src/math/Color.js";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been enjoying these targeted imports.

@@ -27,6 +33,23 @@ type Props = {
renderErrors: ErrorState,
setError: SetError
}

export const mathboxElement = document.getElementById("mathbox");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, the next two consts are the meat. Do we want these declared somewhere else?

this.canvas.addEventListener('touchstart', this.downHandler)
this.canvas.addEventListener('mouseup', this.mouseUpHandler)
this.canvas.addEventListener('touchend', this.touchExitHandler)
this.canvas.addEventListener('pointerdown', this.downHandler)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

my understanding (could be wrong!) is that pointer replaces mouse and touch.

this.isPointerDown = false
this.enterSlowMode()
}

touchExitHandler = async (event: TouchEvent) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

which makes this unnecessary. What am I missing?


import { mathbox } from "containers/MathBoxScene/components/MathBoxScene.js";

export default function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might suggest that this take mathbox as an argument?

@@ -1,6 +1,7 @@
import React, { PureComponent } from 'react'
import MathBoxContainer from 'containers/MathBoxContainer'
import MathBoxScene from 'containers/MathBoxScene'
import { mathboxElement } from "containers/MathBoxScene/components/MathBoxScene";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

easy as pie!

@sritchie sritchie changed the title Upgrade mathbox to new npm package, remove script initialization Upgrade mathbox to new npm package, threejs => r137 Feb 18, 2022
@sritchie
Copy link
Contributor Author

@ChristopherChudzicki commenting here since I edited the description above; this all works great and is ready for review!


export default function () {
const position: Array<number> = mathbox.three.camera.position.toArray();
const lookAt: Array<number> = mathbox.three.controls.target.toArray();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this change from center to target happened in OrbitControls a few versions ago.

@ChristopherChudzicki
Copy link
Owner

@sritchie Awesome! Thanks for making this PR. I'm happy to take a look at the mouse interaction stuff. I did some very hacky things to enable this overflow:

Screen Shot 2022-02-19 at 2 30 21 PM

I saw the issue you wrote in math3d-next... Will send an email shortly.

@sritchie
Copy link
Contributor Author

sritchie commented Feb 19, 2022 via email

@ChristopherChudzicki ChristopherChudzicki merged commit 42e3eb6 into ChristopherChudzicki:master Feb 21, 2022
@sritchie sritchie deleted the sritchie/new_mathbox branch February 21, 2022 01:47
@ChristopherChudzicki
Copy link
Owner

ChristopherChudzicki commented Feb 21, 2022

@sritchie Re the failing tests: There were two separate issues:

  1. Jest tests were failing because by default jest does not transpile node_modules, and the import/export syntax from mathbox, threestrap, shadergraph was causing syntax errors. I'm not really a webpack expert, but it might be worth targeting CommonJS for the node builds of those packages.

    I fixed this with https://github.com/ChristopherChudzicki/math3d-react/pull/333/files#diff-1846122c2c83a486a3693f7966aa522c34cf489f674185c4da0d9221683fd81fR46

  2. After fixing (1), App.test.js was failing because it couldn't initialize Mathbox (probably some lack of canvas support in JSDom, I'm not really sure). Previously Mathbox was initialized in the index.html file, so Jest never had to initialize it. But app.test.js was fairly useless, so I just deleted it.

I merged then fixed (1) on that separate PR.

Thanks again for this... it's an enormous improvement. As I've said... very exciting.

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

Successfully merging this pull request may close these issues.

2 participants