js setAttribute 与数组
js setAttribute with Array
我对 setAttribute 有疑问。我设置了一个数组作为旋转的值,但它不起作用。 gradRoll
的警报是 number
。但是gradRoll
的类型是undefined
。为什么?有谁能够帮助我?提前致谢。
AFRAME.registerComponent('clockhand_roll',{
schema:{
type:'string', default: "secondClockhand"
},
init:function(){
var data = this.data;
var el=this.el;
var oTime= new Date();
var curTime;
var clockhandPositonArray;
if(data == "secondClockhand" ){
curTime = oTime.getSeconds();
}else if(data == "minuteClockhand"){
curTime = oTime.getMinutes();
}else if(data == "hourClockhand"){
curTime = oTome.getHours;
}else {
alert("invalid input!");
}
//alert(curTime);
//clockhandPositonArray=getClockhandPosition(curTime, data);
//alert(el.getAttribute("rotation"));
//el.setAttribute("rotation", 'clockhandPositonArray');
setClockhandPosition(el, curTime, data);
}
});
function setClockhandPosition(el, curTime, typeOfClockhand){
var gradForOneUnit;
var gradRoll;
var rotationArray;
if(typeOfClockhand == "secondClockhand" || typeOfClockhand == "minuteClockhand"){
gradForOneUnit=360/60;
}else if(typeOfClockhand == "hourClockhand"){
gradForOneUnit=360/12;
}
gardRoll=curTime*gradForOneUnit;
//alert(gardRoll); //number: 354!!!!!!!!!!!!!!!!!!!! why??????
//alert(typeof(gradRoll)); //undefined!!!!!!!!!!!!! why??????
rotationArray=[curTime*gradForOneUnit, 0, 0];
//alert(typeof(rotationArray));//object
//alert(typeof(el.getAttribute("rotation"))); //object
//alert(rotationArray); //number: 162 , 0, 0
el.setAttribute("rotation", rotationArray); //dosn't work.
}
使用:
el.setAttribute("transform", "rotate3d,x,y,z,angle)");
或合并rotateX
、rotateY
、rotateZ
.
gradRoll 未定义,因为变量名称是 gardRoll,这是您在第一个警报中重复但在第二个警报中重复的拼写错误。
您似乎只想编辑旋转的 X 值,所以试试这个:
el.setAttribute("rotation", gardRoll + ' 0 ' + '0');
请注意,我使用的变量中有错别字。
我知道这有点脏,但当我遇到与位置组件类似的问题时,它对我有用。
很多人建议:
el.setAttribute("rotation", 'x'+'y' + 'z');
方法,据我所知,省略解析( 'x y z' to {x,y,z}
)并在第一名:
el.setAttribute("rotation", {x:gradRoll,y:0,z:0});
我对 setAttribute 有疑问。我设置了一个数组作为旋转的值,但它不起作用。 gradRoll
的警报是 number
。但是gradRoll
的类型是undefined
。为什么?有谁能够帮助我?提前致谢。
AFRAME.registerComponent('clockhand_roll',{
schema:{
type:'string', default: "secondClockhand"
},
init:function(){
var data = this.data;
var el=this.el;
var oTime= new Date();
var curTime;
var clockhandPositonArray;
if(data == "secondClockhand" ){
curTime = oTime.getSeconds();
}else if(data == "minuteClockhand"){
curTime = oTime.getMinutes();
}else if(data == "hourClockhand"){
curTime = oTome.getHours;
}else {
alert("invalid input!");
}
//alert(curTime);
//clockhandPositonArray=getClockhandPosition(curTime, data);
//alert(el.getAttribute("rotation"));
//el.setAttribute("rotation", 'clockhandPositonArray');
setClockhandPosition(el, curTime, data);
}
});
function setClockhandPosition(el, curTime, typeOfClockhand){
var gradForOneUnit;
var gradRoll;
var rotationArray;
if(typeOfClockhand == "secondClockhand" || typeOfClockhand == "minuteClockhand"){
gradForOneUnit=360/60;
}else if(typeOfClockhand == "hourClockhand"){
gradForOneUnit=360/12;
}
gardRoll=curTime*gradForOneUnit;
//alert(gardRoll); //number: 354!!!!!!!!!!!!!!!!!!!! why??????
//alert(typeof(gradRoll)); //undefined!!!!!!!!!!!!! why??????
rotationArray=[curTime*gradForOneUnit, 0, 0];
//alert(typeof(rotationArray));//object
//alert(typeof(el.getAttribute("rotation"))); //object
//alert(rotationArray); //number: 162 , 0, 0
el.setAttribute("rotation", rotationArray); //dosn't work.
}
使用:
el.setAttribute("transform", "rotate3d,x,y,z,angle)");
或合并rotateX
、rotateY
、rotateZ
.
gradRoll 未定义,因为变量名称是 gardRoll,这是您在第一个警报中重复但在第二个警报中重复的拼写错误。
您似乎只想编辑旋转的 X 值,所以试试这个:
el.setAttribute("rotation", gardRoll + ' 0 ' + '0');
请注意,我使用的变量中有错别字。
我知道这有点脏,但当我遇到与位置组件类似的问题时,它对我有用。
很多人建议:
el.setAttribute("rotation", 'x'+'y' + 'z');
方法,据我所知,省略解析( 'x y z' to {x,y,z}
)并在第一名:el.setAttribute("rotation", {x:gradRoll,y:0,z:0});