/* Preserving string type method 1: Create an intermidiate object, use onChange to update original params object Works fine, but .listen() ability is lost*/constobj1={numberStr1: parseFloat(params.numberStr1)};gui.add(obj1,'numberStr1',0,1).step(0.01).onChange(()=>{params.numberStr1=obj1.numberStr1.toString();}).listen();// .listen() doesn't work
/* Preserving string type method 2: Create an intermidiate object with getter/setter .listen() works, converting on every getValue .onChange()/onFinishChange() are still free, as conversion is invisible to controller*/constobj2={};Object.defineProperties(obj2,{numberStr2: {get: function(){returnparseFloat(params.numberStr2);},set: function(value){params.numberStr2=value.toString();}},});gui.add(obj2,'numberStr2',0,1).step(0.01).listen();// .listen() works
Object with a rgba string color
/* Preserving #rgba string:*/constobj3={rgbaStr: params.rgbaStr.slice(5,-1).split(',')};gui.addColor(obj3,'rgbaStr',255).onChange(value=>{params.rgbaStr=`rgba(${value.join()})`;});/* Add a NumberController for alpha*/constobj3a={alpha: parseFloat(params.rgbaStr.slice(5,-1).split(',')[3])};gui.add(obj3a,'alpha',0,1).onChange(value=>{letrgbaArray=params.rgbaStr.slice(5,-1).split(',');rgbaArray[3]=value;params.rgbaStr=`rgba(${rgbaArray.join()})`;});
Object with a #rrggbbaa string color
/* Preserving #rrggbbaa string:*/constobj4={rrggbbaaStr: params.rrggbbaaStr.slice(0,-2)};gui.addColor(obj4,'rrggbbaaStr').onChange(value=>{params.rrggbbaaStr=value+params.rrggbbaaStr.slice(-2);});/* Add a NumberController for alpha*/constobj4a={alpha: parseInt(params.rrggbbaaStr.slice(-2),16)};gui.add(obj4a,'alpha',0,255).step(1).onChange(value=>{params.rrggbbaaStr=params.rrggbbaaStr.slice(0,-2)+value.toString(16).padStart(2,'0');});