Render image in Fit or Fill mode. Render responsive images.
git clone http://github.com/aneldev/dyna-image
cd dyna-image
yarn install
yarn start
This package comes with two components:
-
DynaImage
that renders an image in the given width & height space -
DynaResponsiveImage
renders an image in the given width. The height is auto
The main difference is that the DynaImage
uses the whole parent's available space, width, and height,
while the DynaResponsiveImage
uses only the width, the height is auto.
Also, note that the DynaImage
is not SEO friendly! Use it for decoration only.
The DynaReposniveImage
is SEO friendly and supports multiple versions of an image for responsive representation.
Render image in Fit or Fill mode.
Create a <div>
width some width
and height
and inside the <DynaImage>
. The <DynaImage>
will occupy the full width and height of the parent <div>
.
It will render the image in Fit or Fill mode.
The <div>
's width
and height
would also be obtained by flex boxes
or grid system
.
The EImageMode
would be:
In Fit mode, the image will fit in the parent div, which means that we will have horizontal or vertical empty spaces.
In Fill mode, the image will cover the whole area of the parent div, which means that the image will be cropped vertically or horizontally.
import {DynaImage, EImageMode} from "dyna-image";
...
<div style={{ height: '400px', backgroundColor: 'black' }}>
<DynaImage src="https://www.example.com/eifel.jpg"/>
</div>
or
<div style={{ height: '400px' }}>
<DynaImage
src="https://www.example.com/eifel.jpg"
mode={EImageMode.FILL}
/>
</div>
Render image in Fit or Fill mode.
IDynaImageProps {
className?: string;
style?: React.CSSProperties; // Container's style
imgStyle?: React.CSSProperties; // Image style (is div with background image)
src: string;
mode?: EImageMode; // Default: EImageMode.FIT
alt?: string;
content?: JSX.Element;
showLoadingSpinner?: boolean; // Default is false
showBrokenImageOnFail?: boolean; // Default is true
crop?: {
percentageX1: number; // 0..100 position
percentageY1: number; // 0..100 position
percentageX2: number; // 0..100 position
percentageY2: number; // 0..100 position
};
horizontalMirror?: boolean;
verticalMirror?: boolean;
blackAndWhite?: boolean;
onLoad?: () => void;
onError?: (error: any) => void;
}
Allows rendering above the image. The container has the entire width and height of the image.
On loading, it shows the Material UI CircularProgress animated icon.
On failed load, it shows the Material UI BrokenImage icon.
IDynaResponsiveImageProps {
className?: string;
imgStyle?: React.CSSProperties; // Image style (is div with background image)
srcSet: {
main: string; // Use it as default image
W192?: string;
W384?: string;
W768?: string;
W1024?: string;
W2048?: string;
W4096?: string;
};
alt?: string;
content?: JSX.Element;
horizontalMirror?: boolean;
verticalMirror?: boolean;
blackAndWhite?: boolean;
zoom?: {
percentageX: number; // Default is 50%. Examples: 0& 20% 100%
percentageY: number; // Default is 50%. Examples: 0& 20% 100%
zoom: number; // Default is 1. 1 1.2 1.5
};
onLoad?: () => void;
onError?: (error: any) => void;
}
Main is required, but all other resolutions are optional.
Allows rendering above the image. The container has the full width and height.
Allows zooming into the image on a specific point on the picture by percentage x/y. This has a crop effect.
- First version.
- Works with React 16
- Works with React 17
- Change:
<DynaImage>
'sACTUAL
mode is omitted. - New:
<DynaResponseveImage>
SEO friendly and supports multiple responsive image versions.