Comments (6)
i did something similar to kevmodrome, except in a component
if anyone wants to see the version with way too many comments (i learned a lot while writing it): https://gitlab.com/benblazak/homepage/-/blob/0cb4a432b3b3e9697f1a4a796d203884de59bb7f/src/lib/Image.svelte
shortened version:
<script lang="ts">
/**
* the output of a vite-imagetools import, using the `meta` query for output
* format
*/
export let meta: { src: string; width: number; format: string }[];
// if there is only one, vite-imagetools won't wrap the object in an array
if (!(meta instanceof Array)) meta = [meta];
// all images by format
let sources = new Map<string, typeof meta>();
meta.map((m) => sources.set(m.format, []));
meta.map((m) => sources.get(m.format).push(m));
// fallback image: first resolution of last format
let image = sources.get([...sources.keys()].slice(-1)[0])[0];
export let sizes = `${image.width}px`;
export let alt: string;
export let loading: string = undefined;
</script>
<picture>
{#each [...sources.entries()] as [format, meta]}
<source
{sizes}
type="image/{format}"
srcset={meta.map((m) => `${m.src} ${m.width}w`).join(", ")}
/>
{/each}
<img src={image.src} {alt} {loading} />
</picture>
from imagetools.
To add on to @benblazak Image component (absolutely incredible by the way, extremely helpful), I edited it slightly for my purposes and maybe others will get some benefit out of it.
- Removed the typescript portion of the component as my project used javascript
- Set the default "sizes" attribute to "100vw" (just a preference)
- Added an "imageClass" and "pictureClass" attribute so you can add classes to either the image or picture tag, which was quite helpful for use with tailwind.
Ideally, there should be easier picture creation, but this is a great option for svelte and probably something they should implement on their end.
<script>
export let meta;
// if there is only one, vite-imagetools won't wrap the object in an array
if (!(meta)) meta = [meta];
// all images by format
let sources = new Map();
meta.map((m) => sources.set(m.format, []));
meta.map((m) => sources.get(m.format).push(m));
// fallback image: first resolution of last format
let image = sources.get([...sources.keys()].slice(-1)[0])[0];
export let sizes = "100vw";
export let alt;
export let loading = undefined;
export let imageClass = "";
export let pictureClass = "";
</script>
<picture class={pictureClass}>
{#each [...sources.entries()] as [format, meta]}
<source
{sizes}
type="image/{format}"
srcset={meta.map((m) => `${m.src} ${m.width}w`).join(", ")}
/>
{/each}
<img src={image.src} {alt} {loading} class={imageClass}/>
</picture>
from imagetools.
I did this manually on the front-end using the meta
feature. Not the most beautiful thing in the world and you get a bunch of unnecessary stuff included when using meta. Just getting the srcset per type would go a long way. something like: { jpg: '...', avif: '...'}
from imagetools.
You are indeed correct. I swore that was valid JS too but for some reason VSCode was throwing an error. I was just messing around and I think when I removed the lang="ts" the errors went away but I forgot I changed that portion.
I haven't attempted to pass in a single resolution and format just because that use case is a little more specific so I appreciate you catching that before I ran into that error and wondered what was going on!
from imagetools.
I have no idea if this could be an option for svelte or if a CDN is an absolute NoGo, but I've been keeping image conversion off my back with Cloudinary (please excuse the advertising, I'm not paid for this). I just upload a large HRES image there and they deliver whatever is needed or doable by the current browser. Meanwhile also AVIF.
And they have a super generous free plan.
(Also, I'd be pretty curious to know why that's not a good option.)
from imagetools.
Glad to see it getting used
About this line:
if (!(meta)) meta = [meta];
Are you sure this works when the import has only 1 resolution and 1 format? If I recall,
if (!(meta instanceof Array)) meta = [meta];
is pure js, and necessary in that case.
from imagetools.
Related Issues (20)
- Upgrading to Vite 4.4.0 broke vite-imagetools HOT 5
- imagetools-core 4.0.4 `resolveConfigs` behavior change, `format` directive no longer working
- Build and build -w output differs HOT 6
- as=source and as=srcset both outputting srcset with all the same width params HOT 3
- manifest.json is generated incorrectly HOT 2
- Upgrading Vite-imagetools 5.0.8 throws an error from 4.x version HOT 2
- generateImageID causes tests to fail on CI HOT 3
- Vite example has a typo HOT 1
- Renaming picture fallback to img is not reflected in the document HOT 1
- Waste less effort transforming images
- Dependency on sharp not declared correctly for yarn’s PnP loader HOT 9
- Vite plugin crash HOT 2
- Loading attribute HOT 1
- v6 broken due to missing sharp 0.33 binariers HOT 3
- Some images get rotated for some reason HOT 14
- crop from / "gravity" HOT 1
- Error: Unable to parse color from string: ##ffaa22
- Pre-transform error: Failed to parse source for import analysis because the content contains invalid JS syntax
- How to receive access to original image in `extendOutputFormats` after v7.0.0? HOT 2
- Resize image to fit in box
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imagetools.