forked from itorr/patina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.js
319 lines (281 loc) · 6.34 KB
/
document.js
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
const readFileToURL = (file,onOver)=>{
var reader = new FileReader();
reader.onload = ()=>{
const src = reader.result;
onOver(src);
};
reader.readAsDataURL(file);
};
const readFileAndSetIMGSrc = file=>{
readFileToURL(file,src=>{
app.$refs.img.src = src;
});
};
function chooseFileAndSetImageSrc(){
chooseFile(readFileAndSetIMGSrc)
}
document.addEventListener('paste',e=>{
// console.log(e.clipboardData,e.clipboardData.files);
const clipboardData = e.clipboardData;
if(clipboardData.items[0]){
let file = clipboardData.items[0].getAsFile();
if(file && isImageRegex.test(file.type)){
return readFileAndSetIMGSrc(file);
}
}
if(clipboardData.files.length){
for(let i = 0;i<clipboardData.files.length;i++){
if(isImageRegex.test(clipboardData.files[i].type)){
// console.log(clipboardData.files[i])
readFileAndSetIMGSrc(clipboardData.files[i]);
}
}
}
});
document.addEventListener('dragover',e=>{
e.preventDefault();
});
document.addEventListener('drop',e=>{
e.preventDefault();
const file = e.dataTransfer.files[0];
if(file && file.type.match(isImageRegex)){
readFileAndSetIMGSrc(file);
}
});
const chooseFile = callback=>{
chooseFile.form.reset();
chooseFile.input.onchange = function(){
if(!this.files||!this.files[0])return;
callback(this.files[0]);
};
chooseFile.input.click();
};
chooseFile.form = document.createElement('form');
chooseFile.input = document.createElement('input');
chooseFile.input.type = 'file';
chooseFile.input.accept = 'image/*';
chooseFile.form.appendChild(chooseFile.input);
const request = (method,uri,data,callback)=>{
let body = null;
if(data){
body = JSON.stringify(data);
}
fetch(uri,{
method,
mode: 'cors',
body,
credentials: 'include',
headers: {
'content-type': 'application/json'
}
}).then(res => res.json()).then(data => callback(data)).catch(error => console.error(error))
};
const isGIF = async (src) => {
return await fetch(src)
.then(response => response.arrayBuffer())
.then(arrBuff => new Uint8Array(arrBuff).map(byte => byte.toString(16).padStart(2, '0')).slice(0, 4).join(''))
.then(str => str === '47494638')
.catch(() => null)
}
const isImageRegex = /^image\/(.+)$/;
const deepCopy = o=>JSON.parse(JSON.stringify(o));
let defaultConfig = {
isPop:false,
preview:true,
pop:4, //波普
maxWidth:500,
zoom: 100,
mix:1, //像素合并
level: 4, //颜色断层
lightNoise:0, //明度噪声
darkNoise:0, //胶片噪声
shiftx:0,
shifty:0,
light:0,//亮度
contrast:1, //对比度
convoluteName:null, //convolute
quality: 60,
green:true, //贴吧绿图
g:0,
gy:1,
round: 12,
rand:true, //随机
watermark: true,
watermarkSize:1,
watermarkPlan:2,
watermarkShadowAlpha:.6,
};
const userNamesText = `卜卜口
拆家大主教
HomeArchbishop
_Home_Archbishop_
_蒸_気_機_
能不能好好说话
神奇海螺_0000
magiconch.com
电脑玩家海螺
电子包浆
阿卡梦
_阿_卡_梦_
极限天空
_极_限_天_空_
任意门穿梭了时光
干啥都成功的球球
Uahh_
夹去阳间
大吉山放送部
绫波
_绫_波_丽_
_明_日_香_
_久_美_子_
Lilin_0000
Seele_0000
EVANGELION
樱岛麻衣
樱岛麻衣俺老婆0000
_樱岛麻衣
电脑玩家0000
fps爱好者
蒙古上单
黄前久美子
高坂丽奈
川岛绿辉
平泽唯
秋山澪
田井中律
琴吹䌷_
中野梓
山中佐和子
凉宫春日
长门有希
_鹤_屋_
泽渡真琴
冈崎朋也
——古河渚
藤林杏_
藤林椋_
坂上智代
春原阳平
长野原美绪
折木奉太郎
千反田爱瑠
伊原摩耶花
富樫勇太
小鸟游六花
丹生谷森夏
五月七日茴香
凸守早苗
北白川玉子
德拉·打糕难吃
冢本秀一
薇尔莉特·伊芙加登
伊吹公子
冈崎汐0000`;
let config = deepCopy(defaultConfig);
config.userNames = userNamesText.trim().split('\n')
const defaultImageURL = 'totoro-avatar.jpg';
const data = {
src:defaultImageURL,
defaultImageURL,
// src:'hibike-capture.png',
// src:'IMG_7076.JPG',
// src: 'chiya.gif',
// src: 'panda.gif',
downloadFileName:'[lab.magiconch.com][电子包浆].jpg',
output:null,
img:null,
direction:'vertical',
runing:false,
current:0,
debug:false,
config,
width:400,
userNamesText,
superMode:false,
convoluteNames,
isGIF: false,
isLoadingGIF: false,
isPackingGIF: false,
lastConfig: {}
};
const app = new Vue({
el:'.app',
data,
methods:{
patina(){
if (this.isGIF) {
patinaGIF(this.$refs.img,this.config,app)
} else {
patina(this.$refs.img,this.config,app)
}
},
_patina(){
clearTimeout(this.T)
this.T = setTimeout(this.patina,300)
},
async load(){
const imageEl = this.$refs.img;
let _width = imageEl.naturalWidth;
let _height = imageEl.naturalHeight;
let scale = _width / _height;
let direction = scale > 1.2 ? 'horizontal' : 'vertical';
this.isGIF = await isGIF(imageEl.src)
app.direction = direction;
app.patina();
},
chooseFileAndSetImageSrc,
reset(){
const _config = deepCopy(defaultConfig)
_config.userNames = this.userNamesText.trim().split('\n')
this.config = _config
},
save(e){
this.downloadFileName = `[lab.magiconch.com][电子包浆]-${+Date.now()}.jpg`;
// a.click();
}
},
watch:{
config:{
deep:true,
handler(config){
console.log(config)
const maxWidth = config.maxWidth;
document.documentElement.style.setProperty('--max-width', `${maxWidth}px`);
if (!this.isGIF) {
this._patina();
}
}
},
userNamesText(text){
this.config.userNames = this.userNamesText.trim().split('\n')
},
maxWidth(maxWidth){
}
},
computed:{
isShouldRedoGIF () {
return JSON.stringify(this.lastConfig) !== JSON.stringify(this.config)
}
}
})
if(!/\/$/.test(location.pathname))location.pathname = location.pathname + '/';
const loadScript = (src,onLoad=_=>{},el) =>{
el = document.createElement('script');
el.src = src;
el.onload = onLoad;
document.body.appendChild(el);
};
window._hmt = [];
const gtmId = 'G-13BQC1VDD8';
window.dataLayer = [
['js', new Date()],
['config', gtmId]
];
window.gtag = function(){dataLayer.push(arguments)};
setTimeout(_=>{
loadScript('https://hm.baidu.com/hm.js?f4e477c61adf5c145ce938a05611d5f0');
loadScript('https://www.googletagmanager.com/gtag/js?id='+gtmId);
loadScript('https://sdk.51.la/js-sdk-pro.min.js',_=>{
LA.init({id: "JgPCvOAtY0gH7fbQ",ck: "JgPCvOAtY0gH7fbQ",autoTrack:true,hashMode:true})
})
},1400);