Skip to content

Instantly share code, notes, and snippets.

@yulanggong
Created October 31, 2012 01:52
Show Gist options
  • Save yulanggong/3984344 to your computer and use it in GitHub Desktop.
Save yulanggong/3984344 to your computer and use it in GitHub Desktop.
多态按钮图片的横纵向转换
/**
* 多态按钮图片的横纵向转换
* 在 Photoshop CS6 下测试过
* 2012-10-30
* @param {Array} map 转换前后的按钮位置映射关系,下面有例子
* @param {String} flag 方向标志, v2h 纵转横, h2v 横转纵
* @param {Document} [doc] 默认为当前文档
*/
function changeDirection(map, flag, doc){
doc = doc || activeDocument;
var count = map.length;
var v2h = flag == 'v2h';
var width = doc.width;
var height = doc.height;
var itemWidth = v2h ? width : width / count;
var itemHeight = v2h ? height / count : height;
var newWidth = v2h ? itemWidth * count : itemWidth;
var newHeight = v2h ? itemHeight : itemHeight * count;
//移动一个选区
function moveSelection(from, to){
var v = v2h ? from : to;
var h = v2h ? to : from;
var vx1 = 0;
var vx2 = itemWidth;
var vy1 = itemHeight * v;
var vy2 = itemHeight * (v + 1);
var hx1 = itemWidth * h;
var hx2 = itemWidth * (h + 1);
var hy1 = 0;
var hy2 = itemHeight;
//纵向上的选区
var vRegion = [
[vx1, vy1],
[vx2, vy1],
[vx2, vy2],
[vx1, vy2],
[vx1, vy1]];
//横向上的选区
var hRegion = [
[hx1, hy1],
[hx2, hy1],
[hx2, hy2],
[hx1, hy2],
[hx1, hy1]];
var fromRegion = v2h ? vRegion : hRegion;
var toRegion = v2h ? hRegion : vRegion;
doc.selection.select(fromRegion);
doc.selection.cut();
doc.selection.select(toRegion);
doc.paste();
//artLayers[0] 对应从上往下第一个图层
doc.artLayers[0].merge();
}
doc.resizeCanvas(itemWidth * count, itemHeight * count, AnchorPosition.TOPLEFT);
for (var i = 0; i < map.length; i++){
var step = map[i];
//都为 0 时不需要移动
if (!(step[0] === 0 && step[1] === 0)){
moveSelection.apply(this , step);
}
}
doc.resizeCanvas(newWidth, newHeight, AnchorPosition.TOPLEFT);
}
//把一个 4 态的按钮图片由纵向转横向,顺序不变
changeDirection([
[0, 0]
, [1, 1]
, [2, 2]
, [3, 3]
], 'v2h');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment