Recent

Author Topic: Does a slightly better TCanvas exist anywhere?  (Read 1394 times)

QuinnMartin

  • Jr. Member
  • **
  • Posts: 56
Does a slightly better TCanvas exist anywhere?
« on: February 15, 2024, 03:41:42 am »
I am starting a new project in Lazarus and would like TCanvas support for z-ordering, basic transparency, anti-aliased lines, and better performance (i.e. Pixels[x,y] in Delphi was way too slow).  It would also be nice to have lines with fractional widths rather than just width=1,2 etc.  I will just be drawing lines, filled areas, pixels, and so forth.  Does such a component exist anywhere?  Hopefully nothing too complex as I am just getting my feet wet with Lazarus.  Any suggestions are appreciated.

Thanks.

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: Does a slightly better TCanvas exist anywhere?
« Reply #1 on: February 15, 2024, 07:22:07 am »
Try BGRAControls, here is an example of an anti-aliased circle:
https://forum.lazarus.freepascal.org/index.php/topic,64685.msg492036.html#msg492036

Fast access to pixels:

Quote
Direct access to pixels
To access pixels, there are two properties, Data and Scanline. The first gives a pointer to the first pixel of the image, and the second gives a pointer to the first pixel of a given line.

Wiki:
https://wiki.freepascal.org/BGRABitmap

circular

  • Hero Member
  • *****
  • Posts: 4369
    • Personal webpage
Re: Does a slightly better TCanvas exist anywhere?
« Reply #2 on: February 15, 2024, 09:07:12 am »
Welcome to the forum, QuinnMartin  :)

I suppose Fibonacci meant "try BGRABitmap". BGRAControls is a UI based on BGRABitmap.

TBGRABitmap is an image that has various drawing functions, with any line width and opacity. They are available directly at the root of the object.
https://wiki.freepascal.org/BGRABitmap_tutorial_6

For ease of use, it also provides a CanvasBGRA property that is very similar to TCanvas, though you will probably find it limiting as it works with integer coordinates and line width.

Otherwise it provides a Canvas2d property that is similar to the HTML canvas (any line width and opacity) and that handles affine transforms.

About z-ordering, there is TBGRALayeredBitmap that can store multiple images and render the result. See second part of this tutorial:
https://wiki.freepascal.org/BGRABitmap_tutorial_5

Regards
Conscience is the debugger of the mind

Thaddy

  • Hero Member
  • *****
  • Posts: 16367
  • Censorship about opinions does not belong here.
Re: Does a slightly better TCanvas exist anywhere?
« Reply #3 on: February 15, 2024, 05:47:52 pm »
Two rematks:
1) franctionals make no sense as Tcanvas is for display. E.g. you can render the data for a CAD drawing in memory, but that needs to be scaled to pixels. So use pixel coordinates, not floats.
2) Accessing Pixel[x,y] in a loop is not a very good idea. On Windows use Scanline.
There is nothing wrong with being blunt. At a minimum it is also honest.

lainz

  • Hero Member
  • *****
  • Posts: 4659
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Does a slightly better TCanvas exist anywhere?
« Reply #4 on: February 15, 2024, 07:36:58 pm »
Two rematks:
1) franctionals make no sense as Tcanvas is for display. E.g. you can render the data for a CAD drawing in memory, but that needs to be scaled to pixels. So use pixel coordinates, not floats.
2) Accessing Pixel[x,y] in a loop is not a very good idea. On Windows use Scanline.

But with Canvas2D you can use floats, because it makes the antialias different (more or less light), so it worth using float. Say for the line width. With this you can move objects smoothly and scale smoothly as well.

Scanline is good as well, but better use the drawing functions provided, else you need to compute the antialias. But for making a pixel work BGRABitmap as well has Scanline, for Windows and all OS.

QuinnMartin

  • Jr. Member
  • **
  • Posts: 56
Re: Does a slightly better TCanvas exist anywhere?
« Reply #5 on: February 15, 2024, 07:39:15 pm »
Fractional widths are definitely used for antialiased lines.  I use them all the time in other environments like Python MatPlotLib to get fine grained control of lines that are plotted.

Thaddy

  • Hero Member
  • *****
  • Posts: 16367
  • Censorship about opinions does not belong here.
Re: Does a slightly better TCanvas exist anywhere?
« Reply #6 on: February 15, 2024, 07:44:27 pm »
NO. anti aliasing is not performed on the canvas level. In any language. It is performed in memory and only then scaled back to pixels.
There is nothing wrong with being blunt. At a minimum it is also honest.

lainz

  • Hero Member
  • *****
  • Posts: 4659
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Does a slightly better TCanvas exist anywhere?
« Reply #7 on: February 15, 2024, 10:06:58 pm »
NO. anti aliasing is not performed on the canvas level. In any language. It is performed in memory and only then scaled back to pixels.

Cool, thanks for the information.

I didn't want to say that is that way, just I didn't know the way is computed- :)

circular

  • Hero Member
  • *****
  • Posts: 4369
    • Personal webpage
Re: Does a slightly better TCanvas exist anywhere?
« Reply #8 on: February 16, 2024, 12:54:43 am »
Thaddy, while the technical nuances of rendering and coordinate systems are important, our main focus here is on providing QuinnMartin with the best tools for their project. BGRABitmap offers a versatile solution that incorporates floating-point values for greater precision in visual rendering, for example when specifying line width. It seems to align well with QuinnMartin's requirements.

I would not want newcomers to be put off by unsolicited debate. In the rendering process, at some point, fractional sizes are translated into whole pixels using all the available colors, like with antialiasing. From the user perspective, the values are indeed fractional and QuinnMartin's statement is correct. Using integer values would not yield the same visual result.

The rasterization process though is a subject I find fascinating. Programming such a library gave me insights that would be interesting to talk about. For example, a width of 0.5 is similar to an opacity of 50% but it is not quite the same. And a line, even if rendered without antialising, will not be the same if provided with floating-point coordinates instead of integers as it changes the Bresenham algorithm.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018