From 2d4401a0591cf8032f2d17d0bf982ce00a7ce8c4 Mon Sep 17 00:00:00 2001 From: brynnchernosky <56202540+brynnchernosky@users.noreply.github.com> Date: Mon, 1 May 2023 16:55:19 -0400 Subject: refactor weight --- .../nodes/PhysicsBox/PhysicsSimulationWeight.tsx | 261 ++++++++++----------- 1 file changed, 129 insertions(+), 132 deletions(-) (limited to 'src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx') diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx index 4b3daa0d8..2ae34374d 100644 --- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx +++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx @@ -1,7 +1,7 @@ -import { Doc } from '../../../fields/Doc'; +import { Doc } from '../../../../fields/Doc'; import React = require('react'); import { useEffect, useState } from "react"; -import { IWallProps } from "./Wall"; +import { IWallProps } from "./PhysicsSimulationWall"; import "./Weight.scss"; export interface IForce { @@ -890,100 +890,97 @@ export default class Weight extends React.Component { }; // Render weight, spring, rod(s), vectors - return ( + render () {
{ - if (draggable) { + if (this.draggable) { e.preventDefault(); - setPaused(true); - setDragging(true); - setClickPositionX(e.clientX); - setClickPositionY(e.clientY); - } else if (this.props.dataDoc['mode'] == "Review") { - setSketching(true); - } + this.props.dataDoc['paused'] = true; + this.setState({dragging: true}); + this.setState({clickPositionX: e.clientX}); + this.setState({clickPositionY: e.clientY}); + } }} onPointerMove={(e) => { e.preventDefault(); - if (dragging) { - let newY = yPosition + e.clientY - clickPositionY; - if (newY > yMax - 2 * radius - 10) { - newY = yMax - 2 * radius - 10; + if (this.state.dragging) { + let newY = this.state.yPosition + e.clientY - this.state.clickPositionY; + if (newY > this.props.yMax - 2 * this.props.radius - 10) { + newY = this.props.yMax - 2 * this.props.radius - 10; } else if (newY < 10) { newY = 10; } - let newX = xPosition + e.clientX - clickPositionX; - if (newX > xMax - 2 * radius - 10) { - newX = xMax - 2 * radius - 10; + let newX = this.state.xPosition + e.clientX - this.state.clickPositionX; + if (newX > this.props.xMax - 2 * this.props.radius - 10) { + newX = this.props.xMax - 2 * this.props.radius - 10; } else if (newX < 10) { newX = 10; } if (this.props.dataDoc['simulationType'] == "Suspension") { - if (newX < (xMax + xMin) / 4 - radius - 15) { - newX = (xMax + xMin) / 4 - radius - 15; - } else if (newX > (3 * (xMax + xMin)) / 4 - radius / 2 - 15) { - newX = (3 * (xMax + xMin)) / 4 - radius / 2 - 15; + if (newX < (this.props.xMax + this.props.xMin) / 4 - this.props.radius - 15) { + newX = (this.props.xMax + this.props.xMin) / 4 - this.props.radius - 15; + } else if (newX > (3 * (this.props.xMax + this.props.xMin)) / 4 - this.props.radius / 2 - 15) { + newX = (3 * (this.props.xMax + this.props.xMin)) / 4 - this.props.radius / 2 - 15; } } - setYPosition(newY); - setDisplayYPosition( - Math.round((yMax - 2 * radius - newY + 5) * 100) / 100 - ); + this.setState({yPosition: newY}); + this.props.dataDoc['positionYDisplay'] = + Math.round((this.props.yMax - 2 * this.props.radius - newY + 5) * 100) / 100 if (this.props.dataDoc['simulationType'] != "Pulley") { - setXPosition(newX); - setDisplayXPosition(newX); + this.setState({xPosition: newX}); + this.props.dataDoc['positionXDisplay'] = newX } if (this.props.dataDoc['simulationType'] != "Suspension") { if (this.props.dataDoc['simulationType'] != "Pulley") { - setUpdatedStartPosX(newX); + this.setState({updatedStartPosX: newX}); } - setUpdatedStartPosY(newY); + this.setState({updatedStartPosY: newY}); } - setClickPositionX(e.clientX); - setClickPositionY(e.clientY); - setDisplayValues(); + this.setState({clickPositionX: e.clientX}); + this.setState({clickPositionY: e.clientY}); + this.setDisplayValues(); } }} onPointerUp={(e) => { - if (dragging) { + if (this.state.dragging) { e.preventDefault(); if ( this.props.dataDoc['simulationType'] != "Pendulum" && this.props.dataDoc['simulationType'] != "Suspension" ) { - resetEverything(); + this.resetEverything(); } - setDragging(false); - let newY = yPosition + e.clientY - clickPositionY; - if (newY > yMax - 2 * radius - 10) { - newY = yMax - 2 * radius - 10; + this.setState({dragging: false}); + let newY = this.state.yPosition + e.clientY - this.state.clickPositionY; + if (newY > this.props.yMax - 2 * this.props.radius - 10) { + newY = this.props.yMax - 2 * this.props.radius - 10; } else if (newY < 10) { newY = 10; } - let newX = xPosition + e.clientX - clickPositionX; - if (newX > xMax - 2 * radius - 10) { - newX = xMax - 2 * radius - 10; + let newX = this.state.xPosition + e.clientX - this.state.clickPositionX; + if (newX > this.props.xMax - 2 * this.props.radius - 10) { + newX = this.props.xMax - 2 * this.props.radius - 10; } else if (newX < 10) { newX = 10; } if (this.props.dataDoc['simulationType'] == "Spring") { - setSpringStartLength(newY); + this.props.dataDoc.springStartLength = newY } if (this.props.dataDoc['simulationType'] == "Suspension") { - let x1rod = (xMax + xMin) / 2 - radius - yMin - 200; - let x2rod = (xMax + xMin) / 2 + yMin + 200 + radius; - let deltaX1 = xPosition + radius - x1rod; - let deltaX2 = x2rod - (xPosition + radius); - let deltaY = yPosition + radius; + let x1rod = (this.props.xMax + this.props.xMin) / 2 - this.props.radius - this.props.yMin - 200; + let x2rod = (this.props.xMax + this.props.xMin) / 2 + this.props.yMin + 200 + this.props.radius; + let deltaX1 = this.state.xPosition + this.props.radius - x1rod; + let deltaX2 = x2rod - (this.state.xPosition + this.props.radius); + let deltaY = this.state.yPosition + this.props.radius; let dir1T = Math.PI - Math.atan(deltaY / deltaX1); let dir2T = Math.atan(deltaY / deltaX2); let tensionMag2 = - (mass * Math.abs(gravity)) / + (this.props.mass * Math.abs(this.props.gravity)) / ((-Math.cos(dir2T) / Math.cos(dir1T)) * Math.sin(dir1T) + Math.sin(dir2T)); let tensionMag1 = @@ -1004,7 +1001,7 @@ export default class Weight extends React.Component { }; const grav: IForce = { description: "Gravity", - magnitude: mass * Math.abs(gravity), + magnitude: this.props.mass * Math.abs(this.props.gravity), directionInDegrees: 270, component: false, }; @@ -1013,8 +1010,8 @@ export default class Weight extends React.Component { } }} > -
-

{mass} kg

+
+

{this.props.mass} kg

{this.props.dataDoc['simulationType'] == "Spring" && ( @@ -1028,7 +1025,7 @@ export default class Weight extends React.Component { zIndex: -2, }} > - + {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((val) => { const count = 10; let xPos1; @@ -1036,14 +1033,14 @@ export default class Weight extends React.Component { let xPos2; let yPos2; if (val % 2 == 0) { - xPos1 = xPosition + radius - 20; - xPos2 = xPosition + radius + 20; + xPos1 = this.state.xPosition + this.props.radius - 20; + xPos2 = this.state.xPosition + this.props.radius + 20; } else { - xPos1 = xPosition + radius + 20; - xPos2 = xPosition + radius - 20; + xPos1 = this.state.xPosition + this.props.radius + 20; + xPos2 = this.state.xPosition + this.props.radius - 20; } - yPos1 = (val * yPosition) / count; - yPos2 = ((val + 1) * yPosition) / count; + yPos1 = (val * this.state.yPosition) / count; + yPos2 = ((val + 1) * this.state.yPosition) / count; return ( { zIndex: -1, }} > - + @@ -1091,12 +1088,12 @@ export default class Weight extends React.Component { zIndex: -2, }} > - + @@ -1114,12 +1111,12 @@ export default class Weight extends React.Component { zIndex: -2, }} > - + @@ -1128,17 +1125,17 @@ export default class Weight extends React.Component { style={{ position: "absolute", zIndex: 10, - left: (xMax + xMin) / 2 - radius - yMin - 200 + 80 + "px", + left: (this.props.xMax + this.props.xMin) / 2 - this.props.radius - this.props.yMin - 200 + 80 + "px", top: 10 + "px", - backgroundColor: labelBackgroundColor, + backgroundColor: this.labelBackgroundColor, }} > {Math.round( ((Math.atan( - (yPosition + radius) / - (xPosition + - radius - - ((xMax + xMin) / 2 - radius - yMin - 200)) + (this.state.yPosition + this.props.radius) / + (this.state.xPosition + + this.props.radius - + ((this.props.xMax + this.props.xMin) / 2 - this.props.radius - this.props.yMin - 200)) ) * 180) / Math.PI) * @@ -1159,12 +1156,12 @@ export default class Weight extends React.Component { zIndex: -2, }} > - + @@ -1174,19 +1171,19 @@ export default class Weight extends React.Component { style={{ position: "absolute", zIndex: 10, - left: (xMax + xMin) / 2 + yMin + 200 + radius - 80 + "px", + left: (this.props.xMax + this.props.xMin) / 2 + this.props.yMin + 200 + this.props.radius - 80 + "px", top: 10 + "px", - backgroundColor: labelBackgroundColor, + backgroundColor: this.labelBackgroundColor, }} > {Math.round( ((Math.atan( - (yPosition + radius) / - ((xMax + xMin) / 2 + - yMin + + (this.state.yPosition + this.props.radius) / + ((this.props.xMax + this.props.xMin) / 2 + + this.props.yMin + 200 + - radius - - (xPosition + radius)) + this.props.radius - + (this.state.xPosition + this.props.radius)) ) * 180) / Math.PI) * @@ -1207,12 +1204,12 @@ export default class Weight extends React.Component { zIndex: -2, }} > - + @@ -1230,39 +1227,39 @@ export default class Weight extends React.Component { zIndex: -2, }} > - + - {!dragging && ( + {!this.state.dragging && (

- {Math.round(pendulumLength)} m + {Math.round(this.props.pendulumLength)} m

- {Math.round(pendulumAngle * 100) / 100}° + {Math.round(this.props.pendulumAngle * 100) / 100}°

)} @@ -1273,8 +1270,8 @@ export default class Weight extends React.Component {
- - + +
@@ -1282,18 +1279,18 @@ export default class Weight extends React.Component { style={{ position: "absolute", zIndex: 500, - left: Math.round(xMax * 0.5 - 200 + wedgeWidth - 80) + "px", + left: Math.round(this.props.xMax * 0.5 - 200 + this.props.wedgeWidth - 80) + "px", top: Math.round(window.innerHeight * 0.8 - 40) + "px", }} > {Math.round( - ((Math.atan(wedgeHeight / wedgeWidth) * 180) / Math.PI) * 100 + ((Math.atan(this.props.wedgeHeight / this.props.wedgeWidth) * 180) / Math.PI) * 100 ) / 100} °

)} - {!dragging && showAcceleration && ( + {!this.state.dragging && this.props.showAcceleration && (
{ top: 0, }} > - + { { style={{ pointerEvents: "none", position: "absolute", - left: xPosition + radius + xAccel * 3 + 25 + "px", - top: yPosition + radius + yAccel * 3 + 70 + "px", + left: this.state.xPosition + this.props.radius + this.state.xAccel * 3 + 25 + "px", + top: this.state.yPosition + this.props.radius + this.state.yAccel * 3 + 70 + "px", zIndex: -1, lineHeight: 0.5, }} >

{Math.round( - 100 * Math.sqrt(xAccel * xAccel + yAccel * yAccel) + 100 * Math.sqrt(this.state.xAccel * this.state.xAccel + this.state.yAccel * this.state.yAccel) ) / 100}{" "} m/s 2 @@ -1349,7 +1346,7 @@ export default class Weight extends React.Component {

)} - {!dragging && showVelocity && ( + {!this.state.dragging && this.props.showVelocity && (
{ top: 0, }} > - + { { style={{ pointerEvents: "none", position: "absolute", - left: xPosition + radius + xVelocity * 3 + 25 + "px", - top: yPosition + radius + yVelocity * 3 + "px", + left: this.state.xPosition + this.props.radius + this.state.xVelocity * 3 + 25 + "px", + top: this.state.yPosition + this.props.radius + this.state.yVelocity * 3 + "px", zIndex: -1, lineHeight: 0.5, }} @@ -1398,8 +1395,8 @@ export default class Weight extends React.Component { {Math.round( 100 * Math.sqrt( - displayXVelocity * displayXVelocity + - displayYVelocity * displayYVelocity + this.props.displayXVelocity * this.props.displayXVelocity + + this.props.displayYVelocity * this.props.displayYVelocity ) ) / 100}{" "} m/s @@ -1630,5 +1627,5 @@ export default class Weight extends React.Component { ); })}
- ); + } }; -- cgit v1.2.3-70-g09d2