axelson

axelson

Scenic Core Team

Change images in LiveView without flashing

Is there a technique to change images in LiveView without flashing that preferably does not involve js or the browser downloading the images ahead of time (i.e. more than a few seconds)? This is a hard business requirement. Unfortunately based on my understanding of LiveView I will have to relent and use a js hook for this.

Most Liked

djm

djm

There will probably be techniques around this, but I think the JS hook would probably end up being the most straightforward and fault tolerant.

It sounds like you’re aware but for other readers: the core problem is that the DOM doesn’t know about the URL so it can’t fetch it until it first sees the replacing of the src attribute on the img element. Any trick you take to get that URL in the DOM before switching the src will reduce the flash but JS is the way only way to ensure the image is actually loaded before taking another action.

e.g

    var img=new Image();
    img.src=url;
    img.onload = callback; // callback places url in DOM.

I’m thinking off the top of my head here without testing but..

..a possible working could be having a list of images rather than one per item you want to display on the page. You always render the first image in the list normally. When a new images comes in, append to the list, and add an img element to the DOM (invisible but not display: none; otherwise it won’t download) without taking the original one away.

Then get the LiveComponent to tick after say 5s and remove the first element from the list, “promoting” your next image URL into the one which actually gets displayed. While the 5s window would give the image time to download it, it would not guarantee it like the JavaScript version would, obviously though you could play with the time window. You’d need CSS to ensure the images were laid on top of each other, and you could also add CSS transitions to transition between them, there are some examples of that here.

Writing that out I feels like I’ve invented frankenstein, I have no idea if that would actually work or be worth it.

Good luck, let us know.

axelson

axelson

Scenic Core Team

Thanks for writing this up! It helps clarify my thinking. Funnily enough I’m actually using bitmap images inside SVG. So I can’t easily embed them inline, although I suppose I still could, but I’m not sure the larger payload size would be worth it, but I guess it depends on the size of the images (which I’m still figuring out).

I wonder if it would make sense to build support for this into Phoenix LiveView itself, since that could make this all transparent to the developer.

Last Post!

Qqwy

Qqwy

TypeCheck Core Team

If you already know what images you will be using, you might introduce them somewhere else in the page, thereby allowing the browser to fetch them already, possibly.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement