所以基本上我試圖發送WebGL幀/屏幕截圖到一個服務器,可以將這些幀保存到硬盤驅動器,這樣我就可以將這些保存的幀合併到視頻文件中。 我發現這一點: Exporting video from webgl如何編寫包含兩段POST數據的AJAX請求?
我不喜歡打擾你們用WebGL的相關細節......你應該知道,我想送兩個客戶端的內容唯一的JavaScript變量localhost node.js服務器。所以,這兩個客戶端變量如下:
var frame = frame + 1; //this is the frame counter
var dataUrl = renderer.domElement.toDataURL("image/png"); //this is the encoded screenshot/frame
接收服務器端代碼被喬特納給出的(這在node.js中運行)。 所以我的問題是:如何將上面提到的兩個變量的內容發送到這個node.js代碼?
var http = require('http');
var querystring = require('querystring');
var fs = require('fs');
// Override so we don't decode spaces, and mess up the base64 encoding
querystring.unescape = function(s, decodeSpaces) {
return s;
};
// Pad to follow the processing export format
function pad(num) {
var s = "000" + num;
return s.substr(s.length-4);
}
http.createServer(function (request, response) {
request.content = '';
request.addListener("data", function(data) {
request.content += data;
});
request.addListener("end", function() {
if (request.content.trim()) {
request.content = querystring.parse(request.content);
var data = unescape(request.content['data']);
var frame = request.content['frame'];
// Remove data:image/png;base64,
data = data.substr(data.indexOf(',') + 1);
var buffer = new Buffer(data, 'base64');
fs.writeFile('screen-' + pad(frame) + '.png',
buffer.toString('binary'), 'binary');
}
});
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'X-Requested-With'
});
response.end();
}).listen(8080, "127.0.0.1");
我應該這樣做嗎?
...
...
//WebGL related code
...
...
var frame = frame + 1;
var dataUrl = renderer.domElement.toDataURL("image/png");
$.ajax({
url: "http://127.0.0.1:8080",
type: "POST",
data: {"data": dataUrl, "frame": frame}
});
...
...
例如,讓我們看看這個實驗:
pointLight.position.copy(camera.position);
progress += frameTime;
lastTimestamp = timestamp;
renderer.render(scene, camera);
stats.update();
requestAnimationFrame(animate);
向該:
pointLight.position.copy(camera.position);
progress += frameTime;
lastTimestamp = timestamp;
renderer.render(scene, camera);
stats.update();
requestAnimationFrame(animate);
var dataUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAATsAAAC+CAYAAABK1qgYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAA08SURBVHhe7d1vaF31Hcfx7oF74PZkWB0yNermv1n/VMbYH13BsQ18sD0YIj4yQnEwJxUKCxQhCBaHe9CWMDrxQScoSpHGItgH6WYoYhXaSoO0Li6k2JrcQhs10Zvc/Pvu/M6fe09uvrm599vc3m887wMvbc795eTo4fPp73fOTbJBVti++OKLS3Lq1CmZm5tTDQ4OylUbN8rGq6+Wq6+5BgDajrIDUAiUHYBCoOwAFIKp7Lq7u9X9eZQdAE9aLrtQdBnt9QxlB8CTlsouX3SrFR5lB8CTpsuuvuDqP65H2QHwpOWyW21fhrID4ElLZVe/r9F+yg6AJy0/oGgWZQfAE8oOQCFQdgAKgbIDUAiUHYBCWLHsjh8/bkbZAfCGsgNQCJQdgELoWNmdeWKDXPzLBpnctkG+enqDfF0wY3/+lhx+9Ap57tffkV/d+j314rTiB11dcvu998l992+RX/z2IXngod+jw8J1CNcjXJdwfbTrhsuHsnMgFN9fH/iueoGaccumu9WwwY9QfOE6adcPlwdl58iBP35bvUiN3POzX6rhgk/hemnXEe1H2TnT08IMjxnd+sQMrzMoO2fCkraZe3jhHpAWJPgXlrTcw7v8KDuHnnvwyvhJdSO33bNZDRLWh3D9tECifSg7h8JTWq3g8jbfv0UNEdaHcP20QKJ9KDuHwlJWK7g83l6yvoXrpwUS7UPZOaUVXJ4WIKwvWiDRPh0ru7H+P8hEruym3z0qi7PpF4+2xfJRmd0Vgr9PFtJ99dvCB8tLojq+/I5Uqvuel/no2AuD+XGPyPyX0b6hLbl9qQPvyKKMylxf3f5U+eVdMl/6PBqTbdGfz70mlRfC662er25j+A6TBrTwrOiZg3KiVEnPQmSyNCT7/7ZVH9vA431HZHhiKj1KtE2V5ET/C/Jwbsz2/iEZyw2Z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GOaaGFDWUHOKaFFjaUHeCYFlrYUHaAY1poYUPZAY5poYUNZQc4poUWNpQd4JgWWthQdoBjWmhhQ9kBjmmhhQ1lBzimhRY2lB3gmBZa2FB2gGNaaGFD2QGOaaGFDWUHOKaFFjaUHeCYFlrYUHaAY1poYUPZAY5poYUNZQc4poUWNpQd4JgWWthQdoBjWmhhQ9kBjmmhhQ1lBzimhRY2lB3gmBZa2FB2gGNaaGFD2QGOaaGFDWUHOKaFFjaUHeCYFlrYUHaAY1poYUPZAY5poYUNZQc4poUWNpQd4JgWWthQdoBjWmhhQ9kBjmmhhQ1lBzimhRY2lB3gmBZa2FB2gGNaaGFD2QGOaaGFxTXyfyGJSYm1ZIJcAAAAAElFTkSuQmCC";
var frame = 10;
$.ajax({
url: "http://127.0.0.1:8080",
type: "POST",
data: {"data": dataUrl, "frame": frame}
});
我在上述實驗中改變這一段代碼
但仍看不到任何保存的圖像文件。
[通行證陣列以在$就()AJAX請求]的可能重複(HTTP:/ /stackoverflow.com/questions/8890524/pass-array-to-ajax-request-in-ajax) – bwright