Skip to content

Effects

An effect changes how a node is drawn: rounded corners, a border, a shadow, a blur, a mask or a transform. It never changes the layout, and it works on every node, built-in or yours. This page shows how to add effects, drive them with changing values, and predict the result when several of them stack.

You will learn

  • How to add effects to a node and stack them
  • How to drive an effect with a value that changes
  • How to round a node together with its children
  • In which order JOID applies effects, and how to change it
  • How to clip your own drawing to a rectangle or an image

Node.effect adds an effect to a node and returns the node, so effects chain like any other setter:

RectNode
.create(150, 100, 300, 180)
.color(EffectsUI.SURFACE)
.effect(RoundedNodeEffect.create(24F))
.effect(BorderNodeEffect.create(EffectsUI.ACCENT, 2F))
.effect(ShadowNodeEffect.create(EffectsUI.ACCENT, 24F).offsetY(8D))
.attach(this);

Full file: EffectsUI.java

A dark card with rounded corners, a thin blue border and a soft blue glow under it, the shadow of the card

Three effects on one rectangle: the border and the shadow follow the rounded corners.

A node holds one effect per class: a second RoundedNodeEffect replaces the first. Node.getEffect returns the effect of a class, or null, and hasEffect, removeEffect and clearEffects manage the others. JOID ships seven effects; the catalog shows each one.

You wantEffect
Rounded corners, on every corner or on some of themRoundedNodeEffect
A round node cut from a square oneCircleNodeEffect
An outline that follows the drawn shapeBorderNodeEffect
A drop shadow or a glowShadowNodeEffect
A blurred nodeBlurNodeEffect
Only part of a node visible, cut by a rectangle or an imageMaskNodeEffect
A node moved, rotated or scaled without touching the layoutTransformNodeEffect

Each effect takes its main values in create(...) and the others through setters:

  • RoundedNodeEffect.create(radius) rounds every corner; top(false), bottom(false), left(false) and right(false) keep a side square, and a corner is rounded only when both of its sides are. The radius never goes past half the shorter side, so create(1000F) turns a rectangle into a pill.
  • BorderNodeEffect.create(color, width) outlines the pixels that the node draws: rounded corners, circles, glyphs, the opaque part of an image. mode(...) takes a BorderShader.BorderMode: BorderMode.OUT, the default, draws it outside the shape, and BorderMode.IN inside; corners(false) leaves the corners of an OUT border open.
  • ShadowNodeEffect.create(color, blur) draws a shadow of the shape of the node, moved by offsetX(...) and offsetY(...) in design pixels. It does not read the pixels of the node: it draws a rounded rectangle with the radius of the RoundedNodeEffect of the node, a disc for a CircleNode or a node with a CircleNodeEffect, and a plain rectangle otherwise.
  • BlurNodeEffect.create(radius) blurs the node, by a radius in design pixels.
  • MaskNodeEffect.create() keeps the whole node visible; x(...), y(...), width(...) and height(...) place the rectangle that stays visible, from the top-left corner of the node, and resource(...) cuts it to the opaque part of an image stretched over that rectangle.
  • TransformNodeEffect.create(transformation) applies a Transformation around the center of the node (Drawing resources and transforms).

A border, a blur or a rounding of 0 does nothing, so a value that drops to 0 turns the effect off.

Every setting of an effect takes a value or a Supplier, like a node setter (Signals). Build the suppliers that read the node inside Node.self, which hands you the node:

RectNode
.create(610, 100, 300, 180)
.color(EffectsUI.SURFACE)
.self(rect -> {
rect.effect(RoundedNodeEffect.create(() -> 8F + rect.hoverValue(40F)));
rect.effect(BorderNodeEffect.create(EffectsUI.ACCENT, 1F).width(() -> 1F + rect.hoverValue(5F)));
})
.attach(this);

Full file: EffectsUI.java

The radius goes from 8 to 48 design pixels and the border from 1 to 6 with the hover progress.

hoverValue(max) is the hover progress of the node scaled to max (Hover, tooltips and cursors). An animator drives an effect through an expression: RoundedNodeEffect.create(40F * appear.get()).

By default, an effect applies to the node alone, and its children draw on top of the result, untouched. NodeEffect.NodeEffectScope.CHILDREN, given to scope(...), applies the effect to the node and its children together:

RectNode
.create(1070, 80, 300, 220)
.color(EffectsUI.SURFACE)
.effect(RoundedNodeEffect.create(30F))
.body(card -> {
RectNode.create(0, 0, 300, 70).color(EffectsUI.ACCENT).attach(card);
})
.attach(this);
RectNode
.create(1470, 80, 300, 220)
.color(EffectsUI.SURFACE)
.effect(RoundedNodeEffect.create(30F).scope(NodeEffectScope.CHILDREN))
.body(card -> {
RectNode.create(0, 0, 300, 70).color(EffectsUI.ACCENT).attach(card);
})
.attach(this);

Full file: EffectsUI.java

Two dark cards with rounded corners and a blue header: on the left the header keeps its square top corners, on the right it is rounded with the card

With SELF, the default, the rounded corners cut only the card; with CHILDREN, they cut its header too.

Use CHILDREN for a card whose content reaches its edges, such as an image header or a list that scrolls. The scope matters for the effects that work on pixels (rounded, circle, blur and border): a shadow, a mask or a transform always covers the children.

Each effect has a priority, and JOID applies the effects of a node in a fixed frame, whatever the order you add them in:

