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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement