From 0073c4cbb96c4d761ee64168448abfac6ce5d93f Mon Sep 17 00:00:00 2001 From: Eric Rowell Date: Tue, 19 Mar 2013 10:17:09 -0700 Subject: [PATCH] TextPath now uses Text to build the context.font value for consistency and code reuse. Type._getRandomColorKey now uses an or bitwise operation to round rgb values much faster --- src/plugins/TextPath.js | 13 +++++++++---- src/shapes/Text.js | 7 +++++-- src/util/Type.js | 6 +++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/plugins/TextPath.js b/src/plugins/TextPath.js index b00153d7..d2f451b5 100644 --- a/src/plugins/TextPath.js +++ b/src/plugins/TextPath.js @@ -29,11 +29,12 @@ Kinetic.Plugins.TextPath.prototype = { _initTextPath: function(config) { + var that = this; + this.createAttrs(); this.dummyCanvas = document.createElement('canvas'); this.dataArray = []; - var that = this; - + // call super constructor Kinetic.Shape.call(this, config); @@ -60,7 +61,7 @@ drawFunc: function(canvas) { var charArr = this.charArr, context = canvas.getContext(); - context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily; + context.font = this._getContextFont(); context.textBaseline = 'middle'; context.textAlign = 'left'; context.save(); @@ -124,7 +125,7 @@ context.save(); - context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily; + context.font = this._getContextFont(); var metrics = context.measureText(text); context.restore(); @@ -304,6 +305,10 @@ } } }; + + // map TextPath methods to Text + Kinetic.Plugins.TextPath.prototype._getContextFont = Kinetic.Text.prototype._getContextFont; + Kinetic.Global.extend(Kinetic.Plugins.TextPath, Kinetic.Shape); // add setters and getters diff --git a/src/shapes/Text.js b/src/shapes/Text.js index 53b1b5a8..df91b149 100644 --- a/src/shapes/Text.js +++ b/src/shapes/Text.js @@ -89,7 +89,7 @@ textArrLen = textArr.length, totalWidth = this.getWidth(); - context.font = fontStyle + SPACE + fontSize + PX_SPACE + fontFamily; + context.font = this._getContextFont(); context.textBaseline = MIDDLE; context.textAlign = LEFT; context.save(); @@ -176,7 +176,7 @@ metrics; context.save(); - context.font = this.getFontStyle() + SPACE + fontSize + PX_SPACE + this.getFontFamily(); + context.font = this._getContextFont(); metrics = context.measureText(text); context.restore(); @@ -185,6 +185,9 @@ height: parseInt(fontSize, 10) }; }, + _getContextFont: function() { + return this.getFontStyle() + SPACE + this.getFontSize() + PX_SPACE + this.getFontFamily(); + }, _expandTextData: function(arr) { var len = arr.length; n = 0, diff --git a/src/util/Type.js b/src/util/Type.js index 347d5c9f..b9b8355b 100644 --- a/src/util/Type.js +++ b/src/util/Type.js @@ -280,9 +280,9 @@ }; }, _getRandomColorKey: function() { - var r = Math.round(Math.random() * 255); - var g = Math.round(Math.random() * 255); - var b = Math.round(Math.random() * 255); + var r = (Math.random() * 255) | 0; + var g = (Math.random() * 255) | 0; + var b = (Math.random() * 255) | 0; return this._rgbToHex(r, g, b); }, // o1 takes precedence over o2