0.10.2 • Published 7 years ago

canvas-text-wrapper v0.10.2

Weekly downloads
644
License
MIT
Repository
github
Last release
7 years ago

canvas-text-wrapper

Syntax

CanvasTextWrapper(HTMLCanvasElement, String [, options]);

Options

OptionValueDescription
fontStringText style that includes font size (in px), font weight, font family, etc.
lineHeightString or NumberNumber - 'n' times font size where 1 is equivalent to '100%'. Also the property can be set in '%' or 'px'.
textAlign"left" "center" "right"Horizontal alignment of each line.
verticalAlign"top" "middle" "bottom"Vertical alignment of the whole text block.
paddingXNumberHorizontal padding (in px) that is equally set on left and right sides.
paddingYNumberVertical padding (in px) that is equally set on top and bottoms.
fitParentBooleanFit canvas' container size instead of its own size.
lineBreak"auto" "word""auto" - text goes to the next line on a whole word when there's no room "word" - each next word is placed on a new line
sizeToFillBooleanIgnore given font size and line height and resize text to fill its padded container.
maxFontSizeToFillNumberIf above option is true text won't be bigger than set.
strokeTextBooleanAllow text outline based on canvas context configuration.
justifyLinesBooleanAll lines will try to match the same width with flexed spaces between the words.
allowNewLineBooleanText breaks on a new line character "\n". Supports multiple consecutive new lines.
renderHDPIBooleanText is rendered based on device pixel ratio.
textDecoration"none" "underline"Text is underlined according to context.strokeStyle and context.lineWidth

NOTE: if a single word is too long to fit the width with specified font size, it will break on any letter unless sizeToFill option is enabled.

Default options

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

Usage

Configure context properties such as fillStyle, lineWidth, strokeStyle etc. before passing it to CanvasTextWrapper like so:

var CanvasTextWrapper = require('canvas-text-wrapper').CanvasTextWrapper;

var canvas = document.getElementById('#canvasText');
canvas.width = 200;
canvas.height = 200;
context = canvas.getContext('2d');
context.lineWidth = 2;
context.strokeStyle = '#ff0000';

CanvasTextWrapper(canvas, 'Hello', {strokeText: true});

Test

Run npm t NOTE: Test requires beefy to be installed globally

Examples

see here

Install

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

7 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.8.3

8 years ago

0.8.2

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.0

8 years ago

0.6.7

8 years ago

0.6.6

8 years ago

0.6.5

8 years ago

0.6.4

8 years ago

0.6.3

8 years ago

0.6.2

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago