Browse files

Implement closePath fully

  • Loading branch information...
1 parent d0d8ea7 commit 547c8663859f51e2ce51f7f4229b78a79dc74d7b @em committed Oct 9, 2013
Showing with 40 additions and 32 deletions.
  1. +19 −15 build/gcanvas.js
  2. +1 −1 component.json
  3. +8 −2 lib/gcanvas.js
  4. +7 −0 lib/path.js
  5. +4 −13 lib/subpath.js
  6. +1 −1 package.json
View
34 build/gcanvas.js
@@ -229,7 +229,6 @@ function GCanvas(driver, width, height) {\n\
this.depthOfCut = 0;\n\
this.top = 0;\n\
this.aboveTop = 0;\n\
- this.toolDiameter = 5;\n\
this.strokeAlign = 'center';\n\
this.driver = driver || new GcodeDriver();\n\
this.stack = [];\n\
@@ -398,6 +397,11 @@ GCanvas.prototype = {\n\
});\n\
}\n\
, fill: function(windingRule) {\n\
+\n\
+ if(!this.toolDiameter) {\n\
+ throw 'You must set context.toolDiameter to use fill()'\n\
+ }\n\
+\n\
var path = this.path;\n\
path = path.simplify(windingRule);\n\
path = path.clip(this.clipRegion);\n\
@@ -481,7 +485,9 @@ GCanvas.prototype = {\n\
});\n\
}\n\
, clearRect: function() {}\n\
-, closePath: function() {}\n\
+, closePath: function() {\n\
+ this.path.close();\n\
+ }\n\
};\n\
\n\
GCanvas.Filter = require('./drivers/filter');\n\
@@ -936,6 +942,13 @@ Path.prototype = {\n\
this.subPaths.push(subPath);\n\
this.current = subPath;\n\
}\n\
+, close: function() {\n\
+ if(this.current) {\n\
+ this.current.closed = true;\n\
+ var curStart = this.current.actions[0].args;\n\
+ this.moveTo.apply(this, curStart);\n\
+ }\n\
+ }\n\
\n\
/*\n\
* Pass all curves straight through\n\
@@ -1138,7 +1151,7 @@ SubPath.prototype = {\n\
this.actions.push( { action: SubPath.actions.ELLIPSE, args: arguments } );\n\
}\n\
\n\
-, getPoints: function( divisions, closedSubPath ) {\n\
+, getPoints: function( divisions ) {\n\
divisions = divisions || 12;\n\
\n\
var points = [];\n\
@@ -1294,20 +1307,11 @@ SubPath.prototype = {\n\
break;\n\
\n\
} // end switch\n\
-\n\
}\n\
\n\
-\t// Normalize to remove the closing point by default.\n\
-\t// var lastPoint = points[ points.length - 1];\n\
-\t// var EPSILON = 0.0000000001;\n\
-\t// if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&\n\
-\t// \t\t Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)\n\
-\t// \tpoints.splice( points.length - 1, 1);\n\
-\t// if ( closedSubPath ) {\n\
-\n\
-\t// \tpoints.push( points[ 0 ] );\n\
-\n\
-\t// }\n\
+ if(this.closed) {\n\
+ points.push( points[ 0 ] );\n\
+ }\n\
\n\
return points;\n\
}\n\
View
2 component.json
@@ -2,7 +2,7 @@
"name": "gcanvas",
"repo": "em/gcanvas",
"description": "",
- "version": "0.0.15",
+ "version": "0.0.16",
"keywords": [],
"dependencies": {
},
View
10 lib/gcanvas.js
@@ -26,7 +26,6 @@ function GCanvas(driver, width, height) {
this.depthOfCut = 0;
this.top = 0;
this.aboveTop = 0;
- this.toolDiameter = 5;
this.strokeAlign = 'center';
this.driver = driver || new GcodeDriver();
this.stack = [];
@@ -195,6 +194,11 @@ GCanvas.prototype = {
});
}
, fill: function(windingRule) {
+
+ if(!this.toolDiameter) {
+ throw 'You must set context.toolDiameter to use fill()'
+ }
+
var path = this.path;
path = path.simplify(windingRule);
path = path.clip(this.clipRegion);
@@ -278,7 +282,9 @@ GCanvas.prototype = {
});
}
, clearRect: function() {}
-, closePath: function() {}
+, closePath: function() {
+ this.path.close();
+ }
};
GCanvas.Filter = require('./drivers/filter');
View
7 lib/path.js
@@ -16,6 +16,13 @@ Path.prototype = {
this.subPaths.push(subPath);
this.current = subPath;
}
+, close: function() {
+ if(this.current) {
+ this.current.closed = true;
+ var curStart = this.current.actions[0].args;
+ this.moveTo.apply(this, curStart);
+ }
+ }
/*
* Pass all curves straight through
View
17 lib/subpath.js
@@ -85,7 +85,7 @@ SubPath.prototype = {
this.actions.push( { action: SubPath.actions.ELLIPSE, args: arguments } );
}
-, getPoints: function( divisions, closedSubPath ) {
+, getPoints: function( divisions ) {
divisions = divisions || 12;
var points = [];
@@ -241,20 +241,11 @@ SubPath.prototype = {
break;
} // end switch
-
}
- // Normalize to remove the closing point by default.
- // var lastPoint = points[ points.length - 1];
- // var EPSILON = 0.0000000001;
- // if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&
- // Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)
- // points.splice( points.length - 1, 1);
- // if ( closedSubPath ) {
-
- // points.push( points[ 0 ] );
-
- // }
+ if(this.closed) {
+ points.push( points[ 0 ] );
+ }
return points;
}
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "gcanvas",
- "version": "0.0.15",
+ "version": "0.0.16",
"description": "A Canvas API implementation that generates Gcode",
"keywords": [
"canvas",

0 comments on commit 547c866

Please sign in to comment.