-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
397 lines (326 loc) · 9.26 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
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
<html>
<head>
<title>Displacement map editor</title>
<link rel="icon" href="images/DMapFavicon.ico"/>
<style type="text/css">
html, body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: BDBDAE;
overflow: hidden;
}
</style>
<!-- js script for downloadify-->
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/downloadify.min.js"></script>
<!-- js script for Native Client -->
<script type="text/javascript">
NativeClientModule = null; // Global application object.
statusText = 'NO-STATUS';
// Handle a message coming from the NaCl module.
function handleMessage(message_event)
{
if(typeof message_event.data == "string")
{
if( message_event.data.charAt(0) == 'D' &&
message_event.data.charAt(1) == 'B' &&
message_event.data.charAt(2) == 'G'&&
message_event.data.charAt(3) == ':' &&
message_event.data.charAt(4) == ' ')
{
console.log(message_event.data);
}
}
}
// Indicate success when the NaCl module has loaded.
function moduleDidLoad()
{
NativeClientModule = document.getElementById('nacl');
updateStatus('SUCCESS');
}
// If the page loads before the Native Client module loads, then set the
// status message indicating that the module is still loading. Otherwise,
// do not change the status message.
function pageDidLoad()
{
if (NativeClientModule == null)
{
updateStatus('LOADING...');
}
else
{
// It's possible that the Native Client module onload event fired
// before the page's onload event. In this case, the status message
// will reflect 'SUCCESS', but won't be displayed. This call will
// display the current message.
updateStatus();
}
}
// Set the global status message. If the element with id 'statusField'
// exists, then set its HTML to the status message as well.
// opt_message The message test. If this is null or undefined, then
// attempt to set the element with id 'statusField' to the value of
// |statusText|.
function updateStatus(opt_message)
{
if (opt_message)
{
statusText = opt_message;
}
var statusField = document.getElementById('statusField');
if (statusField)
{
statusField.innerHTML = statusText;
if(statusText == 'SUCCESS')
{
statusField.style.visibility="hidden";
statusField.style.display="none";
}
}
}
</script>
</head>
<body onload="init()" scroll="no" >
<table border="0" scroll="no" width="100%" height="100%" align="center">
<tr scroll="no">
<td id="listener" width="50%" height="50%" align="center" >
<script type="text/javascript">
var listener = document.getElementById('listener')
listener.addEventListener('load', moduleDidLoad, true);
listener.addEventListener('message', handleMessage, true);
</script>
<h2><code id="statusField">NO-STATUS</code></h2>
<embed name="nacl_module"
id="nacl"
width="100%" height="80%"
src="nacl.nmf"
type="application/x-nacl" />
</td>
<td align="center">
<br/>
<br/>
<canvas id="myCanvas" width="256" height="256" style="border:0px solid #c3c3c3;" onmousedown="canvasMouseDown()" onmouseup="canvasMouseUp()" onmousemove="canvasMouseMove()">
Your browser does not support the canvas element.
</canvas>
<br>
<br>
Global height <input type="range" id="baseHeightSlider" min="0" max="255" value="0" step="5" onchange="baseHeightChanged(this.value)" />
<span id="range">0</span>
<br>
<br>
Brush height <input type="range" id="brushHeightSlider" min="0" max="255" value="0" step="5" onchange="brushHeightChanged(this.value)" />
<span id="brushRange">0</span>
<br>
<br>
Brush size <input type="range" id="brushSizeSlider" min="0" max="10" value="0" step="1" onchange="brushSizeChanged(this.value)" />
<span id="brushSizeRange">0</span>
<br>
<br>
Displacment factor <input type="range" id="dispCoeffSlider" min="0" max="1" value="0.5" step="0.1" onchange="sendDispCoeffToNativeClient(this.value)" />
<span id="dispCoeff">0.5</span>
<br>
<br>
<form>
<input type="checkbox" name="highlight" onchange="highlightChanged(this.checked)"/>Highlight displaced pixels<br />
</form>
<br>
<form>
<p id="downloadify">
You must have Flash 10 installed to download this file.
</p>
</form>
<script type="text/javascript">
function InitImageSaving()
{
Downloadify.create('downloadify',
{
filename: function()
{
return "displacementmap.png"
},
data: function()
{
var dataURL = gCanvas.toDataURL();
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
},
onComplete: function(){ },
onCancel: function(){ },
onError: function(){ alert('Failed to save file'); },
swf: 'media/downloadify.swf',
downloadImage: 'images/download.png',
width: 100,
height: 30,
transparent: true,
append: false,
dataType: "base64"
});
}
</script>
Load displacement map: <input type="file" id="DMapFileSelect" name="DMapFileSelect[]" accept="image/png" onchange="loadDMap"></input>
</td>
</tr>
</table>
<!-- js script for the html5 canvas drawing -->
<script type="text/javascript">
var BaseHeight = 0;
var BrushHeight = 0xA0;
var brushSize = 4;
var gCanvas=document.getElementById("myCanvas");
var ctx=gCanvas.getContext("2d");
var ctrlKeyDown = false;
var mouseDown = false;
function init()
{
pageDidLoad();
document.getElementById("baseHeightSlider").value = BaseHeight;
document.getElementById("brushHeightSlider").value = BrushHeight;
document.getElementById("brushSizeSlider").value = brushSize;
baseHeightChanged(BaseHeight);
brushHeightChanged(BrushHeight);
brushSizeChanged(brushSize);
InitImageSaving();
}
function baseHeightChanged(newValue)
{
document.getElementById("range").innerHTML=newValue;
BaseHeight = newValue;
ctx.fillStyle= "rgb(" + BaseHeight + ", " + BaseHeight + ", " + BaseHeight + ");";
ctx.fillRect(0,0,256,256);
sendImageToNativeClient();
}
function brushHeightChanged(newValue)
{
document.getElementById("brushRange").innerHTML=newValue;
BrushHeight = newValue;
}
function brushSizeChanged(newValue)
{
document.getElementById("brushSizeRange").innerHTML=newValue;
brushSize = newValue;
}
function sendDispCoeffToNativeClient(val)
{
var fval = parseFloat(val)
document.getElementById("dispCoeff").innerHTML=fval.toFixed(4)
if(NativeClientModule != null)
{
var msg = "DCOEF" + val.toString();
NativeClientModule.postMessage(msg);
}
}
function sendImageToNativeClient()
{
if(NativeClientModule != null)
{
var imgData=ctx.getImageData(0,0,256,256);
var msg = 'TEXTU';
var data = imgData.data;
var len = data.length;
for (var i = 0; i < len; i += 4)
{
var pixelValue = data[i].toString();
msg = msg.concat(pixelValue.concat(' '));
}
NativeClientModule.postMessage(msg);
/*var dataURL = gCanvas.toDataURL("image/jpeg");
var msg = 'TEXTU' + atob(dataURL.replace(/^data:image\/(jpeg);base64,/, ""));
NativeClientModule.postMessage(msg);*/
/*var imageData = ctx.getImageData(0, 0, 256, 256);
var byteArray = imageData.data;
var stringValue = "TEXTU";
for (var i = 0; i < byteArray.length; i += 4)
{
stringValue += String.fromCharCode(byteArray[i]);
}
NativeClientModule.postMessage(stringValue);*/
}
}
resetLineStartPos = true;
function canvasApplyBrush(event)
{
event = event || window.event;
x = event.offsetX;
y = event.offsetY;
if(resetLineStartPos)
{
lastX = x;
lastY = y;
resetLineStartPos = false;
}
TotalHeight = BrushHeight;
ctx.strokeStyle="rgb(" + TotalHeight + ", " + TotalHeight + ", " + TotalHeight + ");";
ctx.lineWidth = brushSize;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(x, y);
ctx.stroke();
lastX = x;
lastY = y;
//sendImageToNativeClient();
}
function canvasMouseMove(event)
{
if(mouseDown)
{
canvasApplyBrush(event);
}
}
function canvasMouseDown(event)
{
mouseDown = true;
resetLineStartPos = true;
canvasApplyBrush(event);
}
function canvasMouseUp(event)
{
mouseDown = false;
resetLineStartPos = true;
canvasApplyBrush(event);
sendImageToNativeClient();
}
function highlightChanged(checked)
{
if(checked)
{
if(NativeClientModule != null)
{
NativeClientModule.postMessage("HIGH1");
}
}
else
{
if(NativeClientModule != null)
{
NativeClientModule.postMessage("HIGH0");
}
}
}
function DMapLoaded(e)
{
//Get the Base64 representation
var loadedimg = e.target.result;
var img = new Image();
img.onload = function()
{
ctx.drawImage(img,0,0);
sendImageToNativeClient();
}
img.src = loadedimg;
}
function loadDMap(evt)
{
var files = evt.target.files; //FileList object
var f = files[0];
if(f)
{
var reader = new FileReader();
reader.onload = DMapLoaded;
reader.readAsDataURL(f);
}
}
document.getElementById('DMapFileSelect').addEventListener('change', loadDMap, false);
</script>
</body>
</html>