• Stars
    star
    761
  • Rank 59,698 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This polyfill takes the official CSS filters syntax and translates it to the different equivalent techniques that the browsers know for those effects

Polyfilter - a CSS Filters Polyfill

This polyfill takes the official CSS Filter Effects syntax, which spec you can find over here and translates it to the different equivalent techniques that the browsers know for those effects:

  • Prefixing for WebKit/Blink-based browsers
  • Translating into SVG-filters for Firefox
  • Translating into DirectX-filters for IE 6-9

For instance, this allows you to assign a property like

filter: blur(10px);

in your stylesheets and the polyfill will take care that it works in as many browsers as possible.

Supported Filters

  • grayscale*
  • sepia*
  • blur
  • invert*
  • brightness
  • drop-shadow

Have a look at this overview.

* the IEs only support 0% or 100% values

Supported Browsers

Currently the polyfill is able to support

  • Chrome 20+ (brightness filter 28+),
  • Opera 15+
  • Safari 6+,
  • Yandex 1+,
  • Firefox 4+,
  • IE 6 - 9 on desktop (IE 6 & 7 slightly degraded),

and

  • iOS 6+ Safari/Chrome/Webview
  • Chrome 28+ on Android,
  • Opera Mobile 14+,
  • Blackberry Browser 10+,
  • Firefox 4+ on Mobile,
  • IE on Windows Phone 7, which just supports grayscale.

Also have a look at http://caniuse.com/css-filters

Not supported Browsers

  • IE 10+,
  • older Presto-based Operas,
  • Opera Mini,
  • Android browser

A word regarding IE 10+

Why is IE 6 - 9 supported, but not IE 10 or higher? Well, since Microsoft decided to switch sides and to now follow standards, they killed their old proprietary filters beginning with IE 10 which I rely on for emulation.

Altough they did introduce SVG filters sadly those are limited to a usage inside SVGs. They cannot be applied to HTML-elements.

Even triggering legacy mode does not help any more. So this is why we are left at the end with no hook/support at all in IE10+ :(

And what about those Presto-based Opera?

Older Operas with Presto engine are not supported, as they offer none of the hooks I used.

Setup

First create a <script> element in which you define the absolute(!) path to the polyfill library (the stuff in the /lib/ subfolder) in a variable named polyfilter_scriptpath, like so:

<script>  
	var polyfilter_scriptpath = '/css-filters-polyfill/lib/';  
</script>

This is important both for the old IEs and the web worker script.

Should you not want the document stylesheets to not get automatically parsed, like when your plan is to apply filters only via JavaScript, then you can additionally set a polyfilter_skip_stylesheets switch:

<script>  
	var polyfilter_scriptpath = '/css-filters-polyfill/lib/';  
	var polyfilter_skip_stylesheets = true;  
</script>

Then you link cssParser.js and css-filters-polyfill.js from the polyfill library.

<script src="/css-filters-polyfill/lib/cssParser.js"></script>
<script src="/css-filters-polyfill/lib/css-filters-polyfill.js"></script>

In an ideal world you should minify and concatenate both of them together with your other JavaScript. If you don't want your page to get blocked by script-loading you put the scripts way down before the closing </body>. This might lead to some flickering of the filter effects during loading. If you can't live with the short flickering, put the scripts in the <head> of the page. Then it'll be gone, but your page will load slower. You decide.

Usage

Declarative assignment

This polyfill supports filter declarations in embedded (<style>) and external (<link rel="stylesheet">) stylesheets. It does not support inline-styles (i.e. style-attributes).

You define a filter by using the unprefixed W3C syntax, e.g.:

.element{
	filter: blur(10px);
}

And you can even assign two filters at once, e.g.

.element{
	filter: sepia(1) blur(10px);
}

Programmatic assignment

In addition the polyfill also extends the JavaScript CSSStyleDeclaration object, so that you can assign filter styles on the fly as you are used to with CSS. But instead of exposing a element.style.filter property as one would think, you instead need to address element.style.polyfilter, e.g.:

element.style.polyfilter = 'blur(10px)';

or via jQuery:

$(element).css('polyfilter','blur(10px)');

And, again, you can assign two filters at once, e.g.

element.style.polyfilter = 'sepia(1) blur(10px)';

Note: This does not work for IE 6 & 7. They just ignore any programmatic assignment.

Animations

Likewise, if you want to animate a filter, then you do this:

var value = 0,   
    increment = 1  
    elem = document.getElementById('filtered');

window.setInterval(function(){  
    value += increment;  
    elem.style.polyfilter = 'blur(' + value + 'px)';  
    if(value >= 10 || value <= 0) increment *= -1;  
},100);

Note: This again does not work for IE 6 & 7. They just ignore any programmatic assignment.

Cross Origin Restrictions

If you practice domain sharding and serve your assets from a different domain than the page you have two options to solve the problem of the poylfill not being allowed to refetch the stylesheets:

a) If you use the same domain, just with different subdomains, e.g. www.yourdomain.com + assets.yourdomain.com, then you can set

document.domain = "yourdomain.com";

and you are fine.

b) If you use different domains, then you need to activate HTTP CORS headers (Cross-Origin Resource Sharing) on the machine that hosts the stylesheets. If it is an Apache machine you can add the following to its/an .htaccess in its root:

<IfModule mod_headers.c>
  <FilesMatch "\.css$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

Or if you want more security, replace the * with the requesting domain:

<IfModule mod_headers.c>
  <FilesMatch "\.css$">
    Header set Access-Control-Allow-Origin "requestingdomain.com"
  </FilesMatch>
</IfModule>

Examples and Howtos

See http://schepp.github.io/CSS-Filters-Polyfill/examples/

License

Copyright (c) 2012 - 2013 Christian Schepp Schaefer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

