Syntax
new CanvasTextWrapper(HTMLCanvasElement, String [, Options]);
-
font(String) - text style that includes font size in px (REQUIRED), weight & family, etc. specified similarly to CSS font shorthand property
-
textAlign(String) - horizontal alignment that applies for each line
- "left"
- "center"
- "right"
-
verticalAlign(String) - vertical alignment that applies on a whole block of text
- "top"
- "middle"
- "bottom"
-
paddingX(Number) - horizontal padding in pixels set equally on both, left and right sides of the element
-
paddingY(Number) - vertical padding in pixels set equally on both, top and bottom sides of the element
-
fitParent(Boolean) - parameter that controls which element to fit
- true - fit canvas parent's width instead of canvas own width
- false - fit canvas width
-
lineBreak(String) - text split rule
- "auto" - text fills the element's width going to a new line on a whole word when no more space
- "word" - each next word will be placed on a new line
-
sizeToFill(Boolean) - auto font size to fill text container
- "true" - ignore given font size and resize text to fill its padded container
- "true" - use specified or default font size
NOTE: if a single word is too long to fit the width with specified font size, it will be broken into as many lines as required on any letter without specific word breaking rule unless sizeToFill option is used.
Defaults
The default options object which values will be used if a property is not specified or no object is passed:
{
font: "18px Arial, sans-serif",
textAlign: "left",
verticalAlign: "top",
paddingX: 0,
paddingY: 0,
fitParent: false,
lineBreak: "auto",
sizeToFill: false
}
Usage
Use standard canvas text drawing methods such as "fillStyle" and "globalCompositeOperation" when needed before using CanvasTextWrapper like so:
var canvas = document.createElement('canvas');
canvas.width = 300;
canvas.height = 250;
context = canvas.getContext("2d");
context.fillStyle = "rgb(255, 255, 255)";
context.fillRect(0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = "destination-out";
var wrapper = new CanvasTextWrapper(canvas, 'Hello'); // default options will apply
Installation
bower install canvas-text-wrapper
npm install canvas-text-wrapper