Skip to content

Commit 1f49358

Browse files
committed
feat(background): add offset prop
Signed-off-by: braks <[email protected]>
1 parent e70ebc2 commit 1f49358

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

packages/background/src/Background.vue

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,53 @@
22
import { useVueFlow } from '@vue-flow/core'
33
import { BackgroundVariant } from './types'
44
import type { BackgroundProps } from './types'
5+
import { LinePattern } from './patterns'
56
67
const {
8+
id,
79
variant = 'dots' as BackgroundVariant,
810
gap = 10,
9-
size = 0.4,
11+
size = 1,
12+
lineWidth = 1,
1013
height = 100,
1114
width = 100,
1215
x = 0,
1316
y = 0,
1417
bgColor,
1518
patternColor: initialPatternColor,
19+
offset = 2,
1620
} = defineProps<BackgroundProps>()
1721
1822
const defaultColors: Record<BackgroundVariant, string> = {
1923
[BackgroundVariant.Dots]: '#81818a',
2024
[BackgroundVariant.Lines]: '#eee',
2125
}
2226
23-
const { id, viewport } = useVueFlow()
27+
const { id: vueFlowId, viewport } = useVueFlow()
28+
29+
const gapXY = computed(() => (Array.isArray(gap) ? gap : [gap, gap]))
2430
2531
const background = $computed(() => {
26-
const scaledGap = gap && gap * viewport.value.zoom
27-
const xOffset = scaledGap && viewport.value.x % scaledGap
28-
const yOffset = scaledGap && viewport.value.y % scaledGap
29-
const bgSize = size * viewport.value.zoom
32+
const scaledGap = [gapXY.value[0] * viewport.value.zoom || 1, gapXY.value[1] * viewport.value.zoom || 1]
33+
34+
const scaledSize = size * viewport.value.zoom
35+
36+
const patternOffset =
37+
variant === BackgroundVariant.Dots
38+
? [scaledSize / offset, scaledSize / offset]
39+
: [scaledGap[0] / offset, scaledGap[1] / offset]
3040
3141
return {
3242
scaledGap,
33-
xOffset,
34-
yOffset,
35-
size: bgSize,
43+
offset: patternOffset,
44+
size: scaledSize,
3645
}
3746
})
3847
3948
// when there are multiple flows on a page we need to make sure that every background gets its own pattern.
40-
const patternId = `pattern-${id}`
49+
const patternId = computed(() => `pattern-${vueFlowId}${id ? `-${id}` : ''}`)
4150
4251
const patternColor = computed(() => initialPatternColor || defaultColors[variant || BackgroundVariant.Dots])
43-
44-
const d = computed(
45-
() => `M${background.scaledGap / 2} 0 V${background.scaledGap} M0 ${background.scaledGap / 2} H${background.scaledGap}`,
46-
)
4752
</script>
4853

4954
<script lang="ts">
@@ -61,22 +66,24 @@ export default {
6166
width: `${width > 100 ? 100 : width}%`,
6267
}"
6368
>
69+
<!-- todo: rename to `pattern -->
6470
<slot :id="patternId" name="pattern-container">
6571
<pattern
6672
:id="patternId"
67-
:x="background.xOffset"
68-
:y="background.yOffset"
69-
:width="background.scaledGap"
70-
:height="background.scaledGap"
73+
:x="viewport.x % background.scaledGap[0]"
74+
:y="viewport.y % background.scaledGap[1]"
75+
:width="background.scaledGap[0]"
76+
:height="background.scaledGap[1]"
77+
:patternTransform="`translate(-${background.offset[0]},-${background.offset[1]})`"
7178
patternUnits="userSpaceOnUse"
7279
>
7380
<slot name="pattern">
7481
<template v-if="variant === BackgroundVariant.Lines">
75-
<path :stroke="patternColor" :stroke-width="size" :d="d" />
82+
<LinePattern :size="lineWidth" :color="patternColor" :dimensions="background.size" />
7683
</template>
7784

7885
<template v-else-if="variant === BackgroundVariant.Dots">
79-
<circle :cx="background.size" :cy="background.size" :r="background.size" :fill="patternColor" />
86+
<DotPattern :color="patternColor" :radius="background.size / offset" />
8087
</template>
8188

8289
<svg v-if="bgColor" height="100" width="100">

packages/background/src/patterns.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { FunctionalComponent } from 'vue'
2+
import { BackgroundVariant } from './types'
3+
4+
interface LinePatternProps {
5+
dimensions: [number, number]
6+
size?: number
7+
color: string
8+
}
9+
10+
export const LinePattern: FunctionalComponent<LinePatternProps> = ({ dimensions, size, color }) => {
11+
return () =>
12+
h('path', {
13+
'stroke': color,
14+
'stroke-width': size,
15+
'd': `M${dimensions[0] / 2} 0 V${dimensions[1]} M0 ${dimensions[1] / 2} H${dimensions[0]}`,
16+
})
17+
}
18+
19+
interface DotPatternProps {
20+
radius: number
21+
color: string
22+
}
23+
24+
export const DotPattern: FunctionalComponent<DotPatternProps> = ({ radius, color }) => {
25+
return () => h('circle', { cx: radius, cy: radius, r: radius, fill: color })
26+
}
27+
28+
export const Patterns = {
29+
[BackgroundVariant.Lines]: LinePattern,
30+
[BackgroundVariant.Dots]: DotPattern,
31+
}

packages/background/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ export enum BackgroundVariant {
44
}
55

66
export interface BackgroundProps {
7+
id?: string
78
/** The background pattern variant, {@link BackgroundVariant} */
89
variant?: BackgroundVariant
910
/** Background pattern gap */
10-
gap?: number
11+
gap?: number | number[]
1112
/** Background pattern size */
1213
size?: number
14+
lineWidth?: number
1315
/** Background pattern color */
1416
patternColor?: string
1517
/** Background color */
@@ -22,4 +24,6 @@ export interface BackgroundProps {
2224
x?: number
2325
/** Background y-coordinate (offset y) */
2426
y?: number
27+
/** Background offset */
28+
offset?: number
2529
}

0 commit comments

Comments
 (0)