-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunBG.jsx
204 lines (201 loc) · 9.25 KB
/
runBG.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var log=function(v){
function formatObj(obj){
if(obj instanceof Object){
var des = "";
for(var names in obj){
if(names !="textItem"){
des += names + ":" + obj[names] + ";\n";
}
}
return des.toString();
}else{
return obj;
}
}
alert(formatObj(v));
}
function run(){
if(app.documents.length<1){alert("未发现任何可生成页面!请参照教程布局设计稿","警告",true);return;}
app.preferences.rulerUnits = Units.PIXELS;
var adoc = app.activeDocument;
if(adoc.width.as("px")/adoc.height.as("px")>= 0.7){
if(!confirm("设计稿高宽比不规范,要继续生成吗?", "提示")) {
return false;
}
}
var whstr = prompt("输入设计稿宽高(键入格式:750,1334),默认'750*1334'", "1", "提示");
var width = whstr.length>0?parseInt(whstr.split(',')[0]):750;
var height = whstr.length>0?parseInt(whstr.split(',')[1]):1334;//1334
var resolution = 72;
var resampleMethod = ResampleMethod.AUTOMATIC;
var amount = 50;
adoc.resizeImage(width, height, resolution, resampleMethod, amount);
var templatePath =new File($.fileName).parent+"/TemplateBG/";
try{
adoc.rasterizeAllLayers ();
}catch(err){
alert("请手动'栅格化所有图层'");
}
try{
var savePath = new Folder(adoc.path + "/H5/");
if(savePath.exists) {
if(!confirm("输出目录已存在是否覆盖?", "提示")) {
return false;
}
}
}catch(err){
alert('请先保存文件再执行脚本');
return;
}
copyTemplate(templatePath,savePath);
eachDocument(savePath,adoc);
}
//遍历图层 & 替换模版文件
function eachDocument(path,adoc){
var resources = [];
var _html="";
var startMark=1;
if(confirm("是否指定图片名开始下标?", "提示")){
startMark = parseInt(prompt("请输入图片名开始下标", "1", "提示"));
}
if(!confirm("是否确定开始生成?", "提示")){
return;
}
for(var i=0,doc=app.activeDocument.layers;i<app.activeDocument.layers.length;i++){
if(doc[i].visible){
if(doc[i].typename == "LayerSet"){
var resultHtml = replaceSymbol(doc[i],path,startMark,adoc,resources);
if(resultHtml){_html+=resultHtml}
startMark++;
}else{
alert('请按照格式规范[组合,排列]');
return false;
}
}
}
if(_html){
var htmlfile = new File(path + "/index.html");
var htmlread = "";
htmlfile.open("r");
htmlfile.encoding = 'utf-8';
htmlread = htmlfile.read();
htmlfile.close();
htmlread = htmlread.replace("{{content_html}}",_html);
htmlread = htmlread.replace("{{resourcesTpl}}",resources.join(','));
htmlfile.open("w");
htmlfile.encoding = 'utf-8';
htmlfile.write(htmlread);
htmlfile.close();
}
}
//生成 html内容 & 图片
function replaceSymbol(set,path,startMark,adoc,resources){
var modeTime_tack = 0;
var html_page = '\n\t<div class="page">{{imgItem}}\n\t</div>\n';
var html_bg='\n\t\t<div class="full" style="background:url({{BgImgSrc}}) center / cover no-repeat;-webkit-animation: scaletest 1s ease-out {{modeTIme}}s 1 both"></div>';
var html_img_t = '\n\t\t<div style="background:url({{imgSrc}}) center / cover no-repeat;position:absolute;{{imgCss}}-webkit-animation: {{aniName}} {{aniTime}}s ease {{modeTIme}}s 1 both;"></div>';
var html_img = "";
var canvasWidth = app.activeDocument.width.as("px");
var canvasHeight = app.activeDocument.height.as("px");
for(var j=set.layers.length-1;j>=0;j--){
if(set.layers[j].visible){
if(set.layers[j].typename != "ArtLayer"){
alert('请按照格式规范[组合,排列]');
return false;
}
if(set.layers[j].bounds[2].as("px")==0&&set.layers[j].bounds[3].as("px")==0){
alert("图层不能为空,请隐藏或删除空白图层!重新执行脚本");
return false;
}
if(j==set.layers.length-1){//背景判断
set.layers[j].copy();
var width = app.activeDocument.width.as("px");
var height = app.activeDocument.height.as("px");
app.documents.add(width,height, 72, "boxDoc", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
app.activeDocument.paste();
var savepath = new File(path+'/images/bg'+startMark+'.jpg');
resources.push("'/images/bg"+startMark+".jpg'");
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.JPEG;
exportOptions.quality = 100;
app.activeDocument.exportDocument(savepath, ExportType.SAVEFORWEB, exportOptions);
var arr = savepath.toString().split('/');
html_bg = html_bg.replace("{{BgImgSrc}}",'./images/'+arr[arr.length-1]).replace("{{modeTIme}}",isNaN(parseFloat(set.layers[j].name))?'1':set.layers[j].name);
}else{
var x=set.layers[j].bounds[0].as("px");
var y=set.layers[j].bounds[1].as("px");
var w=set.layers[j].bounds[2].as("px")-x;
var h=set.layers[j].bounds[3].as("px")-y;
var left=(x/canvasWidth*100).toFixed (3);
var top=(y/canvasHeight*100).toFixed (3);
var width=(w/canvasWidth*100).toFixed (3);
var height=(h/canvasHeight*100).toFixed (3);
app.activeDocument.activeLayer = set.layers[j];
var region = [
[set.layers[j].bounds[0].as('px'), set.layers[j].bounds[1].as('px')],
[set.layers[j].bounds[2].as('px'), set.layers[j].bounds[1].as('px')],
[set.layers[j].bounds[2].as('px'), set.layers[j].bounds[3].as('px')],
[set.layers[j].bounds[0].as('px'), set.layers[j].bounds[3].as('px')]
];
var type = SelectionType.REPLACE;
var feather = 0;
var antiAlias = true;
app.activeDocument.selection.select(region, type, feather, antiAlias);
app.activeDocument.selection.copy();
app.documents.add(w,h, 72, "boxDoc", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
app.activeDocument.paste();
var savepath = new File(path+'/images/'+startMark+'-'+(j+1)+'.png');
var exportOptions = new ExportOptionsSaveForWeb();
resources.push("'/images/"+startMark+"-"+(j+1)+".png'");
exportOptions.format = SaveDocumentType.PNG;
exportOptions.PNG8 = false;
app.activeDocument.exportDocument(savepath,ExportType.SAVEFORWEB, exportOptions);
var arr = savepath.toString().split('/');
var nameParams = set.layers[j].name.split('-');//解析图层名字符
var html_img_t_result = html_img_t.replace("{{imgSrc}}",'./images/'+arr[arr.length-1]);
html_img_t_result = html_img_t_result.replace("{{imgCss}}","z-index:"+(set.layers.length-j)+";left:"+left+"%;top:"+top+"%;width:"+width+"%;height:"+height+"%;")
if(nameParams.length==1 && !isNaN(parseFloat(nameParams[0]))){//1参 情况
html_img_t_result=html_img_t_result.replace("{{aniName}}",'fadeInUp');
html_img_t_result=html_img_t_result.replace("{{aniTime}}",'1');
html_img_t_result=html_img_t_result.replace("{{modeTIme}}",nameParams[0]);
}else{
html_img_t_result=html_img_t_result.replace("{{aniName}}",'fadeInUp');
html_img_t_result=html_img_t_result.replace("{{aniTime}}",'1');
html_img_t_result=html_img_t_result.replace("{{modeTIme}}",-(j-(set.layers.length-1))/2);
}
if(nameParams.length==3){//3参 情况
html_img_t_result=html_img_t_result.replace("{{aniName}}",nameParams[0]);
html_img_t_result=html_img_t_result.replace("{{aniTime}}",nameParams[1]);
html_img_t_result=html_img_t_result.replace("{{modeTIme}}",nameParams[2]);
}
html_img += html_img_t_result;
}
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = adoc;
}
}
return html_page.replace("{{imgItem}}",(html_bg+html_img));
}
//复制模板文件
function copyTemplate(src,target){
var srcdir = new Folder(src);
var targetdir = new Folder(target);
if(targetdir.exists) {
targetdir.remove();
}
targetdir.create();
var files = srcdir.getFiles();
for(var i = 0; i < files.length; i++) {
var ifile = files[i];
if(ifile instanceof Folder) {
copyTemplate(src + '/' + ifile.name, target + '/' + ifile.name);
continue;
}
var ofile = new File(targetdir + '/' + ifile.name);
if(ofile.exists) {
ofile.remove();
}
ifile.copy(ofile);
}
}
run();