refactor(background): use repeating inline svg as bg pattern #356

This commit is contained in:
moklick
2020-07-25 15:06:58 +02:00
parent 3da7efae35
commit 0d2bdf7ce8
3 changed files with 22 additions and 39 deletions

View File

@@ -21,8 +21,6 @@ const defaultColors = {
const Background = memo(
({ variant = BackgroundVariant.Dots, gap = 24, size = 0.5, color, style, className }: BackgroundProps) => {
const width = useStoreState((s) => s.width);
const height = useStoreState((s) => s.height);
const [x, y, scale] = useStoreState((s) => s.transform);
const bgClasses = cc(['react-flow__background', className]);
@@ -32,15 +30,22 @@ const Background = memo(
const yOffset = y % scaledGap;
const isLines = variant === BackgroundVariant.Lines;
const path = isLines
? createGridLinesPath(width, height, xOffset, yOffset, scaledGap)
: createGridDotsPath(width, height, xOffset, yOffset, scaledGap, size);
? createGridLinesPath(xOffset, yOffset, scaledGap)
: createGridDotsPath(xOffset, yOffset, scaledGap, size);
const fill = isLines ? 'none' : bgColor;
const stroke = isLines ? bgColor : 'none';
const bg = `<svg width="${scaledGap + size}" height="${scaledGap + size}" xmlns='http://www.w3.org/2000/svg' ${
typeof style !== 'undefined' ? `style="${style}"` : ''
}><path fill="${fill}" stroke="${stroke}" strokeWidth="${size}" d="${path}" /></svg>`;
return (
<svg width={width} height={height} style={style} className={bgClasses}>
<path fill={fill} stroke={stroke} strokeWidth={size} d={path} />
</svg>
<div
className={bgClasses}
style={{
backgroundImage: `url("data:image/svg+xml;utf8,${encodeURIComponent(bg)}")`,
}}
></div>
);
}
);

View File

@@ -2,4 +2,6 @@
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

View File

@@ -1,37 +1,13 @@
export const createGridLinesPath = (
width: number,
height: number,
xOffset: number,
yOffset: number,
gap: number
): string => {
const lineCountX = Math.ceil(width / gap) + 1;
const lineCountY = Math.ceil(height / gap) + 1;
export const createGridLinesPath = (xOffset: number, yOffset: number, scaledGap: number): string => {
const x = xOffset < 0 ? scaledGap + xOffset : xOffset;
const y = yOffset < 0 ? scaledGap + yOffset : yOffset;
const xValues = Array.from({ length: lineCountX }, (_, i) => `M${i * gap + xOffset} 0 V${height}`);
const yValues = Array.from({ length: lineCountY }, (_, i) => `M0 ${i * gap + yOffset} H${width}`);
return [...xValues, ...yValues].join(' ');
return `M${x} 0 V${scaledGap} M0 ${y} H${scaledGap}`;
};
export const createGridDotsPath = (
width: number,
height: number,
xOffset: number,
yOffset: number,
gap: number,
size: number
): string => {
const lineCountX = Math.ceil(width / gap) + 1;
const lineCountY = Math.ceil(height / gap) + 1;
export const createGridDotsPath = (xOffset: number, yOffset: number, scaledGap: number, size: number): string => {
const x = xOffset < 0 ? scaledGap + xOffset : xOffset;
const y = yOffset < 0 ? scaledGap + yOffset : yOffset;
const values = Array.from({ length: lineCountX }, (_, col) => {
const x = col * gap + xOffset;
return Array.from({ length: lineCountY }, (_, row) => {
const y = row * gap + yOffset;
return `M${x} ${y - size} l${size} ${size} l${-size} ${size} l${-size} ${-size}z`;
}).join(' ');
});
return values.join(' ');
return `M${x} ${y - size} l${size} ${size} l${-size} ${size} l${-size} ${-size}z`;
};