Last active
August 29, 2015 14:03
-
-
Save theuntitled/4176681e28774665506e to your computer and use it in GitHub Desktop.
Generates an inline svg with the specified styles
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
require File.join(File.dirname(__FILE__), 'inline-svg.rb'); | |
# ... your stuff |
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
require 'sass' | |
require 'base64' | |
module Sass::Script::Functions | |
def inlineSvg(string, list = Sass::Script::List.new(Array.new, :comma)) | |
assert_type string, :String | |
file = File.open(string.value, "rb") | |
svgFileContents = file.read | |
file.close | |
cssString = '' | |
actualList = list.to_a | |
if actualList.length > 0 | |
cssString = '<style type="text/css">circle,ellipse,line,path,polygon,polyline,rect,text{' | |
if actualList.length > 2 | |
actualList.each { |pair| | |
cssString = cssString + pair.to_s.sub(' ', ': ') + ' !important;' | |
} | |
else | |
cssString = cssString + actualList.at(0).to_s + ': ' + actualList.at(1).to_s + ' !important;' | |
end | |
cssString = cssString + '}</style>'; | |
end | |
svgFileContents = svgFileContents.sub('</svg>', cssString + '</svg>'); | |
return Sass::Script::String.new('url(\'data:image/svg+xml;charset=utf-8;base64,' + Base64.encode64(svgFileContents) + '\')') | |
end | |
end |
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
.svg-stuff { | |
width: 100px; | |
height: 100px; | |
display: block; | |
background: inlineSvg('image.svg', (stroke #000)); | |
&:hover { | |
background: inlineSvg('image.svg', (fill rgba(0, 0, 255, .5), stroke #f00)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment