@@ -92,21 +92,27 @@ export class Math3D extends xb.Script {
9292 light . position . set ( - 0.5 , 4 , 1.0 ) ;
9393 this . add ( light ) ;
9494
95- this . panel . position . set ( 0 , 1.9 , - 1.0 ) ;
95+ this . panel . position . set ( 0 , 2.0 , - 1.0 ) ;
9696
9797 this . keyboard = new Keyboard ( ) ;
9898 this . add ( this . keyboard ) ;
9999 this . keyboard . position . set ( 0 , - 0.3 , 0 ) ;
100100
101+ const startFn = this . mathObjects [ 0 ] . functionText ;
102+
103+ this . keyboard . setText ?. ( startFn ) || ( this . keyboard . keyText = startFn ) ;
104+
101105 this . keyboard . onEnterPressed = ( newFunctionText ) => {
102106 this . mathObjects [ this . descriptionPagerState . currentPage ] . functionText =
103107 newFunctionText ;
104108 this . updateGraph ( newFunctionText ) ;
105109 } ;
106110
107111 this . keyboard . onTextChanged = ( currentText ) => {
108- if ( this . functionDisplay ) {
109- this . functionDisplay . text = currentText ;
112+ const index = this . descriptionPagerState . currentPage ;
113+ const currentDisplay = this . descriptionPager . children [ index ] . children [ 0 ] ;
114+ if ( currentDisplay && currentDisplay . setText ) {
115+ currentDisplay . setText ( currentText ) ;
110116 }
111117 } ;
112118
@@ -174,11 +180,24 @@ export class Math3D extends xb.Script {
174180 var yMin = - 5 ,
175181 yMax = 5 ,
176182 yRange = yMax - yMin ;
183+
184+ const Z_LIMIT = 25 ; // Maximum absolute value for z to prevent extreme spikes in the graph
177185 var zFunction = Parser . parse ( zFunctionText ) . toJSFunction ( [ 'x' , 'y' ] ) ;
178- var parametricFunction = function ( x , y , target ) {
179- var x = xRange * x + xMin ;
180- var y = yRange * y + yMin ;
181- var z = zFunction ( x , y ) ;
186+ var parametricFunction = ( u , v , target ) => {
187+ const x = xRange * u + xMin ;
188+ const y = yRange * v + yMin ;
189+
190+ let z ;
191+ try {
192+ z = zFunction ( x , y ) ;
193+ // Clamp the value to stay between -Z_LIMIT and Z_LIMIT
194+ z = xb . clamp ( z , - Z_LIMIT , Z_LIMIT ) ;
195+
196+ // Handle cases where the math results in NaN (not a number)
197+ if ( isNaN ( z ) ) z = 0 ;
198+ } catch ( e ) {
199+ z = 0 ;
200+ }
182201
183202 target . set ( x , y , z ) ;
184203 } ;
0 commit comments