Git Product home page Git Product logo

canvas-txt's Introduction

canvas-txt's People

Contributors

b-vm avatar dependabot[bot] avatar geongeorge avatar jiakuan avatar neomusic avatar strdr4605 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

canvas-txt's Issues

Add "..." if the text is too long

Hi there!

I'd like to suggest the addition of a "maxLines" parameter to limit text overflow by appending "..." if it exceeds the specified line count.

Adding this feature would greatly enhance the package's versatility in managing text display within defined boundaries.

Thanks for considering this enhancement!

The last character is out of the rectangle

Hi,
I noticed that sometimes the last character of the word is out of the rectangle :
image

This happens when the last character which is out the rectangle is also the last character of a word:

while (textpixlen < linelen) {
            textlen++
            texttoprint = temptext.substr(0, textlen)
            textpixlen = ctx.measureText(temptext.substr(0, textlen)).width
          }

This can be fixed by removing the last character after this loop :

textlen--
texttoprint = texttoprint.substr(0, textlen)

Can I make a PR for this ?

It is important for me because when the text is justified we see some lines with a character which is not aligned.

Overflow Y Hidden

This is very impressive.

Is there a way to stop text spilling out of the text box bounds?

If i provide too much text or increase the font size too much the text is spilling out of the text box at the top and bottom.

Multiple Colors

Do you think there will be support for multiple colored words at any stage?

Why Y position + fontsize?

Hi @geongeorge

Thank you for creating this great helper. Feel free to send me a PayPal donate link ๐Ÿ™‚.

I have a quick question: in your source code, I see you're adding the fontsize to the Y-position:

if (this.vAlign === 'top')
      txtY = y + this.fontSize

Because of that, my output is not aligned to the top of the debug lines:

image

Is there a reason I'm not aware about for adding the fontSize to the Y position? Can it be removed?

Having bold or italic text

Hi, thank you for this great library it is very useful.

I have a bold text to display, in canvas I would do context.font = 'bold 35pt Arial', but I saw that you override the font, so do you have an idea on how I can do this ?

Thank you.

Text height approximation

Currently the height of the text is auto approximated with
let charHeight = ctx.measureText('M').width;
This does not do justice to all types of fonts.

Add text-shadow to canvas-txt

I have an idea i dont know how to implement but i think it would be appriciated, my idea is to add the text-shadow from css into canvas-txt to make it possible to add shadows to text.

Issue when changing font size

I have a method in my react class component that display text on canvas ,The properties passed to canvas text can be changed through state of the component everything works fine and I can change the font color ,text position ,but I when I change font size with text on top right or top left position the text dissapears from the canvas ,but shows up If I change it position other then top right and top left

code I am using

  draw = (
    video: any,
    img: any,
    context: any,
    context2: any,
    width: any,
    height: any
  ) => {
    if (video.paused || video.ended) return false;
    context2.drawImage(video, 0, 0, width, height);
    context2.fillStyle = this.state.textColor;
    canvasTxt.fontSize = this.state.fontSize;
    canvasTxt.vAlign = this.state.vAlign;
    canvasTxt.align = this.state.align;
    canvasTxt.lineHeight = 20;
    canvasTxt.debug=true;
    canvasTxt.drawText(
      context2,
      this.state.text,
      30,
      30,
      width - 50,
      height - 50
    );
    let idata = context2.getImageData(0, 0, width, height);
    let that = this;
    context.putImageData(idata, 0, 0);
    setTimeout(function() {
      that.draw(video, img, context, context2, width, height);
    }, 0);
  };

Space at the beginning of line not preserved

Hello, let me start by thanking the creator/mantainers of this little library it's great! However I'm running into one issue, I have some text that has some whitespaces in front of it. When I try to draw this text I'm noticing that the whitespace is trimmed out (only if it's not between actual characters in a line).

Changes to canvas-txt API

Up until now, we have a canvasTxt object with props and methods you can invoke. This to me is a bad pattern. I built this a while back and it fitted my need perfectly back then. But as the library is evolving and others are using it, I feel it is necessary to follow a better pattern.

I can think of 2 different ways we can use canvas-txt:

1. Individual function export

This will look a lot like this

import { drawText } from 'canvas-txt'

const config = {
  width: 200,
  height: 200,
  x: 200,
  y: 200,
  vAlign: 'middle',
  align: 'center',
  debug: true
}

drawText(ctx, txt, config)

2. Class

This will look something like

import CanvasTxt from 'canvas-txt'

const ct = new CanvasTxt({
  vAlign: 'middle',
  align: 'center',
  debug: true
})


ct.drawText(ctx, txt, {
  width: 200,
  height: 200})
  
 ct.align = 'left'
 // Or a setter
 ct.setAlign('left')

I personally prefer the function approach, but the class might end up useful for some use cases. I would love if anyone has any suggestions or if there's a different approach we can go with.

Support for `overflow` property

Hi,

i was looking for overflow property to clip overflowed content of an element's box, but found nothing ๐Ÿ˜•
it would be great to have such feature. Could you please consider?

Before overflow:
image

After overflow:
image

The text goes out of the box with rtl language

Hello,

I have this strange issue when I write arabic, the text goes out of the box.
When I use align = left the text fits correctly, but when I make align = right it doesn't fit.
But with rtl language I need to align right.

I tried to set canvas.dir = 'rtl' but it didn't change.

Here some screenshots :
With align right :
image

With align left it fits perfectly:
image

The text I used to test :

ุงู„ู„ู‡ู ู„ุงูŽ ุฅูู„ูŽู‡ูŽ ุฅูู„ุงูŽู‘ ู‡ููˆูŽ ุงู„ุญูŽูŠูู‘ ุงู„ู‚ูŽูŠูู‘ูˆู…ู ู„ุงูŽ ุชูŽุฃู’ุฎูุฐูู‡ู ุณูู†ูŽุฉูŒ ูˆูŽ ู„ุงูŽ ู†ูŽูˆู’ู…ูŒ ู„ูŽู‡ู ู…ูŽุง ูููŠ ุงู„ุณูŽู‘ู…ูŽุงูˆูŽุงุชู ูˆูŽ ู…ูŽุง ูููŠ ุงู„ุฃูŽุฑู’ุถู ู…ูŽู†ู’ ุฐูŽุง ุงู„ูŽู‘ุฐููŠ ูŠูŽุดู’ููŽุนู ุนูู†ู’ุฏูŽู‡ู ุฅูู„ุงูŽู‘ ุจูุฅูุฐู’ู†ูู‡ู ูŠูŽุนู’ู„ูŽู…ู ู…ูŽุง ุจูŽูŠู’ู†ูŽ ุฃูŽูŠู’ุฏููŠู‡ูู…ู’ ูˆูŽ ู…ูŽุง ุฎูŽู„ู’ููŽู‡ูู…ู’ ูˆูŽ ู„ุงูŽ ูŠูุญููŠุทููˆู†ูŽ ุจูุดูŽูŠู’ุกู ู…ูู†ู’ ุนูู„ู’ู…ูู‡ู ุฅูู„ุงูŽู‘ ุจูู…ูŽุง ุดูŽุงุกูŽ ูˆูŽุณูุนูŽ ูƒูุฑู’ุณููŠูู‘ู‡ู ุงู„ุณูŽู‘ู…ูŽุงูˆูŽุงุชู ูˆูŽ ุงู„ุฃูŽุฑู’ุถูŽ ูˆูŽ ู„ุงูŽ ูŠูŽุฆููˆุฏูู‡ู ุญููู’ุธูู‡ูู…ูŽุง ูˆูŽ ู‡ููˆูŽ ุงู„ุนูŽู„ููŠูู‘ ุงู„ุนูŽุธููŠู…ู.

Do you have an idea of what I could do ?

\n breaks, inconsistent line length

Support for \n line breaks is already in here but
this is only accounted for after lines are already split into arrays.
This ends up creating lines with a single word or few
image

Code

...
var txt =
  'Hello how are \n Lorem ipsum dolor sit ame\n Lorem ipsum dolor sit ame Lorem ipsum dolor sit ame'

canvasTxt.fontSize = 24
canvasTxt.fontWeight = '100'
canvasTxt.fontStyle = 'oblique'
canvasTxt.fontVariant = 'small-caps'
// canvasTxt.debug = true
canvasTxt.align = 'right'
canvasTxt.vAlign = 'middle'
...

Feature Request: align justify

Hi @geongeorge ,

Is it possible to justify the text ?
I know that canvas context doesn't support "align justify" because it doesn't support multi-line text, but since this package provides multi-line support, maybe it could be a nice feature to have.

What do you think we need to do to implement this ?

Thank you.

Fit text on N lines

Hi @geongeorge

I know this library is probably not maintained anymore but I'm happy to pay for your time.

In your opinion, would it be a lot of work to add an option to force the content on 1 line (or N lines)? So when the width exceeds 1 line, the font size becomes smaller so the text still fits on that one line.

strokeText

How can I use strokeText with CanvasTxt?

Support for Node implementation

I love this package and would like to be able to use it in NodeJS applications as well.
Currently, this does not seem to be possible, as it results in the following error:
ReferenceError: window is not defined

Are there any workarounds for this, or is this something that could be changed in a future update?
Thanks!

Height estimation is incorrect for a majority of custom fonts

Snapshot of Untitled project - 2023-12-29 04-08-28 (0 000s)

In the screenshot I've loaded Source Sans Pro on the right which works correctly and is perfectly centered,
I also loaded in a random TTF font from dafont, and it's not centered. This is the case for most custom fonts I load, especially the artsy ones.

