forked from xingqiao/cLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
351 lines (321 loc) · 14.1 KB
/
app.js
File metadata and controls
351 lines (321 loc) · 14.1 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var Page = {
obj: {},
setting: {
engine: 1,
type: 0, // 页面类型,0-h5页,1-PC页
size: 640,
scope: 10, // 容错值,解决因为压缩导致的颜色偏差
unitSize: 0, // 精细度,默认为宽度的四十分之一
limit: 900, // 切片高度,超过这个值会进行分割
combine: 1, // 是否开启小图片合并
quality: 0.7, // 图像质量
viewSize: 640
},
log: function(text) {
if (!Page.logIndex) {
Page.logIndex = 0;
}
console.log.apply(console, arguments);
if (!Page.$log || !Page.$log.length) {
Page.$log = $(".js_console");
}
if (Page.$log.length) {
Page.$log.show().append($("<div>").text("[" + Page.logIndex + "] " + Array.prototype.join.call(arguments, " "))).scrollTop(Page.$log[0].scrollHeight);
Page.logIndex++;
}
},
// 切换导航显示
nav: function(cur, target) {
index = cur ? Math.abs(cur) : 0;
// 切换导航
$(".js_nav .current")
.not($(".js_nav dd:eq(" + index + ")").addClass("current"))
.removeClass("current");
// 标记按钮
$(".js_next").attr("data-target", target || (index + 1));
// 菜单切换
var $curMenu = $(".js_menu[data-nav=" + index + "]").show();
$(".js_menu").not($curMenu).hide();
// 其他初始化操作
if (index == 0) {
$(".js_set").hide();
$(".js_panel").empty();
$(".js_set").hide();
delete Page.obj.$workbench;
$(".js_selectfile").fadeIn(500);
} else if (index == 1) {
$(".js_set").fadeIn();
}
},
createImgFigure: function(opts) {
return $("<figure>")
.addClass("panel-img " + (opts.className || ""))
.append($("<figcaption>").text(opts.title || ""))
.append($("<div>").addClass("content").append(opts.content));
},
formatTitle: function(filename, originalWidth, originalHeight, width){
var zoom = Math.round(1000 * width / originalWidth) / 10;
return "[" + originalWidth + " ⨯ " + originalHeight + (zoom != 1 ? " @ " + zoom + "%" : "") + "] " + filename;
},
// 加载图片到canvas
loadImgToCanvas: function() {
var width = this.setting.size,
viewSize = this.setting.viewSize;
// PC页不进行缩放
if (this.setting.type == 1) {
width = null;
}
// 加载图片到canvas
var c = CL.loadImgToCanvas(this.obj.img, {width: width, viewWidth: viewSize, backgroundColor: "#fff"});
var o = {
width: c.width,
height: c.height,
img: this.obj.img,
canvas: {
img: c.canvas
}
};
$.extend(this.obj, c, o);
this.obj.canvas.cover = CL.createCanvas({width: c.width, height: c.height, viewWidth: viewSize, name: "cover"});
if (this.obj.$workbench) {
this.obj.$workbench.find("figcaption").html(this.formatTitle(this.setting.filename, c.originalWidth, c.originalHeight, c.width));
this.obj.$workbench.find(".content").empty().append(this.obj.canvas.img, this.obj.canvas.cover);
}
},
// 加载图片文件
loadImgFile: function(file) {
if (file) {
CL.openImgFile(file, function(err, img){
if (err) {
return alert(err);
} else if (img.width < 320) {
return alert("所选择的图片太小,请选择宽度不小于320的图片")
}
// 隐藏水印
$(".watermark").hide();
// 图片信息
Page.obj.img = img;
var title = Page.formatTitle(file.name, img.width, img.height, Page.setting.size);
Page.log("加载图片:" + title);
Page.setting.filename = file.name;
// 展示图片
Page.loadImgToCanvas();
var cWidth = $(".js_body").width();
Page.obj.$workbench = Page.createImgFigure({
content: [Page.obj.canvas.img, Page.obj.canvas.cover],
wrap: "panel panel-img",
title: title
})
.css({"display": "none", "margin-left": (cWidth - Page.setting.viewSize - 16) / 2})
.appendTo(".js_panel")
.fadeIn()
.delay(500)
.animate({marginLeft: 0}, 500, function(){Page.nav(1)});
Page.obj.$workbench.append($("<div>").addClass("console js_console").hide());
});
}
},
// 分析
analyse: function() {
var time = new Date();
var time2 = time;
console.time("analyse");
CL.analyse(Page.obj.canvas.img, $.extend({
onprogress: function() {
console.log("[" + this.index + "/" + this.count + "]", new Date() - time, new Date() - time2);
time2 = new Date();
}
}, Page.setting), function(error, data){
console.timeEnd("analyse");
var allTime = new Date() - time;
console.log(data)
if (data && data.list) {
Page.result = data;
// 绘制分割结果
var $result = $(".js_result_body tbody").empty();
var cover = Page.obj.canvas.cover;
ctx = cover.getContext("2d");
ctx.clearRect(0, 0, cover.width, cover.height);
ctx.textBaseline = "top";
ctx.font = "14px 黑体";
var _append = function (index, item, position) {
var $tr = $("<tr>").html(
"<td>" + index + "</td>"
+ "<td>" + item.width + "</td>"
+ "<td>" + item.height + "</td>"
+ "<td>" + position + "</td>"
+ "<td><a><img></a></td>"
);
$tr.find("a").attr({target: "_blank", href: item.data.src});
$tr.find("img").attr("src", item.data.src);
$result.append($tr);
};
data.list.forEach(function(item, index) {
var position = [];
if (!item.list) {
ctx.fillStyle = "rgba(0, 0, 255, .4)";
ctx.fillRect(item.left, item.top, item.width, item.height);
ctx.strokeStyle = "rgba(255, 255, 255, .4)";
ctx.strokeRect(item.left, item.top, item.width, item.height);
ctx.fillStyle = "rgb(255, 255, 255)";
ctx.fillText(index + ": " + item.width + " * " + item.height, item.left + 5, item.top + 5);
position.push("[" + item.left + "," + item.top + "]");
} else {
item.list.forEach(function(sub, j) {
ctx.fillStyle = "rgba(0, 0, 255, .4)";
ctx.fillRect(sub.originalLeft, sub.originalTop, sub.width, sub.height);
ctx.strokeStyle = "rgba(255, 255, 255, .4)";
ctx.strokeRect(sub.originalLeft, sub.originalTop, sub.width, sub.height);
ctx.fillStyle = "rgb(255, 255, 255)";
ctx.fillText(index + "." + (j + 1) + ": " + sub.width + " * " + sub.height, sub.originalLeft + 5, sub.originalTop + 5);
position.push("[" + sub.originalLeft + "," + sub.originalTop + " @ " + sub.left + "," + sub.top + "]");
});
}
_append(index, item, position.join("<br>"));
});
if (data.backgroundImage) {
var position = [];
var bgtype = parseInt(data.opts && data.opts.bgtype || 0);
if (bgtype & 1) {
position.push("浮动");
}
if (bgtype & 2) {
position.push("平铺");
}
if (bgtype & 3) {
position.push("拉伸");
}
_append(index, data.backgroundImage, position.join("<br>"));
}
$(".js_result h3").empty().append("分割出图片数:" + data.list.length);
$(".js_result h4").html("总耗时:" + allTime + "ms / 解析耗时:" + data.engineTime + "ms");
$(".js_result").fadeIn();
} else {
alert(error || "解析图片失败");
}
});
},
// 初始化
init: function() {
if (!window.FileReader) {
alert("当前浏览器不支持该页面,请升级到IE11,或更换Chrome浏览器。")
} else {
var _togglePanel = function () {
var $curSet = $(".js_set .js_item[data-part=\"" + Page.setting.type + "\"]").show();
$(".js_set .js_item[data-part]").not($curSet).hide();
};
$(document)
// 选择文件
.on("dragover dragleave dragend", function(e){return false})
.on("drop", function(e){
Page.loadImgFile(e.originalEvent.dataTransfer.files[0]);
return false;
})
.on("click", ".js_selectfile", function() {
var $o = $("#photo-input");
if (!$o.length) {
$o = $('<input type="file" id="photo-input" style="display:none" accept="image/*">').change(function(){
Page.loadImgFile(this.files[0]);
}).appendTo("body");
};
$o.trigger("click");
})
// 设置
.on("change", ".js_setting", function(e) {
var $o = $(this);
var name = $o.attr("data-name");
var value = this.value;
if (/^(?:range|number)$/.test(this.type)) {
value = +value;
var min = $o.attr("min");
if (!isNaN(min) && value < min) {
this.value = value = min;
}
var max = $o.attr("max");
if (!isNaN(max) && value > max) {
this.value = value = max;
}
} else if (this.type === "file") {
var file = this.files && this.files[0];
if (file) {
CL.openImgFile(file, function(err, img){
if (!err) {
Page.setting[name] = img;
}
});
}
} else if (name === "bgtype") {
value = 0;
$('.js_setting[data-name="bgtype"]:checked').each(function () {
if (this.value > 0) {
value += parseInt(this.value);
}
})
}
Page.setting[name] = value;
if (value != null) {
if (name === "type") {
var $o = $o.parents(".js_menu");
_togglePanel();
if (Page.obj && Page.obj.img) {
Page.loadImgToCanvas();
}
} else if (name === "size") { // 更改页面宽度
Page.loadImgToCanvas();
}
}
})
.on("input", ".js_setting", function(e) {
if (/^(?:range|number)$/.test(this.type)) {
var $o = $(this);
var value = this.value;
$o.siblings("input").val(value);
}
})
.on("click", ".js_reload", function(e) {
location.reload();
})
.on("click", ".js_start", function(e) {
Page.analyse();
})
.on("click", ".js_result_close", function(e) {
$(".js_result").fadeOut();
});
$(".js_setting").each(function() {
var name = this.getAttribute("data-name"),
value = this.value;
if (value != null && (this.type != "radio" || this.checked)) {
Page.setting[name] = this.value;
}
});
_togglePanel();
// Electron环境,允许生成页面
if (typeof require !== "undefined") {
var ipcRenderer = require("electron").ipcRenderer;
ipcRenderer.on("export-result-success", function(event, ret) {
console.log(ret);
try {
ret = JSON.parse(ret);
if (ret.code != 0) {
alert("导出失败:" + ret.error);
} else {
alert("导出成功:" + ret.data);
}
} catch (error) {
alert("导出失败:" + error);
}
});
$(document).on("click", ".js_result_export", function(e) {
ipcRenderer.send("export-result", JSON.stringify(Page.result));
});
} else {
$(".js_result_export").remove();
}
Page.nav();
}
}
};
;(function(){
Page.init();
$(".js_global_setting").trigger("change");
})();