diff --git a/src/Context.js b/src/Context.js index 74ce154a..4efbbd7d 100644 --- a/src/Context.js +++ b/src/Context.js @@ -9,6 +9,7 @@ 'arc', 'arcTo', 'beginPath', + 'bezierCurveTo', 'clearRect', 'closePath', 'fill', @@ -21,12 +22,8 @@ 'setTransform', 'stroke', 'strokeText', - 'transform' - ], - CONTEXT_PROPERTIES = [ - 'fillStyle', - 'lineWidth', - 'strokeStyle' + 'transform', + 'translate' ]; /** @@ -185,7 +182,7 @@ this.save(); this._applyAncestorTransforms(container); - _context.beginPath(); + this.beginPath(); this.rect(clipX, clipY, clipWidth, clipHeight); _context.clip(); this.reset(); @@ -193,15 +190,8 @@ this.restore(); }, - // context property setters - setFillStyle: function(val) { - this._context.fillStyle = val; - }, - setLineWidth: function(val) { - this._context.lineWidth = val; - }, - setStrokeStyle: function(val) { - this._context.strokeStyle = val; + setAttr: function(attr, val) { + this._context[attr] = val; }, // context pass through methods @@ -212,6 +202,10 @@ beginPath: function() { this._context.beginPath(); }, + bezierCurveTo: function() { + var a = arguments; + this._context.bezierCurveTo(a[0], a[1], a[2], a[3], a[4], a[5]); + }, clearRect: function() { var a = arguments; this._context.clearRect(a[0], a[1], a[2], a[3]); @@ -259,39 +253,34 @@ var a = arguments; this._context.transform(a[0], a[1], a[2], a[3], a[4], a[5]); }, + translate: function() { + var a = arguments; + this._context.translate(a[0], a[1]); + }, _enableTrace: function() { var that = this, len = CONTEXT_METHODS.length, _roundArrValues = Kinetic.Util._roundArrValues, - n; + origSetter = this.setAttr, + n, args; // methods for (n=0; n 2) { @@ -54,18 +53,18 @@ n = 0; while(n < len-1) { - _context.bezierCurveTo(ap[n].x, ap[n++].y, ap[n].x, ap[n++].y, ap[n].x, ap[n++].y); + context.bezierCurveTo(ap[n].x, ap[n++].y, ap[n].x, ap[n++].y, ap[n].x, ap[n++].y); } } // no tension else { for(n = 1; n < length; n++) { point = points[n]; - _context.lineTo(point.x, point.y); + context.lineTo(point.x, point.y); } } - _context.closePath(); + context.closePath(); context.fillStrokeShape(this); }, _setAllPoints: function() { diff --git a/src/shapes/Text.js b/src/shapes/Text.js index 3778e5c8..6a2f55c0 100644 --- a/src/shapes/Text.js +++ b/src/shapes/Text.js @@ -91,8 +91,7 @@ this._setTextData(); }, drawFunc: function(context) { - var _context = context._context, - p = this.getPadding(), + var p = this.getPadding(), fontStyle = this.getFontStyle(), fontSize = this.getFontSize(), fontFamily = this.getFontFamily(), @@ -102,12 +101,12 @@ textArrLen = textArr.length, totalWidth = this.getWidth(); - _context.font = this._getContextFont(); - _context.textBaseline = MIDDLE; - _context.textAlign = LEFT; - _context.save(); - _context.translate(p, 0); - _context.translate(0, p + textHeight / 2); + context.setAttr('font', this._getContextFont()); + context.setAttr('textBaseline', MIDDLE); + context.setAttr('textAlign', LEFT); + context.save(); + context.translate(p, 0); + context.translate(0, p + textHeight / 2); // draw text lines for(var n = 0; n < textArrLen; n++) { @@ -116,29 +115,28 @@ width = obj.width; // horizontal alignment - _context.save(); + context.save(); if(this.getAlign() === RIGHT) { - _context.translate(totalWidth - width - p * 2, 0); + context.translate(totalWidth - width - p * 2, 0); } else if(this.getAlign() === CENTER) { - _context.translate((totalWidth - width - p * 2) / 2, 0); + context.translate((totalWidth - width - p * 2) / 2, 0); } this.partialText = text; context.fillStrokeShape(this); - _context.restore(); - _context.translate(0, lineHeightPx); + context.restore(); + context.translate(0, lineHeightPx); } - _context.restore(); + context.restore(); }, drawHitFunc: function(context) { - var _context = context._context, - width = this.getWidth(), + var width = this.getWidth(), height = this.getHeight(); - _context.beginPath(); - _context.rect(0, 0, width, height); - _context.closePath(); + context.beginPath(); + context.rect(0, 0, width, height); + context.closePath(); context.fillStrokeShape(this); }, /** @@ -207,7 +205,7 @@ _getTextWidth: function (text) { return dummyContext.measureText(text).width; }, - _setTextData: function () { + _setTextData: function () { var lines = this.getText().split('\n'), fontSize = +this.getFontSize(), textWidth = 0, diff --git a/test/runner.html b/test/runner.html index 35b17877..78b5a3f1 100644 --- a/test/runner.html +++ b/test/runner.html @@ -1,4 +1,4 @@ - + KineticJS Mocha Tests @@ -28,6 +28,7 @@ + + + + + + diff --git a/test/unit/Circle-test.js b/test/unit/Circle-test.js index cfa074ac..dbbfbc69 100644 --- a/test/unit/Circle-test.js +++ b/test/unit/Circle-test.js @@ -42,7 +42,7 @@ suite('Circle', function(){ var trace = layer.getContext().getTrace(); //console.log(trace); - assert.equal(trace, 'clearRect(0,0,578,200);clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);beginPath();arc(0,0,70,0,6.283,false);closePath();fillStyle=green;fill();save();lineWidth=4;strokeStyle=black;stroke();restore();restore()'); + assert.equal(trace, 'clearRect(0,0,578,200);clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);beginPath();arc(0,0,70,0,6.283,false);closePath();save();fillStyle=green;fill();restore();save();lineWidth=4;strokeStyle=black;stroke();restore();restore()'); }); // ====================================================== diff --git a/test/unit/Rect-test.js b/test/unit/Rect-test.js index 0c1e2943..f82f9f60 100644 --- a/test/unit/Rect-test.js +++ b/test/unit/Rect-test.js @@ -27,13 +27,13 @@ suite('Rect', function(){ var trace = layer.getContext().getTrace(); //console.log(trace); - assert.equal(trace, 'clearRect(0,0,578,200);save();transform(1,0,0,1,100,50);beginPath();rect(0,0,100,50);closePath();fillStyle=green;fill();save();lineWidth=2;strokeStyle=blue;stroke();restore();restore()'); + assert.equal(trace, 'clearRect(0,0,578,200);save();transform(1,0,0,1,100,50);beginPath();rect(0,0,100,50);closePath();save();fillStyle=green;fill();restore();save();lineWidth=2;strokeStyle=blue;stroke();restore();restore()'); }); // ====================================================== - test('add rect to stage with shadow, rotation, corner radius, and opacity', function(){ + test('add rect with shadow, rotation, corner radius, and opacity', function(){ var stage = new Kinetic.Stage({ container: 'container', width: 578, @@ -70,7 +70,7 @@ suite('Rect', function(){ var trace = layer.getContext().getTrace(); //console.log(trace); - assert.equal(trace, 'clearRect(0,0,578,200);save();transform(1,0,0,1,100,50);beginPath();moveTo(5,0);lineTo(95,0);arc(95,5,5,4.712,0,false);lineTo(100,45);arc(95,45,5,0,1.571,false);lineTo(5,50);arc(5,45,5,1.571,3.142,false);lineTo(0,5);arc(5,5,5,3.142,4.712,false);closePath();fillStyle=green;fill();fillStyle=green;fill();save();lineWidth=2;strokeStyle=blue;stroke();restore();restore()'); + assert.equal(trace, 'clearRect(0,0,578,200);save();transform(1,0,0,1,100,50);beginPath();moveTo(5,0);lineTo(95,0);arc(95,5,5,4.712,0,false);lineTo(100,45);arc(95,45,5,0,1.571,false);lineTo(5,50);arc(5,45,5,1.571,3.142,false);lineTo(0,5);arc(5,5,5,3.142,4.712,false);closePath();save();fillStyle=green;fill();restore();save();fillStyle=green;fill();restore();save();lineWidth=2;strokeStyle=blue;stroke();restore();restore()'); }); diff --git a/tests/js/unit/shapes/textTests.js b/test/unit/Text-test.js similarity index 65% rename from tests/js/unit/shapes/textTests.js rename to test/unit/Text-test.js index 4aab776f..c6b08449 100644 --- a/tests/js/unit/shapes/textTests.js +++ b/test/unit/Text-test.js @@ -1,7 +1,8 @@ -Test.Modules.Text = { - 'add text with shadows': function(containerId) { +suite('Text', function(){ + // ====================================================== + test('add text with shadows', function() { var stage = new Kinetic.Stage({ - container: containerId, + container: 'container', width: 578, height: 200 }); @@ -55,11 +56,13 @@ Test.Modules.Text = { layer.add(group); stage.add(layer); - test(text.getClassName() === 'Text', 'getClassName should be Text'); - }, - 'text getters and setters': function(containerId) { + assert.equal(text.getClassName(),'Text', 'getClassName should be Text'); + }); + + // ====================================================== + test('text getters and setters', function() { var stage = new Kinetic.Stage({ - container: containerId, + container: 'container', width: 578, height: 200 }); @@ -96,25 +99,25 @@ Test.Modules.Text = { * test getters and setters */ - test(text.getX() === stage.getWidth() / 2, 'text box x should be in center of stage'); - test(text.getY() === stage.getHeight() / 2, 'text box y should be in center of stage'); - test(text.getText() === 'Hello World!', 'text should be Hello World!'); - test(text.getFontSize() == 50, 'font size should 50'); - test(text.getFontFamily() == 'Calibri', 'font family should be Calibri'); - test(text.getFontStyle() == 'normal', 'font style should be normal'); - test(text.getFill() == '#888', 'text fill should be #888'); - test(text.getStroke() == '#333', 'text fill should be #333'); - test(text.getAlign() === 'right', 'text should be aligned right'); - test(text.getLineHeight() === 1.2, 'line height should be 1.2'); - test(text.getWidth() === 400, 'width should be 400'); - test(text.getHeight() === 100, 'height should be 100'); - test(text.getPadding() === 10, 'padding should be 10'); - test(text.getShadowColor() === 'black', 'text box shadow color should be black'); - test(text.getDraggable() === true, 'text should be draggable'); - test(text.getWidth() === 400, 'box width should be 400'); - test(text.getHeight() === 100, 'box height should be 100'); - test(text.getTextWidth() > 0, 'text width should be greater than 0'); - test(text.getTextHeight() > 0, 'text height should be greater than 0'); + assert.equal(text.getX(), stage.getWidth() / 2); + assert.equal(text.getY(), stage.getHeight() / 2); + assert.equal(text.getText(), 'Hello World!'); + assert.equal(text.getFontSize(), 50); + assert.equal(text.getFontFamily(), 'Calibri'); + assert.equal(text.getFontStyle(), 'normal'); + assert.equal(text.getFill(), '#888'); + assert.equal(text.getStroke(), '#333'); + assert.equal(text.getAlign(), 'right'); + assert.equal(text.getLineHeight(), 1.2); + assert.equal(text.getWidth(), 400); + assert.equal(text.getHeight(), 100); + assert.equal(text.getPadding(), 10); + assert.equal(text.getShadowColor(), 'black'); + assert.equal(text.getDraggable(), true); + assert.equal(text.getWidth(), 400); + assert.equal(text.getHeight(), 100); + assert(text.getTextWidth() > 0, 'text width should be greater than 0'); + assert(text.getTextHeight() > 0, 'text height should be greater than 0'); text.setX(1); text.setY(2); @@ -131,20 +134,20 @@ Test.Modules.Text = { text.setShadowColor('green'); text.setDraggable(false); - test(text.getX() === 1, 'text box x should be 1'); - test(text.getY() === 2, 'text box y should be 2'); - test(text.getText() === 'bye world!', 'text should be bye world!'); - test(text.getFontSize() == 10, 'font size should 10'); - test(text.getFontFamily() == 'Arial', 'font family should be Arial'); - test(text.getFontStyle() == 'bold', 'font style should be bold'); - test(text.getFill() == 'green', 'text fill should be green'); - test(text.getStroke() == 'yellow', 'text fill should be yellow'); - test(text.getAlign() === 'left', 'text should be aligned left'); - test(text.getWidth() === 300, 'width should be 300'); - test(text.getHeight() === 75, 'height should be 75'); - test(text.getPadding() === 20, 'padding should be 20'); - test(text.getShadowColor() === 'green', 'text box shadow color should be green'); - test(text.getDraggable() === false, 'text draggable should be false'); + assert.equal(text.getX(), 1); + assert.equal(text.getY(), 2); + assert.equal(text.getText(), 'bye world!'); + assert.equal(text.getFontSize(), 10); + assert.equal(text.getFontFamily(), 'Arial'); + assert.equal(text.getFontStyle(), 'bold'); + assert.equal(text.getFill(), 'green'); + assert.equal(text.getStroke(), 'yellow'); + assert.equal(text.getAlign(), 'left'); + assert.equal(text.getWidth(), 300); + assert.equal(text.getHeight(), 75); + assert.equal(text.getPadding(), 20); + assert.equal(text.getShadowColor(), 'green'); + assert.equal(text.getDraggable(), false); // test set text to integer text.setText(5); @@ -154,10 +157,12 @@ Test.Modules.Text = { //layer.setListening(false); layer.drawHit(); - }, - 'text multi line': function(containerId) { + }); + + // ====================================================== + test('text multi line', function() { var stage = new Kinetic.Stage({ - container: containerId, + container: 'container', width: 578, height: 200 }); @@ -195,7 +200,7 @@ Test.Modules.Text = { layer.add(rect).add(text); stage.add(layer); - test(text.getLineHeight() === 1, 'text line height should be defaulted to 1'); + assert.equal(text.getLineHeight(), 1); /* text.transitionTo({ @@ -210,10 +215,12 @@ Test.Modules.Text = { */ - }, - 'text multi line with shadows': function(containerId) { + }); + + // ====================================================== + test('text multi line with shadows', function() { var stage = new Kinetic.Stage({ - container: containerId, + container: 'container', width: 578, height: 200 }); @@ -245,10 +252,14 @@ Test.Modules.Text = { layer.add(text); stage.add(layer); - }, - 'change font size should update text data': function(containerId) { + console.log(layer.getContext().getTrace()); + + }); + + // ====================================================== + test('change font size should update text data', function() { var stage = new Kinetic.Stage({ - container: containerId, + container: 'container', width: 578, height: 200 }); @@ -280,8 +291,8 @@ Test.Modules.Text = { //console.log(text.getHeight() + ',' + height); - test(text.getWidth() > width, 'text box width should have increased.'); - test(text.getHeight() > height, 'text box height should have increased.'); + assert(text.getWidth() > width, 'width should have increased'); + assert(text.getHeight() > height, 'height should have increased'); - } -}; + }); +}); \ No newline at end of file