真的很简单的问题,我有一个相机,它使用变换移动/缩放/旋转,我想这一切都是在GPU上完成的(所以,使用变换)。使用一个转换(translation()
),它可以很好地工作,没有任何问题。但是,如果我把另外2个加进去,它就突然变成了一个空字符串:
namespace `game.modules`(
class Camera{
constructor(tag){
this.tag = tag;
this.x = 0;
this.y = 0;
this.rotation = 0;
this.scaleX = 1;
this.scaleY = 1;
this.animations = []
}
lookAt(object){
this.x=object.x;
this.y=object.y;
}
onDraw(){
this.setTagFromData();
}
setTagFromData(){
var x = -(this.x-100);
var y = -(this.y-60);
this.tag.style.transform = `translate3d(${x}px, ${y}px, 0px)`
console.log(this.tag.style.transform)
}
}
)
这将输出预期的字符串(Translate3D(50px,50px,0px)
)。但是,添加其他属性,如下所示:
namespace `game.modules`(
class Camera{
constructor(tag){
this.tag = tag;
this.x = 0;
this.y = 0;
this.rotation = 0;
this.scaleX = 1;
this.scaleY = 1;
this.animations = []
}
lookAt(object){
this.x=object.x;
this.y=object.y;
}
onDraw(){
this.setTagFromData();
}
setTagFromData(){
var x = -(this.x-100);
var y = -(this.y-60);
this.tag.style.transform = `translate3d(${x}px, ${y}px, 0px) rotate(${this.ang}deg) scale(${this.scaleX}, ${this.scaleY})`
console.log(this.tag.style.transform)
}
}
)
记录空字符串,甚至不记录Translate3D()
。为什么会出现这种情况?
正如你在评论中确认的:
由于this.ang
未定义,生成的字符串中包含单词undefined
,因此它是无效的CSS表达式。众所周知,CSS会忽略无效的表达式,因此整个字符串被简单地丢弃。