box-sizing-polyfill

A CSS box-sizing: border-box polyfill for IE 6/7
1,177
star
2

CSS-JS-Booster

An easy to use stand-alone PHP-Library (but at the same time also a Wordpress plugin) that combines, optimizes, dataURI-fies, re-splits, compresses and caches your CSS and JS for quicker loading times.
PHP
305
star
3

dom-treemap-devtools-extension

A Chrome Devtools extension that helps you explore the distribution of DOM nodes in the document tree.
JavaScript
76
star
4

HTTP-headers

Every call we send to and receive from a web server is accompanied by more or less metadata, also known as the HTTP headers. Usually, those headers stay hidden in the shadows and we barely notice them - and we underestimate their powers. But ignoring them is a mistake! There is a lot happening around them lately. During The last couple of years a lot of new and powerful headers have emerged which, when applied correctly, help us harden our web applications against attacks and make our sites load a lot faster. That's why in this talk, I'm gonna give you the modern take on how to make the web more secure & fast.
HTML
29
star
5

CSS-Parser

A nice litte css parser written in php
PHP
19
star
6

async-document.write

Patches document.write to execute in a non-blocking way.
JavaScript
17
star
7

connection-type-checker

Utility that tries to figure out if you are on cellular or broadband
JavaScript
12
star
8

CSS-Prefix-Spawner

Tired of having to repeat all vendor prefixes over and over again just for getting your cross-browser dosis of CSS 3 eyecandy? Bother no more, my friend! Paste your single-vendor CSS or point to the file and we will automatically add the rest.
PHP
12
star
9

SASS-Mixins

9
star
10

IMG-2-TABLE

PHP-Script that converts GIF, PNG and JPG images to a TABLE-markup reproducing the image. Good for embedding images non-blockable inside HTML-emails. The lower the color-count the better for resulting code-size!
PHP
4
star
11

native-browser-apis

In this talk, I will give you an overview on how far native browser APIs have matured, thus freeing our sites from the grips of jQuery, lodash and other helper libraries. We will look at working more elegantly with arrays and objects in JavaScript, at modern DOM traversal and manipulation possibilities, at event delegation, at native smooth scrolling, look at ways to build carousels without library, we will explore built-in tools for image and font lazy loading, and we will bury DOMContentLoaded forever.
HTML
4
star
12

Chroma-Corners

“Chroma Corners” is the name of a new technique to create rounded corners in IE 6 – 8. It uses IE’s chroma-filter as a tool to mask away corner areas, which are created as a graphic and which are filled with a solid color. It is and behaves like standard HTML + CSS as opposed to the newer VML-based approches. It gives you more freeness and needs less photoshopping work than older background image based techniques for rounded corners like “Mountain Top” or “Sliding Doors”.
PHP
4
star
13

grunt-compile-revealjs

Compiles reveal.js slide decks from a JSON configuration and HTML partials
JavaScript
3
star
14

HTML5-Canvas-Workshop

JavaScript
3
star
15

crx-submit-to-workingdraft-trello

Chrome extension for submitting links to our podcast's board on Trello
JavaScript
3
star
16

making-of-rp-online

Nachrichtenseiten im Jahre 2020: Making of RP ONLINE
JavaScript
3
star
17

grunt-spreadsheet-to-json

Takes a Google Spreadsheet with translations and generates multiple JSON files from the columns.
JavaScript
2
star
18

CSS-JS-Booster-Website

PHP
2
star
19

schepp.dev

My personal site
Nunjucks
2
star
20

prototyping-for-performance

Performance-Experimente mit Chrome Devtools und CloudFlare Workers
JavaScript
2
star
21

Turbine-Extras

Additional files extending the capabilities of Turbine
PHP
2
star
22

HTTP-2

20 years have passed since the IETF introduced HTTP/1.1. At the time web pages were still designed with tables and font tags and they had hardly more than four images per page in total. HTTP/1.1 was perfectly suited to this application scenario. For today's projects, however, HTTP/1.1 is a massive bottleneck. To compensate for its conceptual disadvantages, we developed build processes in which we bundle resources via concatenation, inlining or spriting. In addition, we rely on concepts such as domain sharing and cookieless domains. The good news is: HTTP/2 has arrived and puts an end to the disadvantages of its predecessor. And it is supported by all modern browsers. But it's not enough to just flip a switch. We need to rethink and throw established processes and concepts overboard if we are to get the most out of HTTP/2.
CSS
2
star
23

imagery-on-the-web

Using images on a website is pretty straightforward, right? It's using an <img> element or a CSS background and then have them display a JPG, PNG or SVG. And that's it. Or is there more to it than that? Well, yes, there is. And it's a truckload full of possibilities.
HTML
2
star
24

webtechnologie-gluecksrad

Das Working Draft Webtechnologie Glücksrad
JavaScript
1
star
25

grunt-sass-recursive-import

Recursively includes SASS Partials in all Subdirectories
JavaScript
1
star
26

a-property-to-unlock-the-world

A Talk on why CSS Houdini is dead, but `@property` is not.
HTML
1
star
27

HTML5-WPO-Slides

Slidedeck for the HTML5 Days 2014 in Munich
PHP
1
star
28

css-next

After Grid and Flexbox CSS seems to have lost most of its dynamic. The most urgent layout problems seem to be solved and all eyes turn to JavaScript, because that's where all the exciting things seem to happen at the moment. But is CSS really "feature complete"? And does this development standstill really exist? To find that out, we first search for recently implemented CSS properties. Maybe we will find some after all? Then we take a look into the W3C's CSS crystal ball to see if there are some exciting things planned for the future.
JavaScript
1
star
29

wwnrw

Website of the Webworkers NRW meetup
HTML
1
star