

Canvas-Txt - The better way to render text on HTML5 Canvas
source link: https://www.tuicool.com/articles/3MbUzeE
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Canvas Txt
The better way to render text on HTML5 Canvas
A library to print multiline text on HTML5 canvas with better line breaks and alignments :ab:
Demo
See Demo: Here
Install
npm install canvas-txt --save
Usage
<canvas id="myCanvas" width="500" height="500"></canvas>
Webpack
import canvasTxt from "canvas-txt"; var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var txt = "Lorem ipsum dolor sit amet"; canvasTxt.textSize = 24; canvasTxt.drawText(ctx,txt,100,200,200,200); //canvasTxt.drawText(ctx,txt,x,y,width,height);
CDN
See fiddle : here
<script src="https://unpkg.com/canvas-txt"></script>
var canvasTxt = window.canvasTxt.default; /// ...remaining same as webpack
Properties
Properties Default Descriptiondebug
false
Shows the border and align gravity for debugging purposes
align
center
Text align. Other possible values: left
, right
textSize
14
Font size of the text in px
font
Arial
Font family of the text
lineHeight
null
Line height of the text, if set to null it tries to auto-detect the value
Methods
Method DescriptiondrawText(ctx,text,x,y,width,height)
To draw the text to the canvas
Example
import canvasTxt from "canvas-txt"; var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); //You can use \n to define custom line breaks var txt = "Lorem \nipsum dolor sit amet"; //You can also use other methods alongside this ctx.fillStyle = "#ff0000"; //red color text canvasTxt.font = "Verdana"; canvasTxt.textSize = 20; canvasTxt.align = "left"; canvasTxt.lineHeight = 60; canvasTxt.debug = true; //shows debug info canvasTxt.drawText(ctx,txt,100,200,200,200)
Recommend
-
7
by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=1987
-
5
by zhangxinxu from http://www.zhangxinxu.com/wordpress/?p=6308 本文可全文转载,但需得到原作者书面许可,同时保留原作者和出处,摘要引流则随...
-
4
December 29, 2020 / #Web Development How to Create a Pixel Art Editor...
-
12
Introduction Most of the time, you will write Vue.js components that interact with a webpage via the DOM. But Vue’s reactivity system is useful for more than that! In this article, we’ll create a set of components to render...
-
11
How to test HTML5 canvas with jest? In this short article you will learn what you need to install in order to prepare a test environment for canvas operations with jest. After finishing the article, you will be ready...
-
13
How to write a simple collision detector in HTML5 canvas and JavaScript? Collision detection is the basics of entities interaction on canvas. Imagine you have a Player Character (PC) that’s walking through a maze...
-
6
HTML5 Canvas在预加载完字体后绘图 作者:广树
-
18
How to draw polygons on an HTML5 canvas? advertisements I need to know how to draw polygons on a canvas. Withou...
-
9
Create a dynamic tile swapping game in JavaScript. The result will work with any image and will have adjustable difficulty levels. The Complete HTML5 Canvas Puzzle Here is a demo of the puzzle we will be building: A cou...
-
8
Scratch Card · GitHub (function() { 'use strict';...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK