Skip to content

Instantly share code, notes, and snippets.

@fabe
Last active January 18, 2023 10:21
Show Gist options
  • Save fabe/d0a38b7a5df64f20b2f899aeb5722054 to your computer and use it in GitHub Desktop.
Save fabe/d0a38b7a5df64f20b2f899aeb5722054 to your computer and use it in GitHub Desktop.
Creates an svg dot grid in React
import React from 'react';
export default ({
width = 10,
height = 10,
space = 10,
radius = 1.5,
fill = '#000',
}) => {
const viewWidth = width * radius * 2 + (width - 1) * (space - radius * 2);
const viewHeight = height * radius * 2 + (height - 1) * (space - radius * 2);
let dots = [];
for (let indexWidth = 0; indexWidth < width; indexWidth++) {
for (let indexHeight = 0; indexHeight < height; indexHeight++) {
dots.push(
<circle
key={`${indexWidth}-${indexHeight}`}
cx={radius + indexWidth * space}
cy={radius + indexHeight * space}
r={radius}
/>
);
}
}
return (
<svg
width={viewWidth}
height={viewHeight}
viewBox={`0 0 ${viewWidth} ${viewHeight}`}
version="1.1"
>
<g fill={fill}>{dots}</g>
</svg>
);
};
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment