I have a function that creates a css transform property string based on some global variables. This string can be copied later by a user, so I would prefer to keep the output short.
This is my code:
var createTransformation = () => {
var final_transformation = "";
if (x)
final_transformation += "rotateX(" + x + "deg)";
if (y)
final_transformation += " rotateY(" + y + "deg)";
if (z)
final_transformation += " rotateZ(" + z + "deg)";
if (scalex !== 1 || scaley !== 1)
final_transformation += " scale(" + scalex + "," + scaley + ")";
if (translatex !== 0 || translatey !== 0 || translatez !== 0) {
if (translatex !== 0 && translatey == 0 && translatez == 0)
final_transformation += " translateX(" + translatex + "px)";
else if (translatey !== 0 && translatex == 0 && translatez == 0)
final_transformation += " translateY(" + translatey + "px)";
else if (translatez !== 0 && translatex == 0 && translatey == 0)
final_transformation += " translateZ(" + translatez + "px)";
else if (translatex == 0)
final_transformation += " translateY(" + translatey + "px) translateZ(" + translatez + "px)";
else if (translatez == 0)
final_transformation += " translateX(" + translatex + "px) translateY(" + translatey + "px)";
else if (translatey == 0)
final_transformation += " translateX(" + translatex + "px) translateZ(" + translatez + "px)";
else
final_transformation += " translateX(" + translatex + "px) translateY(" + translatey + "px) translateZ(" + translatez + "px)";
}
//if scalex != 1 || scaley != 1
//If the skew's sliders have been changed e.g, moved from 0 to 1 or from 0 to 100 add skew property
if (skewX || skewY)
final_transformation += " skew(" + skewX + "deg," + skewY + "deg)";
return final_transformation;
};
As you can see there are a lot of if
statements. I was wondering, is there anything I can do to improve this code?