Given a jsdom
based svgcreator.node.js
脚本文件:
var jsdom = require('jsdom');
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK
[ 'http://d3js.org/d3.v3.min.js', // JS DEPENDENCIES online ...
'js/d3.v3.min.js' ], // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
function (err, window) {
var svg = window.d3.select("body")
.append("svg")
.attr("width", 100)
.attr("height", 100);
svg.append("rect")
.attr("id", "rect1")
.attr("x", 10)
.attr("y", 10)
.attr("width", 80)
.attr("height", 80)
.style("fill", "green");
// END svg design
//PRINTING OUT SELECTION
console.log(window.d3.select("body").html());
}
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);
鉴于我使用 NodeJS 终端命令来运行它并生成output.svg
file :
node svgcreator.node.js > output.svg # nodeJS + script command
如何将参数值从终端传递到 NodeJS ?
测试的依赖关系:
-
svgcreator.node.js github 存储库 https://github.com/hugolpz/svgcreator.node.js:
git clone '[email protected] /cdn-cgi/l/email-protection:hugolpz/svgcreator.node.js.git'
- 需要 jsdom,使用:
sudo npm install -g jsdom
(全球的)。
使用的解决方案(@Matt_Harrison):我们依靠process.env.myVar
svgcreator.node.js
JS代码:
var jsdom = require('jsdom');
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ 'http://d3js.org/d3.v3.min.js', // JS DEPENDENCIES online ...
'js/d3.v3.min.js' ], // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
function (err, window) {
var color = process.env.COLOR; // <<################# IMPORTANT !!
var svg = window.d3.select("body")
.append("svg")
.attr("width", 100)
.attr("height", 100);
svg.append("rect")
.attr("id", "rect1")
.attr("x", 10)
.attr("y", 10)
.attr("width", 80)
.attr("height", 80)
.style("fill", color); // <<################# IMPORTANT !!
// END svg design
//PRINTING OUT SELECTION
console.log(window.d3.select("body").html());
}
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);
终端 NodeJS 命令:
COLOR=#66AAFF node svgcreator.node.js > out.svg # <<############# IMPORTANT !! setting the value.
+1 @Matt_Harrison 答案和问题表示赞赏!