-
Notifications
You must be signed in to change notification settings - Fork 702
/
Copy pathanime4k.vue
200 lines (192 loc) · 6.12 KB
/
anime4k.vue
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
<template>
<div class="anime4k">
<nya-container title="Anime4k">
<div class="inputbtn">
<nya-input v-model="txtSrc" label="请输入视频/图片地址" placeholder="https://..." />
<button type="button" class="nya-btn" @click="onSourceChanged">
生成
</button>
</div>
<div class="nya-subtitle">
或者使用本地图片/视频
</div>
<nya-input
v-model="file"
fullwidth
accept="image/*, video/*"
type="file"
placeholder="点击这里选择文件"
@change="onSelectFile"
/>
<div class="nya-subtitle">
Scale
</div>
<client-only>
<vue-slider
v-model="scale"
lazy
:min="1"
:max="4"
:interval="0.01"
@dragging="onScaleChanged"
/>
</client-only>
<div class="nya-subtitle">
Bold
</div>
<client-only>
<vue-slider v-model="bold" lazy :min="0" :max="8" :interval="0.001" />
</client-only>
<div class="nya-subtitle">
Blur
</div>
<client-only>
<vue-slider v-model="blur" lazy :min="0" :max="8" :interval="0.001" />
</client-only>
</nya-container>
<nya-container title="预览">
{{ errorMsg }}
<canvas v-show="!errorMsg" ref="canvas" style="width:100%"></canvas>
<video
ref="video"
style="display:none"
autoplay
loop
preload="auto"
crossorigin="anonymous"
@canplaythrough="onCanplaythrough"
@loadedmetadata="onLoadedmetadata"
@error="onError"
></video>
</nya-container>
<nya-container title="说明">
<ul class="nya-list">
<li>算法来源:<a href="https://github.com/bloc97/Anime4K" target="_blank" rel="noopener noreferrer">Anime4K</a></li>
<li>只是把官方 web 实现简单的移植到了mikutools;纯浏览器实现,因此原图(视频)、Scale 过大会卡死,请珍惜您的设备</li>
<li>暂时不支持下载视频</li>
</ul>
</nya-container>
</div>
</template>
<script>
import Scaler from '../utils/anime4k';
import 'vue-slider-component/theme/default.css';
let VueSlider;
if (process.browser) {
VueSlider = require('vue-slider-component');
}
export default {
name: 'Anime4k',
head() {
return this.$store.state.currentTool.head;
},
components: {
VueSlider
},
data() {
return {
txtSrc: '',
file: null,
scale: 1.25,
bold: 6,
blur: 2,
scaler: null,
gl: null,
errorMsg: '请先选择图片视频!'
};
},
mounted() {
this.initScaler();
},
methods: {
initScaler() {
const canvas = this.$refs.canvas;
this.gl = canvas.getContext('webgl');
this.scaler = new Scaler(this.gl);
function render() {
if (this.scaler) {
this.scaler.bold = parseFloat(this.bold);
this.scaler.blur = parseFloat(this.blur);
this.scaler.render();
}
requestAnimationFrame(render.bind(this));
}
requestAnimationFrame(render.bind(this));
},
getSourceType(uri) {
const movTypes = ['mp4', 'webm', 'ogv', 'ogg'];
let ext = uri
.split('.')
.pop()
.split(/#|\?/)[0];
for (let i = 0; i < movTypes.length; ++i) {
if (ext === movTypes[i]) {
return 'mov';
}
}
return 'img';
},
changeImage(src) {
this.$refs.video.pause();
const inputImg = new Image();
inputImg.crossOrigin = 'Anonymous';
inputImg.src = src;
inputImg.onload = function() {
this.errorMsg = '';
const scale = parseFloat(this.scale);
this.scaler.inputImage(inputImg);
this.scaler.resize(scale);
}.bind(this);
inputImg.onerror = function() {
this.errorMsg = "Can't load the image.";
};
},
changeVideo(src) {
const video = this.$refs.video;
video.src = src;
},
onSourceChanged() {
const uri = this.txtSrc;
if (this.getSourceType(uri) == 'img') {
this.changeImage(uri);
} else {
this.changeVideo(uri);
}
},
onCanplaythrough({ target }) {
this.errorMsg = '';
target.play();
},
onLoadedmetadata({ target }) {
this.scaler = new Scaler(this.gl);
this.scaler.inputVideo(target);
this.scaler.resize(this.scale);
},
onError() {
this.errorMsg = "Can't load the video.";
},
onScaleChanged(value) {
this.scaler.resize(parseFloat(value));
},
onSelectFile({ target }) {
if (target.files && target.files[0]) {
let reader = new FileReader();
reader.onload = function(e) {
let src = e.target.result;
if (this.getSourceType(target.value) == 'img') {
this.changeImage(src);
} else {
this.changeVideo(src);
}
}.bind(this);
reader.readAsDataURL(target.files[0]);
}
}
}
};
</script>
<style>
.nya-subtitle {
margin-top: 15px;
}
</style>