Diagram of nested boxes. The outer box: 1, the wrapping effects start, lowest priority first: transform (-100), shadow (-50), mask (0), yours (0). Inside it, a second box holds a blue box: 2, the node draws itself, 3, its SELF passes by priority: rounded or circle (100), blur (150), border (200); and a dark grey box: 4, the children draw on top. Under them: 5, the CHILDREN passes run over the node and its children. At the bottom of the outer box: 6, the wrapping effects end, in reverse order.

The priorities decide what a border outlines, what a mask cuts and what a transform moves. A wrapping effect contains every effect of a higher priority.
  1. The effects that wrap the drawing (TransformNodeEffect, ShadowNodeEffect, MaskNodeEffect and most effects of your own) start, the lowest priority first.
  2. The node draws itself.
  3. Its SELF effects that work on pixels run on that drawing, by priority: rounded or circle, then blur, then border. A border outlines the rounded corners whatever the order you add them in.
  4. The children draw on top.
  5. The CHILDREN effects that work on pixels run on the node and its children together.
  6. The wrapping effects end, in reverse order.

priority(...) changes the priority of an effect, and two effects of the same priority keep the order in which you added them. In the example below, the Transformation turns each node by 20 degrees with Rotation.ROLL, in the plane of the screen (Rotate and scale a drawing). A transform, at -100, wraps the mask, at 0, so the mask turns with the node; a mask given a priority under -100 wraps the transform, and stays in place while the node turns under it:

RectNode
.create(250, 480, 200, 200)
.color(EffectsUI.ACCENT.withAlpha(0.3F))
.effect(TransformNodeEffect.create(Transformation.create().rotate(20D, Rotation.ROLL)))
.attach(this);
RectNode
.create(650, 480, 200, 200)
.color(EffectsUI.ACCENT.withAlpha(0.3F))
.effect(TransformNodeEffect.create(Transformation.create().rotate(20D, Rotation.ROLL)))
.attach(this);
RectNode
.create(250, 480, 200, 200)
.color(EffectsUI.ACTIVE)
.effect(TransformNodeEffect.create(Transformation.create().rotate(20D, Rotation.ROLL)))
.effect(MaskNodeEffect.create().y(70D).height(60D))
.attach(this);
RectNode
.create(650, 480, 200, 200)
.color(EffectsUI.ACTIVE)
.effect(TransformNodeEffect.create(Transformation.create().rotate(20D, Rotation.ROLL)))
.effect(MaskNodeEffect.create().y(70D).height(60D).priority(-200))
.attach(this);

Full file: EffectsUI.java

Two faint blue rectangles turned by 20 degrees, each crossed by the solid light blue band that its mask keeps. On the left, the band turns with the rectangle. On the right, the band stays horizontal.

Left: the mask runs inside the transform and turns with the node. Right: priority(-200) starts the mask before the transform, so it stays horizontal. The faint rectangles, two other nodes, show the whole node.

The same rule places a shadow: at its own -50, it turns and moves with the node; with a priority under -100, it stays where the layout puts the node. Custom effects shows how to write an effect of your own, and where its priority places it.

MaskNodeEffect clips a node; the same clipping is open to your own drawing code. UI.mask runs a drawing with everything outside a rectangle hidden:

RectNode
.create(1270, 480, 300, 200)
.onDraw(event -> {
final Node node = event.getNode();
final double centerX = node.getX() + node.getWidth() / 2D;
final double centerY = node.getY() + node.getHeight() / 2D;
super.mask(node.getX(), node.getY(), node.getWidth(), node.getHeight() / 2D, () -> {
DrawUtils.SHAPE.drawCircle(centerX, centerY, EffectsUI.ACCENT, 90D);
});
})
.attach(this);

Full file: EffectsUI.java

The top half of a blue disc, cut flat by the mask

The circle is drawn whole, and the mask keeps the top half of the node.

The overload that takes a Resource first keeps the pixels where the image is opaque (alpha above 0.5F), to cut a drawing to a star, a logo or a rounded frame. Masks nest: a mask started inside another one shows only what both of them keep. Call mask while JOID draws, such as in Node.onDraw or in the draw of a custom node: outside a frame, drawing throws. Drawing shapes and text explains the drawing calls.

  • effect(...) adds an effect to any node; effects stack, one per class.
  • An effect never changes the layout nor the area that receives the mouse.
  • Every setting takes a value or a supplier; self(...) gives the node to build suppliers on it.
  • Effects apply to the node alone; CHILDREN applies them to the node and its children together.
  • Wrapping effects run by priority, the lowest outermost; the pixel effects always run rounded or circle, then blur, then border.
  • Give a RectNode its border through its setters or through a BorderNodeEffect, never both.
  • UI.mask clips your own drawing to a rectangle or to the opaque part of an image.

A pill button. Turn a 200 × 60 rectangle into a pill with a 2 design pixel blue border inside the shape.

Solution
.effect(RoundedNodeEffect.create(30F))
.effect(BorderNodeEffect.create(EffectsUI.ACCENT, 2F).mode(BorderMode.IN))

A radius of half the height gives round ends.

A shadow that stays on the ground. A card turns with a TransformNodeEffect, and its shadow must stay upright under it. What do you change?

Solution

The priority of the shadow: ShadowNodeEffect.create(color, 24F).priority(-200) starts it before the transform, so the transform leaves it in place.

Report a problem with this page