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

Add background border-radius to text layer #9447

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

CANEVETGASPARD
Copy link

Closes #9004

Change List

  • Add backgroundBorderRadius prop to TextLayer.
  • Add render test for backgroundBorderRadius prop.
  • Update TextLayer api-reference documentation.

Copy link
Collaborator

@felixpalmer felixpalmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

float isBorder = smoothedge(distToEdge, vLineWidth);
fragColor = mix(vFillColor, vLineColor, isBorder);
float distToEdge = -dist;
float isBorder = smoothstep(vLineWidth - 1.0, vLineWidth + 1.0, distToEdge);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove smoothedge?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I replicate smoothedge. I will use it instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With shader code, it's best to avoid making unnecessary changes as each might introduce some bug

float maxBorderRadius = min(dimensions.x, dimensions.y) * 0.5;
vec4 borderRadius = vec4(min(textBackground.borderRadius, maxBorderRadius));
float dist = round_rect(pixelPosition, dimensions * 0.5, borderRadius);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to pass uv, dimensions & borderRadius as parameters to function to better encapsulate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean adding a function like that to compute the distance ?

float get_dist(vec2 uv, vec2 dimensions, vec4 borderRadius) {
  // Convert UV to center-based coordinates [-0.5, 0.5]
  vec2 pixelPosition = (uv - 0.5) * dimensions;

  float maxBorderRadius = min(dimensions.x, dimensions.y) * 0.5;
  return round_rect(pixelPosition, dimensions * 0.5, vec4(min(borderRadius, maxBorderRadius)));
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, rather than implement round_rect(pixelPosition, size, radii) have round_rect(uv, dimensions, radii) and then calculate pixelPosition, maxBorderRadius etc in the function

void main(void) {
geometry.uv = uv;
vec2 pixelPosition = uv * dimensions;
// Convert UV to center-based coordinates [-0.5, 0.5]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an if block to only run if border radius is specified

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand why. By adding this borderRadius: {type: 'object', value: 0}, to TextBackgroundLayer default props, I thought that the default value of the props will be 0 and thus will never be undefined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but all that code will run (on every pixel) even for value=0, by putting it in a branch the GPU can skip it

@@ -87,6 +87,12 @@ type _TextLayerProps<DataT> = {
* @default 0
*/
getBorderWidth?: Accessor<DataT, number>;
/** The border radius of the background.
* If a number is supplied, it is the same border radius in pixel for all corner.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* If a number is supplied, it is the same border radius in pixel for all corner.
* If a number is supplied, it is the same border radius in pixel for all corners.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will fix that and will do it for the doc too.

@@ -16,21 +16,37 @@ in vec2 dimensions;
out vec4 fragColor;
float round_rect(vec2 p, vec2 size, vec4 radii) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match formatting with rest of shader code

fontFamily: 'Arial',
background: true,
backgroundPadding: [10, 10],
backgroundBorderRadius: [10, 10, 10, 10],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well make some labels have different radii to prove it works

@CANEVETGASPARD
Copy link
Author

Hello,
In the last commits I improve encapsulation in fragment shader and add an if case for borderRadius. Then I use different values for borderRadius in test and fix documentation

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

Successfully merging this pull request may close these issues.

Border radius on TextLayer background
2 participants