Skip to content

Simple Svelte and React Examples #118

@johnathanludwig

Description

@johnathanludwig

Hi, I've been using this package and it has been great. I just wanted to provide a simple svelte and react examples in case you wanted to add more generic examples to the recipe documentation.

Svelte

https://svelte.dev/repl/61514fb55a1b40be84cd731028435498?version=4.2.19

<script>
	import { defineHex, Grid, rectangle } from 'honeycomb-grid'

	const Hex = defineHex({ dimensions: 30, origin: 'topLeft' })
	const grid = new Grid(Hex, rectangle({ width: 8, height: 5 }))
</script>


<svg viewBox="-1 0 500 500">
  {#each grid as hex}
		<polygon 
			points={hex.corners.map(({ x, y }) => `${x},${y}`)}
			stroke-width="1"
			stroke="#000"
			fill="none"
			/>
	{/each}
</svg>

React

https://stackblitz.com/edit/vitejs-vite-evbazp?file=src%2FApp.tsx

import { defineHex, Grid, rectangle } from 'honeycomb-grid';

const Hex = defineHex({ dimensions: 30, origin: 'topLeft' });
const grid = new Grid(Hex, rectangle({ width: 8, height: 5 }));

function App() {
  return (
    <>
      <svg viewBox="-1 0 500 500">
        {grid.toArray().map((hex, index) => {
          return (
            <polygon
              key={index}
              points={hex.corners.map(({ x, y }) => `${x},${y}`).join(' ')}
              strokeWidth="1"
              stroke="#000"
              fill="none"
            />
          );
        })}
      </svg>
    </>
  );
}

export default App;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions