Skip to content

Commit

Permalink
Ignore any fully invisible strokeStyle and fillStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
em committed Jun 23, 2014
1 parent 63ced3a commit 3fc69ac
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
19 changes: 17 additions & 2 deletions lib/gcanvas.js
Expand Up @@ -32,14 +32,15 @@ function GCanvas(driver, width, height) {
this.top = 0;
this.retract = 0;
this.align = 'center';
this.mode = 'mill';
this.driver = driver || new GcodeDriver();
this.driver.src = this;
this.stack = [];
this.motion = new Motion(this);
this.filters = [];
this.precision = 20;
this.ramping = true;
this.strokeStyle = '#000000';
this.fillStyle = '#000000';

if(this.driver.init)
this.driver.init();
Expand All @@ -58,7 +59,6 @@ GCanvas.prototype = {
depthOfCut: this.depthOfCut,
toolDiameter: this.toolDiameter,
align: this.align,
mode: this.mode,
top: this.top,
filters: this.filters.slice()
});
Expand Down Expand Up @@ -328,7 +328,20 @@ GCanvas.prototype = {

return b;
}
, _isOpaque: function(color) {
if(color == 'transparent') return false;
if(color == 'none') return false;

if(typeof color == 'string' &&
color.match(/rgba\((?:.*,){3}[0\.]*\)/)) {
return false;
}

return true;
}
, stroke: function(align, depth) {
if(!this._isOpaque(this.strokeStyle)) return;

this.save();

if(typeof align === 'number') {
Expand Down Expand Up @@ -374,6 +387,8 @@ GCanvas.prototype = {
this.restore();
}
, fill: function(windingRule, depth) {
if(!this._isOpaque(this.fillStyle)) return;

this.save();

if(typeof windingRule === 'number') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gcanvas",
"version": "0.0.24",
"version": "0.0.25",
"description": "A Canvas API implementation that generates Gcode",
"keywords": [
"canvas",
Expand Down
47 changes: 47 additions & 0 deletions test/test.gcanvas.js
Expand Up @@ -272,5 +272,52 @@ describe('GCanvas', function() {
});

describe('#fill', function() {
it('offsets outward from center', function() {
ctx.rect(0,0,10,10);
ctx.toolDiameter = 8;
ctx.fill();

hand.rapid({x:6,y:6});
hand.linear({x:6, y:4, z:0});
hand.linear({x:4, y:4, z:0});
hand.linear({x:4, y:6, z:0});
hand.linear({x:6, y:6, z:0});

expect(robot.result).eql(hand.result);
});

it('ignores 0 alpha fillStyle', function() {
ctx.rect(0,0,10,10);
ctx.toolDiameter = 8;
ctx.fillStyle='rgba(0,0,0,0)'
ctx.fill();

expect(robot.result).eql(hand.result);
});
});


describe('#stroke', function() {
it('offsets outward from center', function() {
ctx.rect(0,0,10,10);
ctx.toolDiameter = 8;
ctx.stroke();

hand.linear({x:10, y:0, z:0});
hand.linear({x:10, y:10, z:0});
hand.linear({x:0, y:10, z:0});
hand.linear({x:0, y:0, z:0});

expect(robot.result).eql(hand.result);
});

it('ignores 0 alpha strokeStyle', function() {
ctx.rect(0,0,10,10);
ctx.toolDiameter = 8;
ctx.strokeStyle='rgba(0,0,0,0)'
ctx.stroke();

expect(robot.result).eql(hand.result);
});
});
});

0 comments on commit 3fc69ac

Please sign in to comment.