CanvasTextWrapper.js

Split canvas text into lines on specified rule with optional alignment, padding, and more. Supports HDPI screens.

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,
        renderHDPI: true,
        textDecoration: "none"
    }
    

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, {strokeText: true});
    

Installation


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

Examples