Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce CSS filter API on FilterImage #2359

Merged
merged 11 commits into from
Jul 25, 2024
Merged

Conversation

jakex7
Copy link
Member

@jakex7 jakex7 commented Jul 22, 2024

Summary

Provide a CSS-like filter API in the FilterImage component.
Unlike the SVG filter API, which can be complex and challenging even for simple tasks, the CSS API is straightforward and allows users to quickly achieve satisfactory results.

The full API can be viewed here https://developer.mozilla.org/en-US/docs/Web/CSS/filter
We support all <filter-function> listed in the docs while we do not support functions from <url> (url() and src()).

All shorthands are implemented according to the W3 standard described here
https://www.w3.org/TR/filter-effects-1/#ShorthandEquivalents

This PR also changes the filters prop behavior as it adds fe in front of name attribute to not introduce any abstract above that specified in the docs.

<FilterImage
  source={myImage}
  filters={[{ name: 'colorMatrix', type: 'saturate', values: 0.2 }])
/>

is now

<FilterImage
  source={myImage}
  filters={[{ name: 'feColorMatrix', type: 'saturate', values: 0.2 }])
/>

Examples

Below are the simple examples of the usage

through StyleSheet

import React from 'react';
import {StyleSheet, View} from 'react-native';
import {FilterImage} from 'react-native-svg/filter-image';

export default () => {
  return (
    <FilterImage
      style={styles.image}
      source={{
        uri: 'https://cdn.pixabay.com/photo/2024/05/26/00/40/lizard-8787888_1280.jpg',
      }}
    />
  );
};
const styles = StyleSheet.create({
  image: {
    width: 200,
    height: 200,
    filter: 'saturate(100) grayscale(100%)',
  },
});

or directly in the style prop

import React from 'react';
import {StyleSheet, View} from 'react-native';
import {FilterImage} from 'react-native-svg/filter-image';

export default () => {
  return (
    <FilterImage
      style={{
        width: 200,
        height: 200,
        filter: 'saturate(100) grayscale(100%)',
      }}
      source={{
        uri: 'https://cdn.pixabay.com/photo/2024/05/26/00/40/lizard-8787888_1280.jpg',
      }}
    />
  );
};

Compatibility

OS Implemented
iOS
Android

Checklist

  • I have tested this on a device and a simulator
  • I added documentation in README.md
  • I updated the typed files (typescript)

@jakex7 jakex7 requested a review from WoLewicki July 22, 2024 09:29
Copy link
Member

@WoLewicki WoLewicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, please answer them

package.json Show resolved Hide resolved
USAGE.md Outdated Show resolved Hide resolved
apps/examples/src/examples/FilterImage/RemoteImage.tsx Outdated Show resolved Hide resolved
src/filter-image/extract/extractFilters.ts Show resolved Hide resolved
@jakex7 jakex7 requested a review from WoLewicki July 22, 2024 14:17
Copy link
Member

@WoLewicki WoLewicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jakex7 jakex7 merged commit 52466a2 into main Jul 25, 2024
7 checks passed
@jakex7 jakex7 deleted the @jakex7/filterImageCssApi branch July 25, 2024 08:19
@jakex7 jakex7 mentioned this pull request Jul 25, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants