所以基本上我试图将WebGL帧/屏幕截图发送到服务器,这可能会将这些帧保存到硬盘驱动器上,这样我就可以将这些保存的帧合并到视频文件中。 我发现了这个: Exporting video from webgl
我不想用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
接收方服务器端代码由Joe Turner给出(这是在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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YFlrYUHaAY1poYUPZAY5poYUNZQc4poUWNpQd4JgWWthQdoBjWmhhQ9kBjmmhhQ1lBzimhRY2lB3gmBZa2FB2gGNaaGFD2QGOaaGFDWUHOKaFFjaUHeCYFlrYUHaAY1poYUPZAY5poYUNZQc4poUWNpQd4JgWWthQdoBjWmhhQ9kBjmmhhQ1lBzimhRY2lB3gmBZa2FB2gGNaaGFD2QGOaaGFDWUHOKaFFjaUHeCYFlrYUHaAY1poYUPZAY5poYUNZQc4poUWNpQd4JgWWthQdoBjWmhhQ9kBjmmhhQ1lBzimhRY2lB3gmBZa2FB2gGNaaGFD2QGOaaGFxTXyfyGJSYm1ZIJcAAAAAElFTkSuQmCC";
var frame = 10;
$.ajax({
url: "http://127.0.0.1:8080",
type: "POST",
data: {"data": dataUrl, "frame": frame}
});
但仍无法看到任何已保存的图像文件。
答案 0 :(得分:1)
您编写的代码只需要在其网址中包含正确的协议(您错过了“ http:// ”):
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}
});
从您发布的代码中,您可以看到服务器已在解析请求并提取您在POST中发送的两个参数
if (request.content.trim()) {
request.content = querystring.parse(request.content);
var data = request.content['data'];
var frame = request.content['frame'];
//...
在调用
时,可以通过名称检索您在$ .ajax中添加到POST请求的任何内容request.content['name']
只需确保您在客户端和服务器代码中使用的名称匹配。
此外,node.js代码正在接收转义的base64字符串,该字符串不会使此函数正常工作
data.substr(data.indexOf(',') + 1);
由于','字符被转义。
要解决此问题,只需在数据字符串上使用节点内置函数 unescape ,就像这样
var data = unescape(request.content['data']);