Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save philippbosch/1021332 to your computer and use it in GitHub Desktop.
Save philippbosch/1021332 to your computer and use it in GitHub Desktop.
Sass implementation of the Noisy jquery plugin: https://github.com/DanielRapp/Noisy
require "chunky_png"
require "base64"
module Sass::Script::Functions
def background_noise(kwargs = {})
opts = {}
Sass::Util.map_hash({
"intensity" => [0..1, "", :Number, Sass::Script::Number.new(0.5) ],
"opacity" => [0..1, "", :Number, Sass::Script::Number.new(0.08)],
"size" => [1..512, "px", :Number, Sass::Script::Number.new(200) ],
"monochrome" => [[true, false], "", :Bool, Sass::Script::Bool.new(false) ]
}) do |name, (range, units, type, default)|
if val = kwargs.delete(name)
assert_type val, type, name
if range && !range.include?(val.value)
raise ArgumentError.new("$#{name}: Amount #{val} must be between #{range.first}#{units} and #{range.last}#{units}")
end
else
val = default
end
opts[name] = val
end
image = ChunkyPNG::Image.new(opts["size"].to_i, opts["size"].to_i)
for i in (0..(opts["intensity"].to_s.to_f * (opts["size"].to_i**2)))
x = rand(opts["size"].to_i)
y = rand(opts["size"].to_i)
r = rand(255)
a = rand(255 * opts["opacity"].to_s.to_f)
color = opts["monochrome"] ? ChunkyPNG::Color.rgba(r, r, r, a) : ChunkyPNG::Color.rgba(r, rand(255), rand(255), a)
image.set_pixel(x, y, color)
end
data = Base64.encode64(image.to_blob).gsub("\n", "")
Sass::Script::String.new("url('data:image/png;base64,#{data}')")
end
declare :noise, [], :var_kwargs => true
end
$bg-noise-intensity-default: 0.5;
$bg-noise-opacity-default: 0.08;
$bg-noise-size-default: 200;
@mixin bg-noise(
$intensity: $bg-noise-intensity-default,
$opacity: $bg-noise-opacity-default,
$size: $bg-noise-size-default,
$mono: false
) {
background-color: $color;
background-image: background_noise($intensity, $opacity, $size, $mono);
background-repeat: repeat;
}
@mixin bg-noise-mono(
$intensity: $bg-noise-intensity-default,
$opacity: $bg-noise-opacity-default,
$size: $bg-noise-size-default
) {
@include bg-noise($intensity, $opacity, $size, true);
}
body {
@include bg-noise(#cecece);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment