From 6131385a4e87e7160450ae6b3a18345cf6a7c87a Mon Sep 17 00:00:00 2001 From: Eric Rowell Date: Sat, 14 Sep 2013 00:56:17 -0700 Subject: [PATCH] refactored setLineDash a bit, added comments, and added a context trace assertion that tests line dash --- src/Context.js | 42 +++++++++++++++++++++++++++++------------ test/unit/Layer-test.js | 9 +-------- test/unit/Shape-test.js | 4 ++++ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Context.js b/src/Context.js index a6064890..7bade281 100644 --- a/src/Context.js +++ b/src/Context.js @@ -2,6 +2,8 @@ var COMMA = ',', OPEN_PAREN = '(', CLOSE_PAREN = ')', + OPEN_PAREN_BRACKET = '([', + CLOSE_BRACKET_PAREN = '])', EMPTY_STRING = '', EQUALS = '=', SET = 'set', @@ -29,6 +31,7 @@ 'rotate', 'save', 'scale', + 'setLineDash', 'setTransform', 'stroke', 'strokeText', @@ -291,6 +294,25 @@ var a = arguments; this._context.scale(a[0], a[1]); }, + setLineDash: function() { + var a = arguments, + _context = this._context; + + // works for Chrome and IE11 + if(this._context.setLineDash) { + _context.setLineDash(a[0]); + } + // verified that this works in firefox + else if('mozDash' in _context) { + _context.mozDash = a[0]; + } + // does not currently work for Safari + else if('webkitLineDash' in _context) { + _context.webkitLineDash = a[0]; + } + + // no support for IE9 and IE10 + }, setTransform: function() { var a = arguments; this._context.setTransform(a[0], a[1], a[2], a[3], a[4], a[5]); @@ -326,7 +348,12 @@ that[methodName] = function() { args = _roundArrValues(Array.prototype.slice.call(arguments, 0)); ret = origMethod.apply(that, arguments); - that._trace(methodName + OPEN_PAREN + args.join(COMMA) + CLOSE_PAREN); + if (Kinetic.Util._isArray(args[0])) { + that._trace(methodName + OPEN_PAREN_BRACKET + args.join(COMMA) + CLOSE_BRACKET_PAREN); + } + else { + that._trace(methodName + OPEN_PAREN + args.join(COMMA) + CLOSE_PAREN); + } return ret; }; })(CONTEXT_METHODS[n]); @@ -451,8 +478,7 @@ } }, _stroke: function(shape, skipShadow) { - var _context = this._context, - stroke = shape.getStroke(), + var stroke = shape.getStroke(), strokeWidth = shape.getStrokeWidth(), dashArray = shape.getDashArray(); @@ -463,15 +489,7 @@ } this._applyLineCap(shape); if(dashArray && shape.getDashArrayEnabled()) { - if(_context.setLineDash) { - _context.setLineDash(dashArray); - } - else if('mozDash' in _context) { - _context.mozDash = dashArray; - } - else if('webkitLineDash' in _context) { - _context.webkitLineDash = dashArray; - } + this.setLineDash(dashArray); } if(!skipShadow && shape.hasShadow()) { this._applyShadow(shape); diff --git a/test/unit/Layer-test.js b/test/unit/Layer-test.js index 97ee89df..bb211cf9 100644 --- a/test/unit/Layer-test.js +++ b/test/unit/Layer-test.js @@ -21,14 +21,7 @@ suite('Layer', function() { var style = layer.getCanvas()._canvas.style; assert.equal(style.position, 'absolute', 'canvas position style should be absolute'); - - if (Kinetic.UA.browser === 'mozilla') { - assert.equal(style.border, '0px none', 'canvas border style should be 0px'); - } - else { - assert.equal(style.border, '0px', 'canvas border style should be 0px'); - } - + assert.equal(style.border.indexOf('0px'), 0, 'canvas border style should be 0px'); assert.equal(style.margin, '0px', 'canvas margin style should be 0px'); assert.equal(style.padding, '0px', 'canvas padding style should be 0px'); assert.equal(style.backgroundColor, 'transparent', 'canvas backgroundColor style should be transparent'); diff --git a/test/unit/Shape-test.js b/test/unit/Shape-test.js index 642f78d4..9d83f8ab 100644 --- a/test/unit/Shape-test.js +++ b/test/unit/Shape-test.js @@ -407,6 +407,10 @@ suite('Shape-test', function() { assert.equal(circle.getShadowEnabled(), true, 'shadowEnabled should be true'); assert.equal(circle.getDashArrayEnabled(), true, 'dashArrayEnabled should be true'); + var trace = layer.getContext().getTrace(); + //console.log(trace); + assert.equal(trace, 'clearRect(0,0,578,200);save();transform(1,0,0,1,289,100);beginPath();arc(0,0,70,0,6.283,false);closePath();save();shadowColor=black;shadowBlur=10;shadowOffsetX=10;shadowOffsetY=10;fillStyle=green;fill();restore();fillStyle=green;fill();setLineDash([10,10]);lineWidth=4;strokeStyle=black;stroke();restore()'); + });