Giter Site home page Giter Site logo

dr-grunt-svg-sprites's Introduction

dr-grunt-svg-sprites

Grunt plugin to create SVG sprites with PNG fallbacks at needed sizes

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install dr-grunt-svg-sprites --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('dr-grunt-svg-sprites');

The "svg-sprites" task

Overview

In your project's Gruntfile, add a section named svg-sprites to the data object passed into grunt.initConfig().

grunt.initConfig({
	"svg-sprites": {
		options: {
			// Task-specific options go here.
		},
	},
})

Options

options.spriteElementPath

Type: String

The base path of the elements to be sprited.

If set in the target options it will overwrite the global options.

If set on the uppermost options property the target name will automatically be appended to the path:

	options: {
		spriteElementPath: "img",
		// more options
	},
	shapes: {
		// more options
	}

Will result in: img/shapes.

... and a file structure like:

	img
		|- shapes
			|- circle.svg
			|- square.svg
			|- triangle.svg

... you would get the resulting sprite for shapes.

options.spritePath

Type: String

Destination path of the generated sprite images.

options.cssPath

Type: String Optional

Destination path of the generated stylesheet. If left blank only svg sprites and png fallbacks are generated.

options.prefix

Type: String Default value: '' Optional

Defines a prefix for the name of the sprite stylesheet and images and also classnames.

	shapes: {
		prefix: "test",
		// more options
	}

Would result in: test-shapes-sprite.css, test-shapes-sprite.svg and test-shapes-large-sprite.png.

options.cssSuffix

Type: String Default value: 'css' Optional

Stylesheet filetype suffix.

options.unit

Type: Number Default value: 10

Defines unit size of the grid the sprite elements snap to.

options.refSize

Type: String|Number

Defines the basic height of your source svg-elements. All other sizes will be calculated relating to this. It can either be a key from the sizes option (which refers to a number) or just a raw number.

Source elements and refSize

Notice how one source element is bigger than the refSize; this ok - as every element is scaled proportionally.

options.sizes

Type: Object

A hash of size labels and values (Number) that define the different sizes of the needed sprites.

	sizes: {
		large: 39,
		small: 13
	}

sizes

Only 1 SVG sprite is rendered and 1 PNG sprite per defined size.

Usage Examples

Basic Options

grunt.initConfig({
	"svg-sprites": {
		options: {
			spriteElementPath: "img"
			spritePath: "img/sprites",
			cssPath: "css"
		},
		shapes: {
			options: {
				sizes: {
					large: 39,
					small: 13
				},
				refSize: 26,
				unit: 13
			}
		}
	}
});

If you need to compose SVG elements you can use dr-svg-grunt-composer to preproces them before building sprites.


Changelog

0.5.0

Features:

  • Proper target support.

Changes:

  • options.paths properties have become: spriteElementPath, spritePath and cssPath.
  • options.sprites should now be configured as separate targets.

0.2.8

Bug fixes:

  • Fixes creation of fallback pngs in wrong location.

0.2.7

Features:

  • options.prefix will now affect classnames also.
  • options.refSize is now no longer limited to sizes defined in options.sizes; it can take a raw number.

dr-grunt-svg-sprites's People

Contributors

phloe avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.