Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/essentials/effects.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Shaders and Effects

The shader prop allows you to specify a custom shader. Most of the common use ones have shortcuts like `borderRadius`, `border`, `linearGradient`. These shortcuts get combined to create one dynamic shader from the Renderer. The order that they are listed in the props / style object affect how it gets created and could change the UI. Typically you want borderRadius to be the last one. If you have a custom shader, you'll need to use the `shader` prop and create it directly. A shader type named in a `shader` tuple must be registered with the renderer first, with `renderer.registerShaderType(name, type)`; renderer 2.0 draws nothing for an unregistered name and warns. `registerDefaultShaders(renderer)` registers the types Solid's style shortcuts create (`rounded`, `roundedWithBorder`, `roundedWithShadow`, `roundedWithBorderWithShadow`, `shadow`, `holePunch`, `linearGradient`, `radialGradient`). Check out this [example](https://github.com/lightning-js/renderer/blob/main/examples/tests/dynamic-shader.ts) from the renderer for more information.
The shader prop allows you to specify a custom shader. Most of the common use ones have shortcuts like `borderRadius`, `border`, `linearGradient`. These shortcuts get combined to create one dynamic shader from the Renderer. The order that they are listed in the props / style object affect how it gets created and could change the UI. Typically you want borderRadius to be the last one. If you have a custom shader, you'll need to use the `shader` prop and create it directly. A shader type named in a `shader` tuple must be registered with the renderer first, with `renderer.registerShaderType(name, type)`; renderer 2.0 draws nothing for an unregistered name and warns. `registerDefaultShaders(renderer)` registers the types Solid's style shortcuts create (`rounded`, `roundedWithBorder`, `roundedWithShadow`, `roundedWithBorderWithShadow`, `shadow`, `holePunch`, `dashedBorder`, `linearGradient`, `radialGradient`). Check out this [example](https://github.com/lightning-js/renderer/blob/main/examples/tests/dynamic-shader.ts) from the renderer for more information.

```jsx
const RoundedRectangle = ['RoundedRectangle', { radius: 6 }];
Expand Down Expand Up @@ -37,6 +37,26 @@ const style = {
};
```

## Dashed Border

`dashedBorder` clips the node's fill to a rounded rectangle and draws a dashed border inside its edge, e.g. for an empty placeholder. The dashes follow the corners, and the dash and gap are stretched slightly so that a whole number of dashes fits the perimeter. A transparent node draws only the dashes. A `gap` of 0 draws a solid border. It is a shader of its own, so it can't be combined with `border`, `borderRadius` or `shadow` on the same node. Needs `@solidtv/renderer` 2.0.9 or later.

```jsx
<view
width={300}
height={170}
dashedBorder={{
radius: 16,
width: 2,
color: 0xffffff33,
dash: 10,
gap: 10,
}}
/>
```

The DOM renderer draws it with CSS gradients, so `dash`, `gap`, `width`, `color` and `radius` work there too: a dashed strip per side and, with a `radius`, a quarter ring per corner. Each rounded corner is drawn as one dash, which is longer than `dash` when the radius is large.

## Linear Gradient & Radial Gradient

`linearGradient` and `radialGradient` are effects that can be used by setting the effects prop.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
},
"peerDependencies": {
"@solidjs/router": "^0.16.1",
"@solidtv/renderer": "^2.0.7",
"@solidtv/renderer": "^2.0.9",
"solid-js": "*"
},
"peerDependenciesMeta": {},
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions src/core/dom-renderer/dashedBorderStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import type { DashedBorderProps } from '@solidtv/renderer';

/**
* The DOM renderer's `dashedBorder`, in CSS only, so that `dash`, `gap`,
* `width`, `color` and `radius` draw as the WebGL shader draws them:
*
* - each straight side is a repeating linear gradient strip, `width` thick;
* - with a radius, each corner is a quarter ring (a radial gradient), drawn
* as one dash, and the straight sides run gap, dash, ..., dash, gap
* between them; without one, each side starts and ends with a dash, and
* the corners' dashes meet.
*
* On each side the dash and gap are stretched so that a whole number of
* them fits. A `gap` of 0 or less draws a solid border. Returns '' when
* nothing is drawn: a `dash`, `width` or colour alpha of 0 or less, or a
* node without size.
*
* Each gradient is written twice, the `-webkit-` syntax first, for older
* WebKit (e.g. PS4); a browser keeps the last declaration it can read.
*/
export function dashedBorderStyle(
w: number,
h: number,
props: Partial<DashedBorderProps>,
): string {
const width = props.width ?? 2;
const color = props.color ?? 0xffffffff;
const dash = props.dash ?? 10;
const gap = props.gap ?? 10;
if (w <= 0 || h <= 0 || width <= 0 || dash <= 0 || (color & 0xff) === 0) {
return '';
}
const rgba = `rgba(${(color >>> 24) & 0xff},${(color >>> 16) & 0xff},${(color >>> 8) & 0xff},${round((color & 0xff) / 255)})`;
const r = round(Math.max(0, Math.min(props.radius ?? 0, w / 2, h / 2)));
const px = `${width}px`;

// [-webkit- image, image, size, position]
const layers: [string, string, string, string][] = [];
const side = (
length: number,
dirs: [string, string],
size: string,
position: string,
) => {
const stops = strip(length, dash, gap, rgba, r > 0);
if (stops !== '') {
layers.push([
`-webkit-repeating-linear-gradient(${dirs[0]}, ${stops})`,
`repeating-linear-gradient(${dirs[1]}, ${stops})`,
size,
position,
]);
}
};
const straightX = round(w - 2 * r);
const straightY = round(h - 2 * r);
side(straightX, ['left', 'to right'], `${straightX}px ${px}`, `${r}px 0`);
side(straightX, ['left', 'to right'], `${straightX}px ${px}`, `${r}px 100%`);
side(straightY, ['top', 'to bottom'], `${px} ${straightY}px`, `0 ${r}px`);
side(straightY, ['top', 'to bottom'], `${px} ${straightY}px`, `100% ${r}px`);

if (r > 0) {
// A quarter ring around the corner's centre; the layer's border-radius
// clips (and smooths) its outer edge.
const inner = round(r - width);
const ring =
inner > 0
? `transparent ${round(inner - 0.5)}px, ${rgba} ${round(inner + 0.5)}px, ${rgba} ${r}px, transparent ${r}px`
: `${rgba} 0, ${rgba} ${r}px, transparent ${r}px`;
// The corner box's position, and the ring's centre in that box
const corners: [string, string][] = [
['0 0', '100% 100%'],
['100% 0', '0 100%'],
['0 100%', '100% 0'],
['100% 100%', '0 0'],
];
for (const [position, centre] of corners) {
layers.push([
`-webkit-radial-gradient(${centre}, circle, ${ring})`,
`radial-gradient(circle at ${centre}, ${ring})`,
`${r}px ${r}px`,
position,
]);
}
}

const list = (i: number) => layers.map((layer) => layer[i]).join(', ');
return (
`background-image: ${list(0)};` +
`background-image: ${list(1)};` +
`background-size: ${list(2)};` +
`background-position: ${list(3)};` +
'background-repeat: no-repeat;'
);
}

/**
* The colour stops of a straight side `length` long. Between rounded
* corners (which are dashes) it starts and ends with a gap: count dashes,
* count + 1 gaps. Without corners it starts and ends with a dash: count
* dashes, count - 1 gaps. '' when the side holds no dash.
*/
function strip(
length: number,
dash: number,
gap: number,
rgba: string,
betweenCorners: boolean,
): string {
if (length <= 0) return '';
if (gap <= 0) return `${rgba} 0, ${rgba} ${length}px`;
const pair = dash + gap;
const gaps = betweenCorners ? 1 : -1;
const count = Math.max(
betweenCorners ? 0 : 1,
Math.round((length - gaps * gap) / pair),
);
if (count === 0) return '';
const scale = length / (count * dash + (count + gaps) * gap);
const d = round(dash * scale);
const g = round(gap * scale);
const p = round(d + g);
return betweenCorners
? `transparent 0, transparent ${g}px, ${rgba} ${g}px, ${rgba} ${p}px`
: `${rgba} 0, ${rgba} ${d}px, transparent ${d}px, transparent ${p}px`;
}

const round = (n: number) => Math.round(n * 1000) / 1000;
15 changes: 14 additions & 1 deletion src/core/dom-renderer/domRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as lng from '@solidtv/renderer';
import { EventEmitter } from '@solidtv/renderer/utils';
import { Config } from '../config.js';
import { FontLoadOptions } from '../intrinsicTypes.js';
import { dashedBorderStyle } from './dashedBorderStyle.js';
import type {
DomRendererMainSettings,
ExtractProps,
Expand Down Expand Up @@ -542,6 +543,7 @@ function updateNodeStyles(node: DOMNode | DOMText) {

let bgStyle = '';
let borderStyle = '';
let needsDashedBorderLayer = false;
let radiusStyle = '';
let maskStyle = '';
let needsBackgroundLayer = false;
Expand Down Expand Up @@ -713,6 +715,16 @@ function updateNodeStyles(node: DOMNode | DOMText) {
}
}
}
// Dashed border: gradient strips with the props' dash and gap, in a
// layer of its own above the node's fill and below its children, so
// that it neither grows the node nor moves its children.
if (props.shader.shaderType === 'dashedBorder') {
const dashedStyle = dashedBorderStyle(props.w, props.h, shaderProps);
if (dashedStyle !== '') {
borderStyle += dashedStyle;
needsDashedBorderLayer = true;
}
}
// Rounded
if (typeof radius === 'number' && radius > 0) {
radiusStyle += `border-radius: ${radius}px;`;
Expand Down Expand Up @@ -1091,7 +1103,8 @@ function updateNodeStyles(node: DOMNode | DOMText) {

if (node.blobUrls !== null) releaseBlobUrls(node, false);

const needsSeparateBorderLayer = needsBackgroundLayer && maskStyle !== '';
const needsSeparateBorderLayer =
needsDashedBorderLayer || (needsBackgroundLayer && maskStyle !== '');

if (needsSeparateBorderLayer) {
if (node.divBorder == null) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/dom-renderer/domRendererTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as lng from '@solidtv/renderer';
import { EventEmitter } from '@solidtv/renderer/utils';
import {
ShaderBorderPrefixedProps,
ShaderDashedBorderProps,
ShaderHolePunchProps,
ShaderLinearGradientProps,
ShaderRadialGradientProps,
Expand Down Expand Up @@ -45,6 +46,7 @@ export type IRendererShaderProps = Partial<ShaderBorderPrefixedProps> &
Partial<ShaderShadowPrefixedProps> &
Partial<ShaderRoundedProps> &
Partial<ShaderHolePunchProps> &
Partial<ShaderDashedBorderProps> &
Partial<ShaderRadialGradientProps> &
Partial<ShaderLinearGradientProps>;

Expand Down
11 changes: 11 additions & 0 deletions src/core/elementNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type {
RendererMain,
INode,
AnimateProps,
DashedBorderProps,
IAnimationController,
LinearGradientProps,
RadialGradientProps,
Expand Down Expand Up @@ -1000,6 +1001,14 @@ export interface ElementNode extends RendererNode, FocusNode {
* @see https://solid-tv.github.io/solid/#/essentials/effects
*/
radialGradient?: RadialGradientProps;
/**
* Clips the element to a rounded rectangle and draws a dashed border
* inside its edge. The DOM renderer draws it with CSS gradients: dash and
* gap lengths match, and each rounded corner is drawn as one dash.
*
* @see https://solid-tv.github.io/solid/#/essentials/effects
*/
dashedBorder?: Partial<DashedBorderProps>;
/**
* The margin on the bottom side of the element for a flexItem.
*
Expand Down Expand Up @@ -3699,4 +3708,6 @@ Object.defineProperties(ElementNode.prototype, {
createRawShaderAccessor<LinearGradientProps>('linearGradient'),
radialGradient:
createRawShaderAccessor<RadialGradientProps>('radialGradient'),
dashedBorder:
createRawShaderAccessor<Partial<DashedBorderProps>>('dashedBorder'),
});
2 changes: 2 additions & 0 deletions src/core/intrinsicTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { TextNode } from './nodeTypes.js';
import { NodeStates } from './states.js';
import {
ShaderBorderProps,
ShaderDashedBorderProps,
ShaderHolePunchProps,
ShaderLinearGradientProps,
ShaderRadialGradientProps,
Expand Down Expand Up @@ -47,6 +48,7 @@ export interface Effects {
linearGradient?: Partial<ShaderLinearGradientProps>;
radialGradient?: Partial<ShaderRadialGradientProps>;
holePunch?: Partial<ShaderHolePunchProps>;
dashedBorder?: Partial<ShaderDashedBorderProps>;
shadow?: Partial<ShaderShadowProps>;
rounded?: Partial<ShaderRoundedProps>;
borderRadius?: Partial<BorderRadius>;
Expand Down
12 changes: 12 additions & 0 deletions src/core/shaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as lngr from '@solidtv/renderer';
import * as lngr_shaders from '@solidtv/renderer/webgl/shaders';

import type {
DashedBorderProps as ShaderDashedBorderProps,
HolePunchProps as ShaderHolePunchProps,
LinearGradientProps as ShaderLinearGradientProps,
RadialGradientProps as ShaderRadialGradientProps,
Expand All @@ -10,6 +11,7 @@ import type {
} from '@solidtv/renderer';
import { type WebGlShaderType as WebGlShader } from '@solidtv/renderer';
export {
ShaderDashedBorderProps,
ShaderHolePunchProps,
ShaderLinearGradientProps,
ShaderRadialGradientProps,
Expand Down Expand Up @@ -57,6 +59,7 @@ export type ShaderRoundedWithShadow = WebGlShader<ShaderRoundedWithShadowProps>;
export type ShaderRoundedWithBorderAndShadow =
WebGlShader<ShaderRoundedWithBorderAndShadowProps>;
export type ShaderHolePunch = WebGlShader<ShaderHolePunchProps>;
export type ShaderDashedBorder = WebGlShader<ShaderDashedBorderProps>;
export type ShaderRadialGradient = WebGlShader<ShaderRadialGradientProps>;
export type ShaderLinearGradient = WebGlShader<ShaderLinearGradientProps>;

Expand All @@ -68,6 +71,8 @@ export const defaultShaderRoundedWithShadow: ShaderRoundedWithShadow =
export const defaultShaderRoundedWithBorderAndShadow =
lngr_shaders.RoundedWithBorderAndShadow as ShaderRoundedWithBorderAndShadow;
export const defaultShaderHolePunch: ShaderHolePunch = lngr_shaders.HolePunch;
export const defaultShaderDashedBorder: ShaderDashedBorder =
lngr_shaders.DashedBorder;
export const defaultShaderRadialGradient: ShaderRadialGradient =
lngr_shaders.RadialGradient;
export const defaultShaderLinearGradient: ShaderLinearGradient =
Expand Down Expand Up @@ -160,6 +165,12 @@ export function registerDefaultShaderHolePunch(shManager: CoreShaderManager) {
if (SHADERS_ENABLED && !isDomRendererActive())
shManager.registerShaderType('holePunch', defaultShaderHolePunch);
}
export function registerDefaultShaderDashedBorder(
shManager: CoreShaderManager,
) {
if (SHADERS_ENABLED && !isDomRendererActive())
shManager.registerShaderType('dashedBorder', defaultShaderDashedBorder);
}
export function registerDefaultShaderRadialGradient(
shManager: CoreShaderManager,
) {
Expand All @@ -181,6 +192,7 @@ export function registerDefaultShaders(shManager: CoreShaderManager) {
registerDefaultShaderRoundedWithShadow(shManager);
registerDefaultShaderRoundedWithBorderAndShadow(shManager);
registerDefaultShaderHolePunch(shManager);
registerDefaultShaderDashedBorder(shManager);
registerDefaultShaderRadialGradient(shManager);
registerDefaultShaderLinearGradient(shManager);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/contract-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export type {
SdfFontType,
ShaderBorderPrefixedProps,
ShaderBorderProps,
ShaderDashedBorder,
ShaderDashedBorderProps,
ShaderHolePunch,
ShaderHolePunchProps,
ShaderLinearGradient,
Expand Down Expand Up @@ -239,6 +241,7 @@ export type {
ColorTextureProps,
CoreNodeRenderState,
CoreSettings,
DashedBorderProps,
Dimensions,
EdgeFadeProps,
EventEmitter,
Expand Down
Loading