-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
174 lines (160 loc) · 4.5 KB
/
index.html
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
<html>
<title>N-API Demo</title>
<body>
<h1>Cascadia JS 2018</h1>
<h2>N API Souvenir Service</h2>
<div id="content">
<p>Click here above to start video, click again to get a souvenir. Then click to reset.</p>
<img alt="Embedded Image" id="image" src="" />
<video id="video" crossorigin="anonymous" muted playsinline></video>
<canvas id="canvas" width="1024" height="768"></canvas>
</div>
<a download="CascadiaJS 2018 Souvenir.jpg"></a>
</body>
<script>
// Wait for the document to be ready.
window.addEventListener("DOMContentLoaded", () => {
// Get all dom elements of the demo
const canvas = document.getElementById("canvas");
const video = document.getElementById("video");
const image = document.getElementById("image");
const content = document.getElementById("content");
const anchor = document.querySelector("a");
let videoStream = null;
let stage = 0;
let ignoreClicks = false;
// Click handler
const cb = async () => {
if (ignoreClicks) return;
document.querySelector("p").style.display = "none";
switch (stage) {
case 0:
navigator.mediaDevices.getUserMedia({
video: true
}).then(stream => {
videoStream = stream;
video.srcObject = stream;
video.onloadedmetadata = function (e) {
video.play().catch(e => alert(e));
image.style.display = "none";
video.style.display = "block";
}
}, (e) => alert(e));
break;
case 1:
const width = video.videoWidth;
const height = video.videoHeight;
canvas.width = width;
canvas.height = height;
video.pause();
const context = canvas.getContext("2d");
context.drawImage(video, 0, 0, canvas.width, canvas.height);
videoStream.getTracks()[0].stop()
const imageData = canvas.toDataURL('image/jpeg', 0.85);
image.src = imageData;
image.style.display = "block";
video.style.display = "none";
let data = "";
// Electron
if (window.process && window.process.type) {
const fs = require('fs');
const nativeImage = require('electron').nativeImage;
const photo = nativeImage.createFromDataURL(imageData).toJPEG(85);
const editor = require("./build/Release/Edit.node");
await new Promise((resolve) => {
editor.edit(photo, (err, processed) => {
data = "data:image/jpeg;base64," + processed.toString('base64');
resolve();
});
});
} else {
// Browser
const resp = await fetch("/", {
method: 'POST',
body: imageData
});
data = await resp.text();
}
image.src = data;
image.style.display = "block";
video.style.display = "none";
anchor.href = image.src;
setTimeout(() => {
ignoreClicks = true;
anchor.click();
ignoreClicks = false;
}, 100);
stage = -1;
break;
}
stage++;
};
if (!('ontouchstart' in window)) {
document.body.addEventListener("click", cb);
} else {
document.body.addEventListener("touchstart", cb);
}
/*
* Utilities
*/
// Handling sizes better.
function handleResize() {
const width = window.innerWidth;
const height = window.innerHeight - content.offsetTop;
content.style.width = width;
content.style.height = height;
}
window.onresize = handleResize;
handleResize();
});
</script>
<style>
/* Minimum basic styling */
html,
body {
background: #222;
margin: 0;
padding: 0;
}
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
h1,
h2,
p {
text-align: center;
color: #eee;
margin: 5px;
}
#content {
width: 1024px;
height: 768px;
position: relative;
margin: 0 auto;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
#canvas,
#image {
display: none;
max-width: 100%;
}
#video,
#canvas,
#image {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: contain;
}
a {
display: none;
}
</style>
</html>