GithubHelp home page GithubHelp logo

koroeskohr / canvas-text-wrapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from namniak/canvas-text-wrapper

1.0 3.0 0.0 2.65 MB

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

Home Page: http://namniak.github.io/canvas-text-wrapper/

License: MIT License

JavaScript 100.00%

canvas-text-wrapper's Introduction

canvas-text-wrapper

Syntax

CanvasTextWrapper(HTMLCanvasElement, String [, options]);

Options

Option Value Description
font String Text style that includes font size (in px), font weight, font family, etc.
lineHeight String or Number Number - '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.
paddingX Number Horizontal padding (in px) that is equally set on left and right sides.
paddingY Number Vertical padding (in px) that is equally set on top and bottoms.
fitParent Boolean Fit 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
sizeToFill Boolean Ignore given font size and line height and resize text to fill its padded container.
maxFontSizeToFill Number If above option is true text won't be bigger than set.
strokeText Boolean Allow text outline based on canvas context configuration.
justifyLines Boolean All lines will try to match the same width with flexed spaces between the words.
allowNewLine Boolean Text breaks on a new line character "\n". Note it doesn't support multiple new lines, so "\n\n\n" will be rendered the same as a single "\n".
renderHDPI Boolean Text 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

canvas-text-wrapper's People

Contributors

gmjosack avatar koroeskohr avatar martinpucala avatar namniak avatar steoo avatar tony-pizza avatar vladholubiev avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.