Last active
January 18, 2023 10:21
-
-
Save fabe/d0a38b7a5df64f20b2f899aeb5722054 to your computer and use it in GitHub Desktop.
Creates an svg dot grid in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment