Menu

Making a screen's background follow its cover image in React Native Web (no library)
📰

Making a screen's background follow its cover image in React Native Web (no library)

DEV Community: reactnative·M620·1 day ago
#wja7QLwF
#devtoreactnative#devto#dev#const#luma
Reading 0:00
15s threshold

Open an event in Luma and the whole page takes its colour from the cover image: a red poster gives you a deep red page, a blue one gives you navy. It makes every event feel designed, even when the organiser only uploaded one picture.

My open-source Luma clone does the same thing on the web in about 100 lines, with a <canvas> and no colour library. Here is how.

Live demo: https://migrant620.github.io/luma/ (open any event)

Source (MIT): https://github.com/migrant620/luma-clone

1. Shrink the image to 48 × 48

You don't need every pixel to find the main colour. Drawing the image into a tiny canvas averages it for free and keeps the loop cheap:

const N = 48 ; const canvas = document . createElement ( ' canvas ' ); canvas . width = N ; canvas . height = N ; const ctx = canvas . getContext ( ' 2d ' , { willReadFrequently : true }); ctx . drawImage ( img , 0 , 0 , N , N ); const { data } = ctx .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.