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

HTML5 text artifacts for pixel fonts #2568

Open
DigiEggz opened this issue Jun 29, 2022 · 9 comments · May be fixed by #2572
Open

HTML5 text artifacts for pixel fonts #2568

DigiEggz opened this issue Jun 29, 2022 · 9 comments · May be fixed by #2572

Comments

@DigiEggz
Copy link

DigiEggz commented Jun 29, 2022

When targeting HTML5, there are artifacts around zoomed-in text, but only on some devices. I've updated to the latest files from this repository and the issue persists.

https://community.openfl.org/t/html5-text-artifacts/13514
https://community.openfl.org/t/html5-blurry-font-issue/12712

One one computer, the text renders normally:

On another computer, it renders with visual noise:

It may have to do with HiDPI settings (values > 1) and possibly for just pixel fonts, but I am uncertain. I'm willing to test any changes or suggestions!

@barisyild
Copy link
Contributor

I'm not sure but;

I guess while OpenFL draws better on high resolution screens; It draws worse on low resolution screens.

@DigiEggz
Copy link
Author

DigiEggz commented Jul 1, 2022

IMAGE 2022-07-01 12:21:53
I was able to get crisp text to render by doubling the ratio on HiDPI screens before scaling it, but it also requires passing the ratio directly from the window since the renderer can be null for TextFields.

@DigiEggz
Copy link
Author

DigiEggz commented Jul 1, 2022

The text is still blurry even with the new PR, but only on Chromium-based browsers. It appears to be a separate issue where Firefox scales things smoothly, but there's a heavy upscaling noise with Chrome:
v1

Within CanvasTextField, the new code dynamically sets the scale as follows:
var pixelRatio = (renderer.__pixelRatio > 1) ? (renderer.__pixelRatio * 2) : renderer.__pixelRatio;

However, Chrome still is applying some kind of upscaling and resulting in the image above. When I try setting the CanvasTextField to instead use var pixelRatio = 3;, the text no longer had any artifacts at any scaled resolution, even with higher pixel ratios.

v2

I'm not sure why "3" seems to be the magic number for the pixelRatio, as anything less than that is still blurry in Chrome and anything more is potentially overkill. I'm not sure what the best balance is for trying to render standard fonts and blocky fonts, but I'm happy to hear any thoughts on this.

@DigiEggz DigiEggz changed the title HTML5 text artifacts on some devices HTML5 text artifacts for pixel fonts Jul 2, 2022
@barisyild
Copy link
Contributor

Are you sure the problem is not the font?

@ShaharMS
Copy link
Contributor

ShaharMS commented Jul 2, 2022

Yes, it's like that with every font

@ShaharMS
Copy link
Contributor

ShaharMS commented Jul 2, 2022

Also, something to note - when resizing the page in the browser, the texts resolution stays the same - the means upscaling the page will make text look really low quality.

Thought I'll mention this because it might be related to the same problem

@DigiEggz
Copy link
Author

DigiEggz commented Jul 2, 2022

Yes, I've tried a couple different pixel fonts and they all exhibit the problem with the pixelRatio. What further complicates things is that each browser upscales differently and there are further differences depending on which OS the browser is running on and the display type (Retina / HiDPI).

Here are the results for different pixelRatio tests for Chromium-based browsers on Windows:

pixelRatio = 1
v1

pixelRatio = 2
v2

pixelRatio = 3
v3

The minimum scale that smooths out scaling artifacts for blocky fonts is a value of 3. In the first version, you can see artifacts on both sides of the text glyphs. With a scale of 2, you can see it smooths out one side but there is still an scaling problem on the other. When scaling to 3, both sides are smoothed out and the text renders with crisp edges.

I think allowing the user to pass a compiler flag, something like openfl_pixel_font, to render all text with crisp edges is a possible solution. Within CanvasTextField, I added the new define:

...
if (textField.__dirty || graphics.__softwareDirty)
{
            #if openfl_pixel_font
            var pixelRatio = 3;
	    #elseif (openfl_disable_hdpi || openfl_disable_hdpi_textfield)
	    var pixelRatio = 1;
            #else
            var pixelRatio = renderer.__pixelRatio;
	    #end

	    var width = Math.round(graphics.__width * pixelRatio);
            var height = Math.round(graphics.__height * pixelRatio);
...

With this code, adding the define openfl_pixel_font forces the text to render with sharp edges on all platforms I tested. These include Windows (Firefox, Chrome), macOS (Firefox, Chrome, Safari) and macOS with Retina display (Firefox, Chrome, Safari).

Does this sound like an acceptable fix or should we go a different direction?

@ShaharMS
Copy link
Contributor

ShaharMS commented Jul 3, 2022

Just wondering - after a window resize, is the text still crisp?

(resizes with ctrl +/-)

@DigiEggz
Copy link
Author

DigiEggz commented Jul 5, 2022

Just wondering - after a window resize, is the text still crisp?

(resizes with ctrl +/-)
Zooming with the browser did not affect the text blur or other graphics.

The main issue with text specifically is that the actual graphic texture is being stretched out and when relying on Chromium's renderer, it creates a streaking effect on either side of the text. You can see the problem happening here as well: https://community.openfl.org/t/html5-blurry-font-issue/12712

DigiEggz pushed a commit to DigiEggz/openfl that referenced this issue Jul 5, 2022
Pixel fonts render with artifacts around the edges of text for devices with high pixel ratios and also for Chromium-based browsers. This compiler flag forces a 3x pixel ratio, eliminating the ghosting effect on both sides of text. Fixes openfl#2568 , which has more information on the issue.
@DigiEggz DigiEggz linked a pull request Jul 5, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants