phaser源码解析(二) Phaser.Utils类下pad方法

/**

*#填充字符串方法

* Javascript string pad http://www.webtoolkit.info/.

* pad = the string to pad it out with (defaults to a space) 

* dir = 1 (left), 2 (right), 3 (both)

* @method Phaser.Utils.pad 

* @param {string} str - The target string. ——#需要填充的字符 

* @param {number} len - The number of characters to be added. ——#填充的长度

* @param {string} pad - The string to pad it out with (defaults to a space).——#填充的字符

* @param {number} [dir=3] The direction dir = 1 (left), 2 (right), 3 (both).——#填充的位置(左、右、两边)

* @return {string} The padded string ——#填充好的字符

*/

pad: function (str, len, pad, dir) {



if (typeof(len) == "undefined") { var len = 0; }

if (typeof(pad) == "undefined") { var pad = ' '; }

if (typeof(dir) == "undefined") { var dir = 3; }



var padlen = 0;



if (len + 1 >= str.length)

{

switch (dir)

{

case 1:

str = Array(len + 1 - str.length).join(pad) + str;

break;



case 3:



// Math.ceil向上取整

var right = Math.ceil((padlen = len - str.length) / 2);

var left = padlen - right;

str = Array(left+1).join(pad) + str + Array(right+1).join(pad);

break;



default:

str = str + Array(len + 1 - str.length).join(pad);

break;

}

}



return str;



}

 

你可能感兴趣的:(util)