react-svg-spritesheet
Version:
use trimmed or polygon packed sprites without webGL/canvas 2d in the dom
56 lines (36 loc) • 1.8 kB
Markdown
or polygon packed sprites without webGL/canvas 2d in the dom
Spritesheets are great for saving resources, but the usual css/dom implementations don't allow for nicer saving measures like white space trimming (without affecting sizing/positioning) or polygon packing, and sizing images independently of the sprites resolution also isn't easy
Using svgs with clip-paths meakes this possible, without having to use webGL or canvas 2D
```
npm i react-svg-spriteheet
```
create your spritesheet with [TexturePacker](https://www.codeandweb.com/texturepacker) as `JSON (Hash)` or [spritesheet.thisotherthing.co](https://spritesheet.thisotherthing.co/) with format `hash`
it should be set to either trimmed rects or polygons and without rotation
```javascript
import React from "react";
import SvgSprite from "react-svg-spriteheet";
import spritesheet from "../public/spritesheet.json";
export default () => (
<div>
<SvgSprite spritesheet={spritesheet} sprite="26" image="/spritesheet.png" width="120px" />
</div>
);
```
imported or loaded and parsed [TexturePacker](https://www.codeandweb.com/texturepacker) json Hash or [spritesheet.thisotherthing.co](https://spritesheet.thisotherthing.co/) spritesheet json (using either rects or polygons)
the name of the sprite, as saved in the spritesheet
url to the generated spritesheet image
css string for the width of the svg
css string for the height of the svg
run `npm run dev`, this should start the server at `localhost:3000` and have nice auto reloading with react/next.js
use trimmed