-
Notifications
You must be signed in to change notification settings - Fork 34
/
document.js
449 lines (348 loc) · 8.26 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
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=>{
const img = new Image();
img.src = src;
img.onload = ()=>{
app.img = img;
vaporwave(img,app.style, src=>{
app.src = src;
});
}
});
};
const isImageRegex = /^image\/(jpeg|gif|png|bmp|webp)$/;
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 _vaporwave = (img,style,callback)=>{
clearTimeout(vaporwave.T);
vaporwave.T = setTimeout(()=>{
vaporwave(img,style,callback);
app.saveData();
},100);
};
const imageSuffixRegex = /!w300|w300\//;
const imagesScroll = e=>{
// console.log(e);
const target = e.target;
const scrollLeft = target.scrollLeft;
const offsetWidth = target.offsetWidth;
const imagesEl = target.children;
const imageEl = imagesEl[0];
const itemWidth = imageEl.offsetWidth;
const showWidth = scrollLeft + offsetWidth;
const showNum = Math.ceil( showWidth / itemWidth );
app.images.forEach((image,index)=>{
if(image.show) return;
if(index <= showNum){
image.show = true;
}
})
};
const deepCopy = o=>JSON.parse(JSON.stringify(o));
const defaultStyle = {
zoom: 1,
light: 1,
contrast: 1,
darkFade:0,
brightFade:0,
shiftX: 0,
shiftY: 0,
shiftU: 0,
shiftV: 0,
level: 4, //颜色断层
interlaced: 0, //隔行扫描
interlacedStrong:1,
interlacedLine:4,
interlacedLight:0,
transposeX: 0,
transposePow: 4,
transposeNoise:0.04,
replace:0,
vividU : 1,
vividV : 1,
sizeOrigin: false,
sharpen: true,
invertLight: false,
yuv420:true,
yuv420Noise:1,
snow:false,
quality:0.6,
damage:0,
ratio:1,
lightNoise:0, //明度噪声
darkNoise:0, //胶片噪声
fit:'cover',
border:0,
none:false,
watermark:false,
watermarkSize:0.7,
watermarkLeft:0.5,
watermarkTop:0.75,
watermarkAlpha:1,
styleName:false,
styleLeft:0.07,
styleTop:0.16,
styleSize:0.7,
wide:'4:3',
convoluteName:'右倾'
};
const userStyles = JSON.parse(localStorage.getItem('userStyles')||'[]');
const data = {
src:'',
img:null,
showStyle:true,
style:{
...defaultStyle,
...styles[0]
},
styles,
userStyles,
globalStyles:[],
imageBaseURi:'captures/',//'https://image.magiconch.com/90s/captures/',
images
};
const isKanjiRegex = /^[\u0800-\u9fa5]+$/;
const diffDefaultStyle = style=>{
for(let key in style){
if(style[key] === defaultStyle[key]){
delete style[key];
}
}
};
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.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 API_URL = 'https://lab.magiconch.com/api/vaporwave/';
request('GET',API_URL+'style',null,styles=>app.globalStyles = styles);
const uploadImage = options =>{
if(!options) return;
const file = options.file;
if(!file.type.match(/^image\//)) return console.log(/文件类型不正确/,file);
const callback = options.callback;
const plan = options.plan;
request('POST',API_URL.replace(/vaporwave\//,'upyun/authorization'),{
plan,
},upyun=>{
let body = new FormData();
body.append('file', file);
body.append('policy', upyun.policy);
body.append('authorization', upyun.authorization);
fetch(upyun.api + upyun.bucket,{
method:'POST',
mode: 'cors',
body,
}).then(res => res.json()).then(data =>{
data.imageURL = `${upyun.prefix}${data.url}`;
callback(data);
}).catch(error => console.error(error))
});
};
const postStyle = ()=>{
const name = prompt('输入四字中日文滤镜名(务必是四个中日文字)\n提交预设会上传当前画面图像为预览');
if(!name) return;
if(!isKanjiRegex.test(name.trim())) return alert('这不是四字中日文,请检查后重新提交');
let author = prompt('输入作者信息(可留空)');
if(!author){
author = undefined;
}
style = deepCopy(app.style);
diffDefaultStyle(style);
style.name = name;
canvas.toBlob(file=>{
uploadImage({
file,
plan:'output',
callback(r){
request('POST',API_URL+'style',{
preview:r.imageURL,
author,
style
},()=>{
alert('感谢提交公共预设,审核通过即会在公共样式中出现。');
});
}
});
},'image/jpeg',app.style.quality);
};
const uploadCapture = ()=>{
let author = prompt('输入作者信息(可留空)');
if(!author){
author = undefined;
}
chooseFile(file=>{
uploadImage({
file,
plan:'capture',
callback(r){
request('POST',API_URL+'capture',{
src:r.imageURL,
author
},()=>{
alert('感谢提交样图,经过审核通过就有可能展示在界面上。');
});
}
});
});
};
const setLogoFromFile = ()=>{
chooseFile(file=>{
readFileToURL(file,src=>setLogoByURL(src,app.vaporwave));
});
};
const app = new Vue({
el:'.app',
data,
methods: {
setStyle(style){
this.style = deepCopy({
...defaultStyle,
...style
});
this.vaporwave();
},
addStyle(){
const name = prompt('输入四字中日文滤镜名(务必是四个中日文字)');
if(!name) return;
if(!isKanjiRegex.test(name.trim())) return alert('这不是四字中日文,请检查后重新提交');
const style = deepCopy(this.style);
style.name = name;
diffDefaultStyle(style);
console.log(JSON.stringify(style,0,4));
this.userStyles.push(style);
this.saveData();
},
removeUserStyle(style){
if(confirm('确定删除这条预设?删除后不可恢复')){
this.userStyles.splice(this.userStyles.indexOf(style),1);
}
this.saveData();
},
saveData(){
localStorage.setItem('userStyles',JSON.stringify(app.userStyles));
},
_vaporwave(){
_vaporwave(this.img,this.style,src=>{
app.src = src;
});
},
vaporwave(){
setTimeout(_=>clearTimeout(vaporwave.T))
vaporwave(this.img,this.style,src=>{
app.src = src;
});
},
setImageAndDraw(e){
let img = e.target;
if(imageSuffixRegex.test(img.src)){
img.onload = _=>{
this.img = e.target;
this.vaporwave();
};
img.src = img.src.replace(imageSuffixRegex,'')//!h480
return;
}
this.img = img;
this.vaporwave();
},
voteStyle(){
},
output(){
const a = document.createElement('a');
a.href = this.src;
a.download = `[lab.magiconch.com][90s-time-machine]-${+Date.now()}.jpg`;
a.click();
},
chooseFile(){
chooseFile(readFileAndSetIMGSrc)
},
postStyle,
uploadCapture,
setLogoFromFile,
imagesScroll,
},
watch:{
style:{
deep:true,
handler(){
this._vaporwave();
}
}
}
});
const loadScript = (src,el) =>{
el = document.createElement('script');
el.onload = _=> document.body.removeChild(el);
el.src = src;
document.body.appendChild(el);
return el;
};
let _hmt = [];
let dataLayer = [
['js', new Date()],
['config', 'G-13BQC1VDD8']
];
window.gtag = function() {
dataLayer.push(arguments);
};
setTimeout(_ => {
loadScript('//hm.baidu.com/hm.js?f4e477c61adf5c145ce938a05611d5f0');
loadScript('//www.googletagmanager.com/gtag/js?id=G-13BQC1VDD8');
},400);