feat(background): add offset prop
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -2,17 +2,21 @@
|
|||||||
import { useVueFlow } from '@vue-flow/core'
|
import { useVueFlow } from '@vue-flow/core'
|
||||||
import { BackgroundVariant } from './types'
|
import { BackgroundVariant } from './types'
|
||||||
import type { BackgroundProps } from './types'
|
import type { BackgroundProps } from './types'
|
||||||
|
import { LinePattern } from './patterns'
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
id,
|
||||||
variant = 'dots' as BackgroundVariant,
|
variant = 'dots' as BackgroundVariant,
|
||||||
gap = 10,
|
gap = 10,
|
||||||
size = 0.4,
|
size = 1,
|
||||||
|
lineWidth = 1,
|
||||||
height = 100,
|
height = 100,
|
||||||
width = 100,
|
width = 100,
|
||||||
x = 0,
|
x = 0,
|
||||||
y = 0,
|
y = 0,
|
||||||
bgColor,
|
bgColor,
|
||||||
patternColor: initialPatternColor,
|
patternColor: initialPatternColor,
|
||||||
|
offset = 2,
|
||||||
} = defineProps<BackgroundProps>()
|
} = defineProps<BackgroundProps>()
|
||||||
|
|
||||||
const defaultColors: Record<BackgroundVariant, string> = {
|
const defaultColors: Record<BackgroundVariant, string> = {
|
||||||
@@ -20,30 +24,31 @@ const defaultColors: Record<BackgroundVariant, string> = {
|
|||||||
[BackgroundVariant.Lines]: '#eee',
|
[BackgroundVariant.Lines]: '#eee',
|
||||||
}
|
}
|
||||||
|
|
||||||
const { id, viewport } = useVueFlow()
|
const { id: vueFlowId, viewport } = useVueFlow()
|
||||||
|
|
||||||
|
const gapXY = computed(() => (Array.isArray(gap) ? gap : [gap, gap]))
|
||||||
|
|
||||||
const background = $computed(() => {
|
const background = $computed(() => {
|
||||||
const scaledGap = gap && gap * viewport.value.zoom
|
const scaledGap = [gapXY.value[0] * viewport.value.zoom || 1, gapXY.value[1] * viewport.value.zoom || 1]
|
||||||
const xOffset = scaledGap && viewport.value.x % scaledGap
|
|
||||||
const yOffset = scaledGap && viewport.value.y % scaledGap
|
const scaledSize = size * viewport.value.zoom
|
||||||
const bgSize = size * viewport.value.zoom
|
|
||||||
|
const patternOffset =
|
||||||
|
variant === BackgroundVariant.Dots
|
||||||
|
? [scaledSize / offset, scaledSize / offset]
|
||||||
|
: [scaledGap[0] / offset, scaledGap[1] / offset]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
scaledGap,
|
scaledGap,
|
||||||
xOffset,
|
offset: patternOffset,
|
||||||
yOffset,
|
size: scaledSize,
|
||||||
size: bgSize,
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// when there are multiple flows on a page we need to make sure that every background gets its own pattern.
|
// when there are multiple flows on a page we need to make sure that every background gets its own pattern.
|
||||||
const patternId = `pattern-${id}`
|
const patternId = computed(() => `pattern-${vueFlowId}${id ? `-${id}` : ''}`)
|
||||||
|
|
||||||
const patternColor = computed(() => initialPatternColor || defaultColors[variant || BackgroundVariant.Dots])
|
const patternColor = computed(() => initialPatternColor || defaultColors[variant || BackgroundVariant.Dots])
|
||||||
|
|
||||||
const d = computed(
|
|
||||||
() => `M${background.scaledGap / 2} 0 V${background.scaledGap} M0 ${background.scaledGap / 2} H${background.scaledGap}`,
|
|
||||||
)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
@@ -61,22 +66,24 @@ export default {
|
|||||||
width: `${width > 100 ? 100 : width}%`,
|
width: `${width > 100 ? 100 : width}%`,
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
|
<!-- todo: rename to `pattern -->
|
||||||
<slot :id="patternId" name="pattern-container">
|
<slot :id="patternId" name="pattern-container">
|
||||||
<pattern
|
<pattern
|
||||||
:id="patternId"
|
:id="patternId"
|
||||||
:x="background.xOffset"
|
:x="viewport.x % background.scaledGap[0]"
|
||||||
:y="background.yOffset"
|
:y="viewport.y % background.scaledGap[1]"
|
||||||
:width="background.scaledGap"
|
:width="background.scaledGap[0]"
|
||||||
:height="background.scaledGap"
|
:height="background.scaledGap[1]"
|
||||||
|
:patternTransform="`translate(-${background.offset[0]},-${background.offset[1]})`"
|
||||||
patternUnits="userSpaceOnUse"
|
patternUnits="userSpaceOnUse"
|
||||||
>
|
>
|
||||||
<slot name="pattern">
|
<slot name="pattern">
|
||||||
<template v-if="variant === BackgroundVariant.Lines">
|
<template v-if="variant === BackgroundVariant.Lines">
|
||||||
<path :stroke="patternColor" :stroke-width="size" :d="d" />
|
<LinePattern :size="lineWidth" :color="patternColor" :dimensions="background.size" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<template v-else-if="variant === BackgroundVariant.Dots">
|
<template v-else-if="variant === BackgroundVariant.Dots">
|
||||||
<circle :cx="background.size" :cy="background.size" :r="background.size" :fill="patternColor" />
|
<DotPattern :color="patternColor" :radius="background.size / offset" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<svg v-if="bgColor" height="100" width="100">
|
<svg v-if="bgColor" height="100" width="100">
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import type { FunctionalComponent } from 'vue'
|
||||||
|
import { BackgroundVariant } from './types'
|
||||||
|
|
||||||
|
interface LinePatternProps {
|
||||||
|
dimensions: [number, number]
|
||||||
|
size?: number
|
||||||
|
color: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const LinePattern: FunctionalComponent<LinePatternProps> = ({ dimensions, size, color }) => {
|
||||||
|
return () =>
|
||||||
|
h('path', {
|
||||||
|
'stroke': color,
|
||||||
|
'stroke-width': size,
|
||||||
|
'd': `M${dimensions[0] / 2} 0 V${dimensions[1]} M0 ${dimensions[1] / 2} H${dimensions[0]}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DotPatternProps {
|
||||||
|
radius: number
|
||||||
|
color: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DotPattern: FunctionalComponent<DotPatternProps> = ({ radius, color }) => {
|
||||||
|
return () => h('circle', { cx: radius, cy: radius, r: radius, fill: color })
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Patterns = {
|
||||||
|
[BackgroundVariant.Lines]: LinePattern,
|
||||||
|
[BackgroundVariant.Dots]: DotPattern,
|
||||||
|
}
|
||||||
@@ -4,12 +4,14 @@ export enum BackgroundVariant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface BackgroundProps {
|
export interface BackgroundProps {
|
||||||
|
id?: string
|
||||||
/** The background pattern variant, {@link BackgroundVariant} */
|
/** The background pattern variant, {@link BackgroundVariant} */
|
||||||
variant?: BackgroundVariant
|
variant?: BackgroundVariant
|
||||||
/** Background pattern gap */
|
/** Background pattern gap */
|
||||||
gap?: number
|
gap?: number | number[]
|
||||||
/** Background pattern size */
|
/** Background pattern size */
|
||||||
size?: number
|
size?: number
|
||||||
|
lineWidth?: number
|
||||||
/** Background pattern color */
|
/** Background pattern color */
|
||||||
patternColor?: string
|
patternColor?: string
|
||||||
/** Background color */
|
/** Background color */
|
||||||
@@ -22,4 +24,6 @@ export interface BackgroundProps {
|
|||||||
x?: number
|
x?: number
|
||||||
/** Background y-coordinate (offset y) */
|
/** Background y-coordinate (offset y) */
|
||||||
y?: number
|
y?: number
|
||||||
|
/** Background offset */
|
||||||
|
offset?: number
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user