Permalink
Browse files

Remove require from main layout file.

This also includes a backwards-incompatible change to module naming when
used with Node.js: instead of require("d3-cloud").cloud, you should now
use require("d3-cloud") directly.

Fixes #28.
  • Loading branch information...
1 parent 7af5e1d commit a81e468c685f7f32fbd55b578e3ae88b6a0611b6 @jasondavies committed Jan 10, 2014
Showing with 20 additions and 8 deletions.
  1. +5 −5 d3.layout.cloud.js
  2. +12 −0 index.js
  3. +3 −3 package.json
View
@@ -1,6 +1,6 @@
// Word cloud layout by Jason Davies, http://www.jasondavies.com/word-cloud/
// Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf
-(function(exports) {
+(function() {
function cloud() {
var size = [256, 256],
text = cloudText,
@@ -384,8 +384,7 @@
canvas.width = (cw << 5) / ratio;
canvas.height = ch / ratio;
} else {
- // node-canvas support
- var Canvas = require("canvas");
+ // Attempt to use node-canvas.
canvas = new Canvas(cw << 5, ch);
}
@@ -397,5 +396,6 @@
c.fillStyle = c.strokeStyle = "red";
c.textAlign = "center";
- exports.cloud = cloud;
-})(typeof exports === "undefined" ? d3.layout || (d3.layout = {}) : exports);
+ if (typeof module === "object" && module.exports) module.exports = cloud;
+ else (d3.layout || (d3.layout = {})).cloud = cloud;
+})();
View
@@ -0,0 +1,12 @@
+var Canvas = require("canvas"),
+ globals = {};
+
+// stash globals
+if ("Canvas" in global) globals.Canvas = global.Canvas;
+global.Canvas = Canvas;
+
+module.exports = require("./d3.layout.cloud");
+
+// restore globals
+if ("Canvas" in globals) global.Canvas = globals.Canvas;
+else delete global.Canvas;
View
@@ -1,6 +1,6 @@
{
"name": "d3.layout.cloud",
- "version": "1.0.2",
+ "version": "1.0.5",
"description": "Generate word clouds in JavaScript.",
"keywords": [
"word",
@@ -18,9 +18,9 @@
"type": "git",
"url": "http://github.com/jasondavies/d3-cloud.git"
},
- "main": "d3.layout.cloud.js",
+ "main": "index.js",
"dependencies": {
- "canvas": "0.10.x"
+ "canvas": "1.1.x"
},
"devDependencies": {
"uglify-js": "1.2.x"

0 comments on commit a81e468

Please sign in to comment.