-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (155 loc) · 4.61 KB
/
index.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
'use strict'; {
const optionsUrl = `http://api.viqeo.tv/v1/data/init` +
`?video[]=1853477d5dcac86d1260&profile=12`;
const vnClass = `vn`,
imgReadyClass = `vn_img-ready`,
videoReadyClass = `vn_video-ready`,
videoAnimatedClass = `vn_animated`;
/**
* Image disappeare duration in seconds.
*/
const disappearDuration = 3;
/**
* Frames per second (for invert video).
*/
const fps = 25;
const videoType = `video/mp4`;
const videoSelector = `MediaFiles [type="${videoType}"]`;
window.addEventListener(`load`, () => {
run(optionsUrl);
});
/**
* Run all async work.
*/
const run = async (optionsUrl) => {
let dataUrl, previewImage, videoUrl;
let vnOptions = localStorage.vnOptions;
/**
* Load urls.
*/
if (vnOptions) {
({ dataUrl, previewImage, videoUrl } = JSON.parse(vnOptions));
} else {
const options = await fetch(optionsUrl);
({ formats: [{ dataUrl, options: { previewImage } }] } =
await options.json());
const dataXml = await new Promise((res, rej) => {
const xhr = new XMLHttpRequest;
xhr.open(`GET`, dataUrl, true);
xhr.onreadystatechange = () => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status !== 200) {
rej(new Error(`Network error: ${xhr.status}`));
}
res(xhr.responseXML);
};
xhr.send();
});
/**
* @todo Fix crossOrigin request.
*/
//videoUrl = dataXml.querySelector(videoSelector).textContent;
videoUrl = `./test.mp4`;
localStorage.vnOptions = vnOptions =
JSON.stringify({ dataUrl, previewImage, videoUrl });
}
/**
* Get container.
*/
const container = document.querySelector(`.${vnClass}`),
rectangle = container.getBoundingClientRect(),
width = Math.floor(rectangle.width);
/**
* Add image.
*/
const img = new Image;
img.src = previewImage;
img.width = width;
img.style.transition = `opacity ${disappearDuration}s`;
container.classList.add(imgReadyClass);
container.appendChild(img);
await new Promise((res, rej) => {
img.addEventListener(`load`, res);
img.addEventListener(`error`, rej);
});
const height = img.clientHeight || 220;
/**
* Add video.
*/
const video = document.createElement(`video`);
video.preload = `auto`;
video.width = width;
video.height = height;
const canplayCallback = () => {
container.classList.add(videoReadyClass, videoAnimatedClass);
/**
* Add inverting.
*/
changePixelColors(video, invertColors, fps);
setTimeout(() => {
video.removeEventListener(`canplay`, canplayCallback);
video.play();
container.classList.remove(videoAnimatedClass);
}, disappearDuration * 1000);
};
video.addEventListener(`canplay`, canplayCallback);
const source = document.createElement(`source`);
source.src = videoUrl;
source.type = videoType;
video.appendChild(source);
container.appendChild(video);
};
/**
* @return {number[4]}
*/
const invertColors = (r, g, b, a) => [
255 - r, 255 - g, 255 - b, a
];
/**
* @param {!HTMLVideoElement} video - video element for changing colors.
* @param {function(r, g, b, a): number[4]} colorMap - function for changing
* colors.
* @param {number} [fps=60] - frame frequency.
*/
const changePixelColors = (video, colorMap, fps = 60) => {
const rectangle = video.getBoundingClientRect();
const width = Math.floor(rectangle.width),
height = Math.floor(rectangle.height);
const inputCanvas = document.createElement(`canvas`),
outputCanvas = document.createElement(`canvas`);
inputCanvas.width = outputCanvas.width = width;
inputCanvas.height = outputCanvas.height = height;
const inputContex = inputCanvas.getContext(`2d`),
outputContex = outputCanvas.getContext(`2d`);
video.style.display = inputCanvas.style.display = `none`;
video.crossOrigin = `anonymous`;
video.parentNode.insertBefore(outputCanvas, video);
video.parentNode.insertBefore(inputCanvas, video);
setInterval(() => {
if (video.paused || video.ended) {
return;
}
setFrame();
}, Math.round(1000 / fps));
/**
* Set one changed frame from video to output canvas.
*/
const setFrame = () => {
inputContex.drawImage(video, 0, 0, width, height);
const frame = inputContex.getImageData(0, 0, width, height),
data = frame.data,
len = data.length;
for (let i = 0; i < len; i += 4) {
[data[i], data[i + 1], data[i + 2], data[i + 3]] =
colorMap(data[i], data[i + 1], data[i + 2], data[i + 3]);
}
outputContex.putImageData(frame, 0, 0);
};
/**
* If video paused, set first frame anyway.
*/
setFrame();
};
}