Git Product home page Git Product logo

grayscale-polyfill's Introduction

Grayscale Polyfill

A tiny IE polyfill to make images grayscale.

Motivation

IE 10 and above dropped support for legacy DX filters which cases major issues with applications that need to support legacy browsers. Currently solutions are written in jQuery or use the Canvas API which has issues with cross-browser compatibility. For those needing to support IE 10/11 and are not using something like d3 (or some other SVG manipulation library) this is for you.

Usage

This package exports a single function Grayscale and expected to receive a single HTMLImageElement.

Webpack/Rollup

import { gray } from 'grayscale-polyfill';

gray(document.querySelector('img'));

Browser

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Grayscale Demo</title>
    </head>
    <body>
        <img src="wowow.jpg" class="grayscale" alt="Something cool" />
        <script src="https://unpkg.com/grayscale-polyfill/dist-browser/index.js"></script>
        <script>
            Grayscale.gray(document.querySelector('.grayscale'));
        </script>
    </body>
</html>

API

Options API

Option Type Required Description
polyfillCheck () => boolean No Override the default polyfill check function.
svgId string No Specify a custom SVG ID. This is the ID of the grayscale mask root svg element.
mode `'replace' 'manual'` No
grayscaleFilterId string No Specify a custom grayscale filter ID referenced by the SVG Image element

Exported Functions

Aside from options API, there are three (named) functions exported by this package:

Function Parameters Return Value Description
gray image: HTMLImageElement
options?: GrayscaleOptions
HTMLImageElement | SVGElement The main export from this package. This function creates the grayscale mask using the default grayscale filter ID and takes care of replacing image with the generated SVG Image element.
createSvgImage url: string
width: number
height: number
SVGElement Creates the SVG Image element that is used to replace the original image
createSvgMask id?: string
filterId?: string
root?: HTMLElement
SVGElement Returns the SVG element that contains the grayscale matrix

Usage with React

import * as React from 'react';
import { gray } from 'grayscale-polyfill';

const opts = { mode: 'manual' };

function App() {
    const [image, setImage] = React.useState(null);
    const ref = React.useRef(null);

    React.useEffect(() => {
        if (ref.current) {
            setImage({ __html: gray(ref.current, opts).innerHTML });
        }
    }, []);

    return image ? <div dangerouslySetInnerHTML={image} /> : <img ref={ref} src="someimage.jpg" />;
}

Alternatively you can use something like react-html-parser to safely convert the resulting SVG code to a rea ct component.

Usage in Other Browsers

For really old versions of IE and modern clients you can use regular CSS:

.grayscale {
    filter: gray; /* IE 6 - 9 */
    filter: grayscale(1); /* evergreen browsers*/
}

If you have certain browsers that are especially fickle you can save a new file gray.svg:

<svg xmlns="http://www.w3.org/2000/svg">
    <filter id="grayscale">
        <feColorMatrix
            type="matrix"
            values="0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0"
        />
    </filter>
</svg>

<!-- or in a more compact way -->

<svg xmlns="http://www.w3.org/2000/svg">
    <filter id="grayscale">
        <feColorMatrix type="saturate" values="0" />
    </filter>
</svg>

then you can reference in CSS:

.grayscale {
    filter: url('../../gray.svg#grayscale');
}

grayscale-polyfill's People

Contributors

atomicpages avatar

Watchers

 avatar  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.