🖼️ Tiny JSX helper function for high-DPI (Retina Display) images.

Overview


img-srcx

Tiny JSX helper function for high-DPI (Retina Display) images.

394 bytes minified and gzipped for ESM (830 bytes for ES5).

Installation

npm install --save img-srcx
# or
yarn install img-srcx
# or
pnpm install img-srcx

If you’re using this in older runtimes, such as node 8, Internet Explorer, or Android before 4.4.4, you’ll need to use a URL polyfill. You may find Can I use URL? useful.

Example

import { srcx, type Scale } from "img-srcx";

function MyImageComponent({ src, forceScale }: { src: string; forceScale?: Scale; }) {
	return <img {...srcx(src, { forceScale })} />;
}

function Root() {
	return (
		<div>
			<MyImageComponent src="https://repo.chariz.com/CydiaIcon.png" />
			<MyImageComponent src="https://www.google.com/google.jpg" forceScale={1} />
			<MyImageComponent src="https://github.githubassets.com/favicons/favicon.svg" />
		</div>
	);
}

ReactDOM.render(
	<Root />,
	document.getElementById("root")
);

Yields the following rendered HTML:

<div>
	<img
		src="https://repo.chariz.com/CydiaIcon.png"
		srcSet="https://repo.chariz.com/[email protected] 2x,https://repo.chariz.com/[email protected] 3x">

	<img
		src="https://www.google.com/google.jpg">

	<img
		src="https://github.githubassets.com/favicons/favicon.svg">
</div>

Usage

srcx(src: string, options?: { scales?: Scale[], forceScale?: Scale }): { src: string; srcSet?: string }

Returns JSX properties for use on an <img> element. The src and srcSet attributes set as appropriate for the given parameters.

  • src: The URL of the image to be displayed. Will be passed through exactly as the src attribute, unless forceScale is set.
  • options: Optional object with the following properties:
    • scales: An array of image scales to use. Defaults to [1, 2, 3]. The lowest scale will be used as the src attribute, and the remainder will be used as the srcSet attribute, unless forceScale is set.
    • forceScale: The minimum scale to use. If set, the src attribute will be set to the URL matching the given scale, and the srcSet attribute will be set to the remaining scales above the given scale.

Paths ending in .svg will be returned as-is, without srcSet.

Scale

A type for indicating scale factors. This is defined to 1 | 2 | 3, matching the common scale factors for high-DPI images.

Credits

Developed by Chariz:

License

Licensed under the Apache License, version 2.0. Refer to LICENSE.md.

You might also like...

startupDB is an Express middleware function implementing a high-performance in-memory database

startupDB startupDB is a database designed to create REST APIs. It is implemented as an Express middleware function and allows for easy implementation

Jul 26, 2022

🦄 • A simple open source project that can display random anime images and quotes.

Welcome to Rakoko App! 👋 Rakoko App is a simple application that is used to display images and quotes randomly. Built using Framework7. 💖 Demo Page

Jun 12, 2022

A tiny isomorphic fast function for generating a cryptographically random hex string.

ZeptoID A tiny isomorphic fast function for generating a cryptographically random hex string. Accoding to this calculator one would have to generate i

Oct 24, 2022

Very tiny function that checks if an object/array/value is shaped like another, with TypeScript type refining.

@suchipi/has-shape Very tiny (~200B before minification/compression) function that checks if an object/array/value is shaped like another, with TypeSc

Aug 13, 2022

👑 A tiny yet powerful tool for high-performance color manipulations and conversions

👑 A tiny yet powerful tool for high-performance color manipulations and conversions

Colord is a tiny yet powerful tool for high-performance color manipulations and conversions. Features 📦 Small: Just 1.7 KB gzipped (3x+ lighter than

Jan 3, 2023

High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

lazysizes lazysizes is a fast (jank-free), SEO-friendly and self-initializing lazyloader for images (including responsive images picture/srcset), ifra

Jan 1, 2023

Web Components engine based on JSX & TypeScript

Web Components engine based on JSX & TypeScript

WebCell Web Components engine based on JSX & TypeScript Usage Demo & GitHub template: https://web-cell.dev/scaffold/ Project bootstrap Command npm ini

Nov 17, 2022

Like JSX, but native and fast

Like JSX, but native and fast

esx High throughput React Server Side Rendering For a simplified example of esx in action, check out esx-demo. esx is designed to be a high speed SSR

Jan 2, 2023

✏️ Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped

✏️ Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped

Sugar High Introduction Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped Usage npm install --save sugar-high import { h

Dec 8, 2022
Releases(v1.0.0)
Owner
Chariz
A new payments platform for app developers.
Chariz
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Um bot de suporte feito usando threads para o Discord, 100% customizável, feito em JavaScript e inspirado no Rio Helper do servidor Elixir Lab e na Loritta Helper do serivdor de suporte da Loritta.

Ticket Bot Um bot de suporte feito usando threads para o Discord, 100% customizável, feito em JavaScript e inspirado no Rio Helper do servidor Elixir

ADG 6 Dec 21, 2022
High performance JSX web views for S.js applications

Surplus const name = S.data("world"), view = <h1>Hello {name()}!</h1>; document.body.appendChild(view); Surplus is a compiler and runtime to all

Adam Haile 587 Dec 30, 2022
The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.

EaselJS EaselJS is a library for building high-performance interactive 2D content in HTML5. It provides a feature-rich display list to allow you to ma

CreateJS 8k Dec 29, 2022
this is a single-page web application. we built a book website where the user can add , remove and display books. we used modules to implement these functionalities. also, we used the Date class to display the date and time.

Awsome Books In this Project, we have built A Books websites. Built With ?? HTML CSS javascript Git & Github Live Demo Here you can find the live Demo

Nedjwa Bouraiou 10 Aug 3, 2022
A public board for all the Computer Society and Students to display their profile. An online year-book for you to display your profile in the most creative manner

Student's Yearbook by IEEE Computer Society Student's yearbook is an open-source project which intends to dispaly the students who will be graduating

IEEE Computer Society 11 Dec 18, 2022
A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil ?? A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Ben Smith 21 Jun 20, 2022
Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Antonio Stoilkov 14 Dec 8, 2022
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022