CanvasTextWrapper

Pure JavaScript canvas text wrapper that automatically splits a string into lines on specified rule with alignment and padding.

Syntax


CanvasTextWrapper(HTMLCanvasElement, String [, Options]);


Options
- is an object with the following available properties and values:

Default Options


    { 
        font: "18px Arial, sans-serif",
        lineHeight: 1,
        textAlign: "left",
        verticalAlign: "top",
        paddingX: 0,
        paddingY: 0,
        fitParent: false,
        lineBreak: "auto",
        sizeToFill: false,
        allowNewLine: true,
        justifyLines: false,
        strokeText: false
    }
    

Usage


Configure context settings properties such as "fillStyle", "lineWidth" or "strokeStyle" before using CanvasTextWrapper like so:

        var canvas = document.getElementById("#canvasText");
        canvas.width = 200;
        canvas.height = 200;
        context = canvas.getContext("2d");
        context.lineWidth = 2;
        context.strokeStyle = "#ff0000";
        CanvasTextWrapper(canvas,"Hello");    //default options will apply
    

Installation


bower install canvas-text-wrapper
npm install canvas-text-wrapper

Examples