Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG Gradient Generation Support #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/nib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
var stylus = require('stylus')
, nodes = stylus.nodes
, utils = stylus.utils
, gradient
, gradient = require('./nodes/gradient')
, Canvas;

exports = module.exports = plugin;

// conditionally expose gradient api

try {
require('canvas');
gradient = require('./nodes/gradient');
Canvas = require('canvas');
} catch (err) {
// ignore
}
Expand All @@ -48,10 +45,13 @@ exports.path = __dirname;
function plugin() {
return function(style){
style.include(__dirname);
if (gradient) {
style.define('create-gradient-image', gradient.create)
style.define('create-gradient-image', gradient.create)
style.define('gradient-svg-data-uri', gradient.svgDataURL)
style.define('add-color-stop', gradient.addColorStop)
style.define('set-gradient-size', gradient.setSize)

if (Canvas) {
style.define('gradient-data-uri', gradient.dataURL)
style.define('add-color-stop', gradient.addColorStop)
style.define('has-canvas', nodes.true);
} else {
style.define('has-canvas', nodes.false);
Expand Down
6 changes: 6 additions & 0 deletions lib/nib/config.styl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ support-for-ie = true
*/

vendor-prefixes ?= webkit moz official

/*
* Default vendor prefixes.
*/

gradient-formats = png svg css
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?=

only assigns if not already defined

82 changes: 52 additions & 30 deletions lib/nib/gradients.styl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ join-stops(stops, translate)
str += translate(color, pos)
unquote(str)


/*
* Build a linear gradient object with the given start position
* and variable number of color stops.
*/

build-gradient(start, stops)
stops = stops[0] if length(stops) == 1
if start[0] is a 'unit'
size = start[0]
start = start[1] or 'top'
else
start = start[0]
grad = create-gradient-image(start)
set-gradient-size(grad, size) if size is a 'unit'
stops = normalize-stops(stops)
add-color-stop(grad, stop[0], stop[1]) for stop in stops
grad

/*
* Legacy Webkit color stop.
*/
Expand Down Expand Up @@ -101,46 +120,49 @@ std-stop(color, pos)
*
*/

linear-gradient(start, stops...)
linear-gradient(start, stops..., formats=gradient-formats)
error('color stops required') unless length(stops)
prop = current-property[0]
val = current-property[1]
stops = normalize-stops(stops)

// gradient image
if start[0] is a 'unit'
if has-canvas
img = linear-gradient-image(start, stops)
add-property(prop, replace(val, '__CALL__', img))
start = start[1]

// legacy webkit
end = grad-point(opposite-position(start))
webkit-legacy = '-webkit-gradient(linear, %s, %s, %s)' % (grad-point(start) end join-stops(stops, webkit-stop))
add-property(prop, replace(val, '__CALL__', webkit-legacy))

// vendor prefixed
stops = join-stops(stops, std-stop)
for prefix in vendor-prefixes
unless prefix == official
gradient = '-%s-linear-gradient(%s, %s)' % (prefix start stops)
add-property(prop, replace(val, '__CALL__', gradient))

// standard
'linear-gradient(%s, %s)' % (start stops)

// gradient png image
if png in formats and start[0] is a 'unit'
img = linear-gradient-image(start, stops)
add-property(prop, replace(val, '__CALL__', img))

// gradient svg image
if svg in formats
img = linear-gradient-svg(start, stops)
add-property(prop, replace(val, '__CALL__', img))

if css in formats
start = start[1] if start[0] is a 'unit'
// legacy webkit
end = grad-point(opposite-position(start))
webkit-legacy = '-webkit-gradient(linear, %s, %s, %s)' % (grad-point(start) end join-stops(stops, webkit-stop))
add-property(prop, replace(val, '__CALL__', webkit-legacy))

// vendor prefixed
stops = join-stops(stops, std-stop)
for prefix in vendor-prefixes
unless prefix == official
gradient = '-%s-linear-gradient(%s, %s)' % (prefix start stops)
add-property(prop, replace(val, '__CALL__', gradient))

// standard
'linear-gradient(%s, %s)' % (start stops)

/*
* Create a linear gradient image with the given start position
* and variable number of color stops.
*/

linear-gradient-image(start, stops...)
error('node-canvas is required for linear-gradient-image()') unless has-canvas
stops = stops[0] if length(stops) == 1
error('gradient image size required') unless start[0] is a 'unit'
size = start[0]
start = start[1] or 'top'
grad = create-gradient-image(size, start)
stops = normalize-stops(stops)
add-color-stop(grad, stop[0], stop[1]) for stop in stops
grad = build-gradient(start, stops)
'url(%s)' % gradient-data-uri(grad)

linear-gradient-svg(start, stops...)
grad = build-gradient(start, stops)
'url(%s)' % gradient-svg-data-uri(grad)
Loading