@splendidlabz/svelte 4.12.1 → 4.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
// This is the Svelte twin of packages/astro/components/SVG/Displace.astro.
|
|
3
|
+
// Same component, same props, same output — one for each framework. Keep them in sync.
|
|
4
|
+
|
|
5
|
+
import { displaceSettings } from '@splendidlabz/utils'
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
class: klass = '',
|
|
9
|
+
preset = 'wet-ink',
|
|
10
|
+
type,
|
|
11
|
+
frequency,
|
|
12
|
+
octaves,
|
|
13
|
+
scale,
|
|
14
|
+
seed,
|
|
15
|
+
animate = false,
|
|
16
|
+
duration,
|
|
17
|
+
margin = 30,
|
|
18
|
+
children,
|
|
19
|
+
...rest
|
|
20
|
+
} = $props()
|
|
21
|
+
|
|
22
|
+
// Warps its children by reading a noise field as a per-pixel x/y offset.
|
|
23
|
+
// Unlike Noise, the noise is never drawn — only the displacement it produces.
|
|
24
|
+
//
|
|
25
|
+
// preset — supplies defaults for everything below.
|
|
26
|
+
// wet-ink, letterpress, heat-haze, bad-tape, shred
|
|
27
|
+
//
|
|
28
|
+
// frequency — wavelength of the distortion, roughly (1 / frequency) px.
|
|
29
|
+
// Pass [x, y] for directional distortion.
|
|
30
|
+
//
|
|
31
|
+
// scale — how far each pixel moves. Keep it under a quarter of the
|
|
32
|
+
// wavelength to warp rather than shred.
|
|
33
|
+
//
|
|
34
|
+
// animate — emits SMIL, so it runs with no JS. Only does anything on
|
|
35
|
+
// presets carrying animateTo.
|
|
36
|
+
//
|
|
37
|
+
// margin — filter region expansion in %. Too low and displaced pixels clip.
|
|
38
|
+
|
|
39
|
+
// Displacement has to hit real DOM, so unlike Noise it can't inline itself
|
|
40
|
+
// as a data URI — every instance needs its own filter id in the document.
|
|
41
|
+
const uid = $props.id()
|
|
42
|
+
const id = `displace-${uid}`
|
|
43
|
+
|
|
44
|
+
const fx = $derived(
|
|
45
|
+
displaceSettings({ preset, type, frequency, octaves, scale, seed, duration }),
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
const offset = $derived(-margin)
|
|
49
|
+
const size = $derived(100 + margin * 2)
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<span style:filter={`url(#${id})`} class="displace {klass}" {...rest}>
|
|
53
|
+
{@render children?.()}
|
|
54
|
+
</span>
|
|
55
|
+
|
|
56
|
+
<svg class="displace-defs" aria-hidden="true" focusable="false">
|
|
57
|
+
<filter
|
|
58
|
+
{id}
|
|
59
|
+
color-interpolation-filters="sRGB"
|
|
60
|
+
height="{size}%"
|
|
61
|
+
width="{size}%"
|
|
62
|
+
x="{offset}%"
|
|
63
|
+
y="{offset}%"
|
|
64
|
+
>
|
|
65
|
+
<feTurbulence
|
|
66
|
+
baseFrequency={fx.frequency}
|
|
67
|
+
numOctaves={fx.octaves}
|
|
68
|
+
result="noise"
|
|
69
|
+
seed={fx.seed}
|
|
70
|
+
type={fx.type}
|
|
71
|
+
>
|
|
72
|
+
{#if animate && fx.animateTo}
|
|
73
|
+
<animate
|
|
74
|
+
attributeName="baseFrequency"
|
|
75
|
+
dur="{fx.duration}s"
|
|
76
|
+
repeatCount="indefinite"
|
|
77
|
+
values="{fx.frequency};{fx.animateTo};{fx.frequency}"
|
|
78
|
+
/>
|
|
79
|
+
{/if}
|
|
80
|
+
</feTurbulence>
|
|
81
|
+
<feDisplacementMap
|
|
82
|
+
in="SourceGraphic"
|
|
83
|
+
in2="noise"
|
|
84
|
+
scale={fx.scale}
|
|
85
|
+
xChannelSelector="R"
|
|
86
|
+
yChannelSelector="G"
|
|
87
|
+
/>
|
|
88
|
+
</filter>
|
|
89
|
+
</svg>
|
|
90
|
+
|
|
91
|
+
<style>
|
|
92
|
+
.displace {
|
|
93
|
+
display: inline-block;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.displace-defs {
|
|
97
|
+
position: absolute;
|
|
98
|
+
width: 0;
|
|
99
|
+
height: 0;
|
|
100
|
+
overflow: hidden;
|
|
101
|
+
}
|
|
102
|
+
</style>
|
|
@@ -1,37 +1,33 @@
|
|
|
1
1
|
<script>
|
|
2
|
+
// This is the Svelte twin of packages/astro/components/SVG/Noise.astro.
|
|
3
|
+
// Same component, same props, same output — one for each framework. Keep them in sync.
|
|
4
|
+
|
|
5
|
+
import { noiseColorMatrix } from '@splendidlabz/utils'
|
|
6
|
+
|
|
2
7
|
let {
|
|
3
8
|
class: klass = '',
|
|
4
9
|
frequency = 0.9,
|
|
5
|
-
|
|
6
|
-
alpha = 0.6,
|
|
7
|
-
opacity = 0.18,
|
|
10
|
+
strength = 0.1,
|
|
8
11
|
blend = 'overlay',
|
|
9
|
-
|
|
12
|
+
color = 'rainbow',
|
|
10
13
|
} = $props()
|
|
11
14
|
|
|
12
15
|
// frequency — how zoomed in the noise is.
|
|
13
16
|
// low (0.01–0.1): large blobs, clouds, smoke
|
|
14
17
|
// high (0.5–1): fine grain, film noise, static
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// frequency: 0.05, octaves: 6 → realistic cloudy smoke
|
|
23
|
-
// frequency: 0.9, octaves: 2 → subtle film grain overlay (default)
|
|
24
|
-
// frequency: 0.3, octaves: 4 → mid-range crumpled paper texture
|
|
25
|
-
|
|
26
|
-
// seed — omit to let the browser pick. Pass a number to lock noise across
|
|
27
|
-
// renders or to get a different pattern per instance.
|
|
28
|
-
const seedAttr = $derived(seed != null ? ` seed='${seed}'` : '')
|
|
18
|
+
|
|
19
|
+
// color — 'rainbow' (default) for coloured static. A preset name
|
|
20
|
+
// ('black', 'sepia', 'synthwave') or any hex for a single-colour tint.
|
|
21
|
+
|
|
22
|
+
// strength — 0–1 fades the grain; above 1 it saturates into denser grain.
|
|
23
|
+
// Need the whole overlay dimmer? Add CSS opacity via class.
|
|
24
|
+
const matrix = $derived(noiseColorMatrix(color, strength))
|
|
29
25
|
|
|
30
26
|
const svg = $derived(
|
|
31
27
|
"<svg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'>" +
|
|
32
28
|
"<filter id='n'>" +
|
|
33
|
-
`<feTurbulence type='fractalNoise' baseFrequency='${frequency}' numOctaves='
|
|
34
|
-
`<feColorMatrix values='
|
|
29
|
+
`<feTurbulence type='fractalNoise' baseFrequency='${frequency}' numOctaves='2' stitchTiles='stitch'/>` +
|
|
30
|
+
`<feColorMatrix values='${matrix}'/>` +
|
|
35
31
|
'</filter>' +
|
|
36
32
|
"<rect width='100%' height='100%' filter='url(%23n)'/>" +
|
|
37
33
|
'</svg>',
|
|
@@ -42,7 +38,6 @@
|
|
|
42
38
|
|
|
43
39
|
<div
|
|
44
40
|
style:background-image={bg}
|
|
45
|
-
style:opacity
|
|
46
41
|
style:mix-blend-mode={blend}
|
|
47
42
|
class="Noise {klass}"
|
|
48
43
|
aria-hidden="true"
|
|
@@ -8,17 +8,18 @@
|
|
|
8
8
|
* @component
|
|
9
9
|
* @example
|
|
10
10
|
* <!-- Renders /src/svg/logo.svg -->
|
|
11
|
-
* <SVG
|
|
11
|
+
* <SVG name="logo" class="w-6 h-6" />
|
|
12
12
|
* @example
|
|
13
13
|
* <!-- Use DOM parser when the SVG needs runtime DOM APIs -->
|
|
14
|
-
* <SVG
|
|
15
|
-
* @throws {Error} icon
|
|
14
|
+
* <SVG name="spinner" parser="dom" style={{ color: 'rebeccapurple' }} />
|
|
15
|
+
* @throws {Error} When both name and icon are missing
|
|
16
16
|
* @throws {Error} When the requested SVG file cannot be found
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* @typedef {object} SVGProps
|
|
21
|
-
* @property {string}
|
|
21
|
+
* @property {string} [name] - Basename of the SVG file (without .svg)
|
|
22
|
+
* @property {string} [icon] - Alias of name. Use whichever reads better
|
|
22
23
|
* @property {object|string} [style] - Inline styles. Object is converted with `toStyleString`
|
|
23
24
|
* @property {"naive"|"dom"} [parser="naive"] - Parsing strategy
|
|
24
25
|
* @property {*} [children] - Ignored; component does not support slots
|
|
@@ -30,12 +31,15 @@
|
|
|
30
31
|
/** @type {SVGProps} */
|
|
31
32
|
const {
|
|
32
33
|
icon,
|
|
34
|
+
name,
|
|
33
35
|
style,
|
|
34
36
|
parser = 'naive', // or DOM
|
|
35
37
|
children, // Just to remove children slots
|
|
36
38
|
...rest
|
|
37
39
|
} = $props()
|
|
38
40
|
|
|
41
|
+
const basename = name || icon
|
|
42
|
+
|
|
39
43
|
const files = {
|
|
40
44
|
...import.meta.glob(`/src/svg/**/*.svg`, { eager: true, query: 'raw' }),
|
|
41
45
|
...import.meta.glob(`/src/svgs/**/*.svg`, { eager: true, query: 'raw' }),
|
|
@@ -43,9 +47,11 @@
|
|
|
43
47
|
|
|
44
48
|
// Find the SVG
|
|
45
49
|
const file = init(() => {
|
|
46
|
-
if (!
|
|
47
|
-
const found = Object.keys(files).find(
|
|
48
|
-
|
|
50
|
+
if (!basename) throw new Error('name (or icon) property is required')
|
|
51
|
+
const found = Object.keys(files).find(
|
|
52
|
+
f => f.split('/').pop() === `${basename}.svg`,
|
|
53
|
+
)
|
|
54
|
+
if (!found) throw new Error(`${basename}.svg not found`)
|
|
49
55
|
return found
|
|
50
56
|
})
|
|
51
57
|
|
|
@@ -77,19 +83,20 @@
|
|
|
77
83
|
Inline SVG renderer for files under `/src/svg` or `/src/svgs`.
|
|
78
84
|
|
|
79
85
|
- Props:
|
|
80
|
-
-
|
|
86
|
+
- name (string): Basename of the SVG file (without .svg)
|
|
87
|
+
- icon (string): Alias of name. Use whichever reads better
|
|
81
88
|
- style (object|string, optional): Inline styles; objects are converted with `toStyleString`
|
|
82
89
|
- parser ("naive"|"dom", optional, default "naive"): Parsing strategy
|
|
83
90
|
- Slots: none
|
|
84
91
|
- Events: none
|
|
85
92
|
- Errors:
|
|
86
|
-
- Throws if `icon`
|
|
93
|
+
- Throws if both `name` and `icon` are missing
|
|
87
94
|
- Throws if the SVG file cannot be found
|
|
88
95
|
|
|
89
96
|
Usage:
|
|
90
97
|
```svelte
|
|
91
|
-
<SVG
|
|
92
|
-
<SVG
|
|
98
|
+
<SVG name="logo" class="w-6 h-6" />
|
|
99
|
+
<SVG name="spinner" parser="dom" style={{ color: 'rebeccapurple' }} />
|
|
93
100
|
```
|
|
94
101
|
-->
|
|
95
102
|
|
package/components/SVG/index.js
CHANGED
package/components/SVG/parser.js
CHANGED
|
@@ -46,10 +46,10 @@ export function naiveParser(content, options) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// If width/height exist but viewBox doesn't, create viewBox
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
finalViewBox = `0 0 ${
|
|
49
|
+
// from the file's own dimensions — a width/height option only
|
|
50
|
+
// changes the display size, not the coordinate system.
|
|
51
|
+
if (!currentViewBox && currentWidth && currentHeight) {
|
|
52
|
+
finalViewBox = `0 0 ${currentWidth} ${currentHeight}`
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// Apply width if we have a final value
|