From 49557153a6c1af5f2748dd279955c7c917ec09f9 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Tue, 24 Sep 2024 14:26:56 -0300 Subject: [PATCH] feat: background color blending --- src/routes/projects/[id]/+page.svelte | 154 +++++++++++++++++++++----- 1 file changed, 124 insertions(+), 30 deletions(-) diff --git a/src/routes/projects/[id]/+page.svelte b/src/routes/projects/[id]/+page.svelte index 79cc3c6..99ad3f7 100644 --- a/src/routes/projects/[id]/+page.svelte +++ b/src/routes/projects/[id]/+page.svelte @@ -1,4 +1,5 @@ -
+{#if browser}
+	
 {JSON.stringify(
-			{
-				color: color,
-				scroll: scroll
-			},
-			null,
-			2
-		)}
+			>{JSON.stringify(
+				{
+					page: currentPage,
+					color: {
+						background: color,
+						current: currentColor,
+						next: nextColor,
+						percentage: colorPerc
+					},
+					scroll: {
+						current: scroll,
+						max: maxScroll,
+						chunks: chunks,
+						currentChunk: currentChunk,
+						nextChunk: nextChunk
+					}
+				},
+				null,
+				2
+			)}
 
 
+{/if}
@@ -61,26 +128,53 @@ -
(scroll = reader.scrollTop)} - > -
- {#each pages as page} -
- -
-
- - -
-
+ {#key maxScroll} + {#if browser} +
{ + scroll = reader.scrollTop; + if (maxScroll === 0) { + maxScroll = Math.max(reader.scrollHeight - reader.clientHeight); + chunk = Math.round(maxScroll / pages.length); + for (let i = 0; i < pages.length; i++) { + chunks = [...chunks, chunk * i]; + } + } + let i = chunks.findIndex((c) => c > scroll - chunk); + currentColor = pages[i]?.background; + nextColor = pages[i + 1]?.background ?? currentColor; + + currentChunk = chunks[i]; + nextChunk = chunks[i + 1] ?? maxScroll; + + colorPerc = ((scroll - currentChunk) / (nextChunk - currentChunk)) * 100; + + color = blendHexColors(currentColor, nextColor, colorPerc / 100); + + currentPage = i; + }} + > +
+ {#each pages as page, key} + {@const coord = key * chunk} +
+ +
+
+ + +
+
+
+ {coord} + {/each}
- {/each} -
-
+
+ {/if} + {/key}