npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grayscale-polyfill

v0.0.1

Published

A tiny grayscale polyfill for IE 10 and 11.

Downloads

9

Readme

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 | Do not perform replacement. Return the instance of the SVGElement that has been created. | | 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');
}