From 27532e1d05771946e6182bdfee3275165f4a4bc1 Mon Sep 17 00:00:00 2001 From: Barrett Harber Date: Fri, 13 Dec 2013 02:07:16 -0500 Subject: [PATCH] Added Ring Shape MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ring shape—circle with hollow center. --- Gruntfile.js | 1 + src/shapes/Ring.js | 105 ++++++++++++++++++++++++++++++++++ test/runner.html | 1 + test/unit/shapes/Ring-test.js | 23 ++++++++ 4 files changed, 130 insertions(+) create mode 100644 src/shapes/Ring.js create mode 100644 test/unit/shapes/Ring-test.js diff --git a/Gruntfile.js b/Gruntfile.js index ea767943..ac58660b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -20,6 +20,7 @@ module.exports = function(grunt) { 'src/shapes/Rect.js', 'src/shapes/Circle.js', 'src/shapes/Ellipse.js', + 'src/shapes/Ring.js', 'src/shapes/Wedge.js', 'src/shapes/Arc.js', 'src/shapes/Image.js', diff --git a/src/shapes/Ring.js b/src/shapes/Ring.js new file mode 100644 index 00000000..8df81696 --- /dev/null +++ b/src/shapes/Ring.js @@ -0,0 +1,105 @@ +(function() { + // the 0.0001 offset fixes a bug in Chrome 27 + var PIx2 = (Math.PI * 2) - 0.0001; + + /** + * Ring constructor + * @constructor + * @augments Kinetic.Shape + * @param {Object} config + * @param {Number} config.angle + * @param {Number} config.angleDeg angle in degrees + * @param {Number} config.innerRadius + * @param {Number} config.outerRadius + * @param {Boolean} [config.clockwise] + * @@shapeParams + * @@nodeParams + * @example + * var Ring = new Kinetic.Ring({
+ * innerRadius: 40,
+ * outerRadius: 80,
+ * fill: 'red',
+ * stroke: 'black',
+ * strokeWidth: 5
+ * }); + */ + Kinetic.Ring = function(config) { + this.___init(config); + }; + + Kinetic.Ring.prototype = { + ___init: function(config) { + // call super constructor + Kinetic.Shape.call(this, config); + this.className = 'Ring'; + }, + drawFunc: function(context) { + context.beginPath(); + context.arc(0, 0, this.getInnerRadius(), 0, PIx2, false); + context.moveTo(this.getOuterRadius(), 0); + context.arc(0, 0, this.getOuterRadius(), PIx2, 0, true); + context.closePath(); + context.fillStrokeShape(this); + }, + // implements Shape.prototype.getWidth() + getWidth: function() { + return this.getOuterRadius() * 2; + }, + // implements Shape.prototype.getHeight() + getHeight: function() { + return this.getOuterRadius() * 2; + }, + // implements Shape.prototype.setWidth() + setWidth: function(width) { + Kinetic.Node.prototype.setWidth.call(this, width); + this.setOuterRadius(width / 2); + }, + // implements Shape.prototype.setHeight() + setHeight: function(height) { + Kinetic.Node.prototype.setHeight.call(this, height); + this.setOuterRadius(height / 2); + } + }; + Kinetic.Util.extend(Kinetic.Ring, Kinetic.Shape); + + // add getters setters + Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'innerRadius', function() { + return 0; + }); + + /** + * set innerRadius + * @name setInnerRadius + * @method + * @memberof Kinetic.Ring.prototype + * @param {Number} innerRadius + */ + + /** + * get innerRadius + * @name getInnerRadius + * @method + * @memberof Kinetic.Ring.prototype + * @returns {Number} + */ + + Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'outerRadius', function() { + return 0; + }); + + /** + * set outerRadius + * @name setOuterRadius + * @method + * @memberof Kinetic.Ring.prototype + * @param {Number} innerRadius + */ + + /** + * get outerRadius + * @name getOuterRadius + * @method + * @memberof Kinetic.Ring.prototype + * @returns {Number} + */ +})(); diff --git a/test/runner.html b/test/runner.html index f1e420dc..8ce6f4d6 100644 --- a/test/runner.html +++ b/test/runner.html @@ -64,6 +64,7 @@ + diff --git a/test/unit/shapes/Ring-test.js b/test/unit/shapes/Ring-test.js new file mode 100644 index 00000000..95d150ae --- /dev/null +++ b/test/unit/shapes/Ring-test.js @@ -0,0 +1,23 @@ +suite('Ring', function() { + + // ====================================================== + test('add ring', function() { + var stage = addStage(); + var layer = new Kinetic.Layer(); + var ring = new Kinetic.Ring({ + x: stage.getWidth() / 2, + y: stage.getHeight() / 2, + innerRadius: 50, + outerRadius: 90, + fill: 'green', + stroke: 'black', + strokeWidth: 4 + }); + layer.add(ring); + stage.add(layer); + assert.equal(ring.getClassName(), 'Ring'); + + var trace = layer.getContext().getTrace(); + assert.equal(trace, 'clearRect(0,0,578,200);save();transform(1,0,0,1,289,100);beginPath();arc(0,0,50,0,6.283,false);moveTo(90,0);arc(0,0,90,6.283,0,true);closePath();fillStyle=green;fill();lineWidth=4;strokeStyle=black;stroke();restore();'); + }); +}); \ No newline at end of file