Changing the baseline, unfortunately, doesn't help much, with ideographic being the closest to being centered, and the rest just putting it even lower
msedge (2023-12-29 04ึ‰12ึ‰42)

Changing the bounding box width to be too low causes a freeze

Great library, we use it to render text on our site

I'm not 100% sure, however it seems to me that changing the bounding box' width to be too low for the text causes a freeze. I haven't tested with height but I imagine it's the same.

You can reproduce the issue on our site by opening the editor, then following these steps:

  1. Add a Text item
  2. Select (click) the Text item
  3. Type some text in the text field
  4. Under Positioning & Size, set the text box width to something like 1
  5. Observe the freeze

I haven't tested this issue in isolation using just your library, however no such issue has occurred with any other items we have, so I'm afraid it's your library - however, if it's not, feel free to close the issue.

Squares instead text

Hey yo! Someone knows why do i see squares instead normal text? I need help please i used as the example
image

Issue with new perf improvements branch

For this text copied for wikipedia

ๅผบ็ƒˆ็ƒญๅธฆๆฐ”ๆ—‹ๆœ้ฉฌๅฅ‡ๅฐ”ๆ˜ฏ2018ๅนด3ๆœˆไธŠๆ—ฌๅœจ้ฉฌ่พพๅŠ ๆ–ฏๅŠ ๅ’Œ็•™ๅฐผๆฑชๅผ•ๅ‘ๆดช็พ็š„ๅผบ็ƒˆ็†ฑๅธถๆฐฃๆ—‹๏ผŒ2ๆœˆ27ๆ—ฅ็”ฑ่ฅฟๅ—ๅฐๅบฆๆด‹้˜ฟๅŠ ่ŠๅŠ ็พคๅณถ้™„่ฟ‘็š„ไฝŽๆฐ”ๅŽ‹ๅŒบๅ‘ๅฑ•่€Œๆˆใ€‚็ณป็ปŸ3ๆœˆ2ๆ—ฅๅ‡่šๆˆ็ƒญๅธฆๆ‰ฐๅŠจ๏ผŒๆฌกๆ—ฅๅ‡็บง็ƒญๅธฆ้ฃŽๆšดๅนถ่Žทๅใ€‚ๆญคๅŽไธคๅคฉๆœ้ฉฌๅฅ‡ๅฐ”ๅ› ๅค–็•Œ็Žฏๅขƒๆœ‰ๅˆฉ่ฟ…้€Ÿๅขž

The previous version looks much more perfect

image
image

Knowing the height of the text inserted

I have a use case where I need to insert some text at the end of a canvas, and then I need to adjust the height of the canvas so it fits the text.
I am not sure how to do this, but I thought I could use the value of vheight calculated at this line :

const vheight = charHeight * (textarray.length - 1)

What do you think ? Maybe we could return it so we can use it :

const { vHeight } = canvasTxt.drawText(ctx, txt, 0, 0, 200, 200)

[Question] Is there possible to get the text height dynamically if the text wrapped?

Hi, wanna ask is there possible to get the text height dynamically if the text wrapped?

I've 2 different font of text stacking top bottom to each other, but I want the top text if it is goes to 2 lines, the bottom text position can be move to bottom. Is there a way to calculate this or just get the top text bounding box height so that I can get the value?

Whitespace returning wrong width

Scenario
Consider the following example with a <textarea> element over a <canvas> element, both with the exact same size, font properties and absolute position. If done right, they align perfectly. In the animation below textarea has a light grey background and canvas has the debug bounding box borders on.

canvas-text-issue-

Issue
textarea second line: 'render text on HTML5'
canvas second line: 'render text on'

Possible cause
I assume there is a whitespace at the beginning of the second line (so actually ' render text on') and the measured width is wrong.

nothing rendered on iOS11

Hi,

If I don't specify lineHeight nothing gets rendered on iOS11.
From reading the code i guess the bug is somewhere in the getTextHeight function.

Thanks for a great canvas-helper!

How to add options

I like how you did in the project page adding handles for the text field, position slider, width slider, height slider, etc.

I am wondering how you did it.
Is there any source I can have a look at it?

image

Performance upgrades

Hi George!

I am a big fan of your work; I really appreciate your contribution here. I wanted to use canvas-txt in another project I maintain however I found it to be too slow. I eventually forked and modified your library to the point of being basically unrecognizable, but made it better suited to the tasks I had at hand.

Anyway, I figured you might want to take some of the performance adaptations back into your library to make it much faster with large amounts of text. You can find the work here: https://github.com/glideapps/canvas-hypertxt

While I have every intention to maintain this library and keep it up to date as I need it for my own uses, I fully expect the vast majority of people are better served by canvas-txt unless they are performance sensitive or need to integrate into a larger rendering framework which already has text rendering functionality.

I'm still in the process of publishing and setting up project pages, if you decide to bring back any of the performance improvements Im happy to update the benchmarks.

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.