diff --git a/CanvasTextWrapper.js b/CanvasTextWrapper.js index 705b141..97b04f3 100644 --- a/CanvasTextWrapper.js +++ b/CanvasTextWrapper.js @@ -1,5 +1,5 @@ /*! CanvasTextWrapper (https://github.com/namniak/CanvasTextWrapper) - * Version: 0.1.0 + * Version: 0.2.0 * * MIT License (http://www.opensource.org/licenses/mit-license.html) * Copyright (c) 2014 Vadim Namniak @@ -16,7 +16,7 @@ paddingY: 0, // zero px top & bottom text padding relative to canvas or parent fitParent: false, // text is tested to fit canvas width lineBreak: 'auto', // text fills the element's (canvas or parent) width going to a new line on a whole word - sizeToFill: false // text is resized to fill the container height (given font size is ignored) + sizeToFill: false // text is resized to fill the container (given font size is ignored) }; window.CanvasTextWrapper = function(canvas, text, opts) { @@ -34,7 +34,7 @@ } // extract font size - this.lineHeight = parseInt(this.font.replace(/^\D+/g, ''), 10); + this.lineHeight = parseInt(this.font.replace(/^\D+/g, ''), 10) || 18; // validate all set properties this.validate(); @@ -78,10 +78,10 @@ this.setTextVerticalAlign(textPos, textBlockHeight); for (var i = 0; i < lines.length; i++) { - this.setTextHorizontalAlign(context, textPos, elementWidth, lines[i]); + this.setTextHorizontalAlign(this.context, textPos, elementWidth, lines[i]); textPos.y = parseInt(textPos.y) + parseInt(this.lineHeight); - context.fillText(lines[i], textPos.x, textPos.y); + this.context.fillText(lines[i], textPos.x, textPos.y); } }, @@ -196,4 +196,4 @@ } } }; -})(); +})(); \ No newline at end of file diff --git a/CanvasTextWrapper.min.js b/CanvasTextWrapper.min.js index 3d652df..40abfd1 100644 --- a/CanvasTextWrapper.min.js +++ b/CanvasTextWrapper.min.js @@ -1,7 +1,7 @@ /*! CanvasTextWrapper (https://github.com/namniak/CanvasTextWrapper) - * Version: 0.1.0 + * Version: 0.2.0 * * MIT License (http://www.opensource.org/licenses/mit-license.html) * Copyright (c) 2014 Vadim Namniak */ -!function(){"use strict";var a={font:"18px Arial, sans-serif",textAlign:"left",verticalAlign:"top",paddingX:0,paddingY:0,fitParent:!1,lineBreak:"auto",sizeToFill:!1};window.CanvasTextWrapper=function(b,c,d){if(!(this instanceof CanvasTextWrapper))throw new TypeError('CanvasTextWrapper constructor failed. Use "new" keyword when instantiating.');this.canvas=b,this.text=c;for(var e in a)this[e]=d&&d[e]?d[e]:a[e];this.lineHeight=parseInt(this.font.replace(/^\D+/g,""),10),this.validate(),this.context=this.canvas.getContext("2d"),this.context.font=this.font,this.context.textBaseline="bottom",this.drawText()},CanvasTextWrapper.prototype={drawText:function(){var a=this.fitParent===!1?this.canvas.width:this.canvas.parentNode.clientWidth,b={x:0,y:0};if(this.sizeToFill){var c=(this.fitParent===!1?this.canvas.height:this.canvas.parentNode.clientHeight)-2*this.paddingX,d=this.text.trim().split(/\s+/).length,e=0;do{this.setFontSize(++e);var f=this.getWrappedText(a),g=f.length*this.lineHeight}while(c>g&&f.join(" ").split(/\s+/).length==d);this.setFontSize(--e)}var f=this.getWrappedText(a),g=f.length*this.lineHeight;this.setTextVerticalAlign(b,g);for(var h=0;hc){for(var g=0;a.measureText(e+b[d][g]).width<=c&&gg&&f.join(" ").split(/\s+/).length==d);this.setFontSize(--e)}var f=this.getWrappedText(a),g=f.length*this.lineHeight;this.setTextVerticalAlign(b,g);for(var h=0;hc){for(var g=0;a.measureText(e+b[d][g]).width<=c&&g