Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to canvas-txt API #77

Closed
geongeorge opened this issue Aug 16, 2022 · 7 comments
Closed

Changes to canvas-txt API #77

geongeorge opened this issue Aug 16, 2022 · 7 comments

Comments

@geongeorge
Copy link
Owner

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.

This was referenced Aug 16, 2022
@PoeHaH
Copy link

PoeHaH commented Aug 16, 2022

To me, a function is a single action while a class is a collection of multiple actions.

So in its current form, I also prefer the function approach. It doesn't overcomplicates things.

However, if you're planning on expanding the library with other canvas-related tasks , such as adding images on a canvas or whatever else you want, then it would make sense to go the class approach.

My vote for now is the function approach :).

@jassmith
Copy link

I think the function approach is also superior for your users. The only advantage I see to the class approach potentially is if you can somehow use internal state to make things faster, however I can't really think of a good way to do that. The class is just overhead for you. The function could be trivially wrapped into a class by anyone who needs that form of API.

@geongeorge
Copy link
Owner Author

Thank you @PoeHaH and @jassmith for your quick reply, I'll go ahead with the function approach :)

@mgambati
Copy link

The functional approach looks awesome.

One feature request (I may try to implement it too) is the text-overflow behaviors from CSS.: ellipsis and clip. How hard would that be to implement?

@jassmith
Copy link

I've implemented those in custom codebases. They are possible to implement but come with significant performance overhead. They are also imperfect due to hinting/kerning against the … character being somewhat unpredictable. The character may kern enough against the lead out character that you could actually have fit the next char as well, but that's probably not a big issue.

@geongeorge
Copy link
Owner Author

geongeorge commented Jul 14, 2023

Thank you for your feedback everyone, it only took me an eternity but I'm ready with this now: #76

import { drawText, splitText, getTextHeight } from 'canvas-txt'

@geongeorge
Copy link
Owner Author

New version published https://www.npmjs.com/package/canvas-txt 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants