Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sijad committed Jan 7, 2024
1 parent e55cc80 commit b4be435
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 22 deletions.
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function split(txt) {
return txt
.split(" ")
.map((w) => `<span><span>${w}</span></span>`)
.map((w) => `<span class="word"><span>${w}</span></span>`)
.join(" ");
}
//!-->
Expand Down Expand Up @@ -72,6 +72,13 @@ <h1><%- split("Sajjad Hashemian"); %></h1>
</svg>
</a>
</li>
<li>
<a href="https://codepen.io/sijad">
<span class="sr-only">My Codepen</span>
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 72 72">
<path d="M36 12a24 24 0 0 0-24 24 24 24 0 0 0 24 24 24 24 0 0 0 24-24 24 24 0 0 0-24-24Zm-.048 9.509H36.054l.038.002c.243.016.47.096.662.222l13.102 8.697a1.364 1.364 0 0 1 .358.332l.026.033.018.026a1.365 1.365 0 0 1 .233.756v8.818a1.366 1.366 0 0 1-.202.736l-.016.024-.015.024a1.368 1.368 0 0 1-.365.367l-13.12 8.705a1.364 1.364 0 0 1-.679.238l-.057.002h-.096l-.038-.002a1.363 1.363 0 0 1-.674-.235l-13.08-8.682a1.363 1.363 0 0 1-.317-.274l-.002-.005a1.371 1.372 0 0 1-.081-.107l-.007-.007a1.366 1.366 0 0 1-.233-.78v-8.794a1.366 1.366 0 0 1 .165-.67l.026-.048.013-.02a1.378 1.378 0 0 1 .435-.44l13.079-8.68a1.364 1.364 0 0 1 .632-.231l.053-.005zm-1.323 3.927-9.281 6.158 4.093 2.751 5.188-3.489zm2.742.002v5.418l5.188 3.49 4.091-2.752zM36 33.24 31.898 36 36 38.758 40.102 36Zm-11.749.92v3.677L26.982 36Zm23.496.003L45.017 36l2.73 1.837zm-18.306 3.49-4.091 2.753 9.279 6.156v-5.418zm13.118 0-5.188 3.49v5.42l9.28-6.157z"/>
</svg>
</li>
<li>
<a href="mailto:hello@hashemian.me">
<span class="sr-only">Reachout via Email.</span>
Expand Down
149 changes: 149 additions & 0 deletions src/bg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d")!;

document.body.append(canvas);

let rects: {
x: number;
y: number;
delay: number;
duration: number;
a: number;
row: number;
col: number;
}[] = [];
let SIZE = 0;
let SPACE = 0;
let width = 0;
let height = 0;
let columns = 0;
let rows = 0;
let mouseX = -1;
let mouseY = -1;
let smoothMouseX = 0;
let smoothMouseY = 0;
let img: ImageData;

function onMouseMove(e: MouseEvent) {
mouseX = e.pageX;
mouseY = e.pageY;
}

window.addEventListener(
"mousemove",
(e) => {
onMouseMove(e);

smoothMouseX = mouseX;
smoothMouseY = mouseY;

window.addEventListener("mousemove", onMouseMove);
},
{ once: true }
);

function onResize() {
rects = [];
width = window.innerWidth;
height = window.innerHeight;

if (width < 768) {
SIZE = 2;
SPACE = 2;
} else {
SIZE = 3;
SPACE = 2;
}

canvas.width = width;
canvas.height = height;

ctx.fillStyle = "#fff";

columns = Math.ceil(width / SIZE / SPACE);
rows = Math.ceil(height / SIZE / SPACE);

for (let i = 0; i < columns * rows; i++) {
const col = i % columns;
const row = Math.floor(i / columns);

rects.push({
x: col * SPACE * SIZE,
y: row * SPACE * SIZE,
delay: Math.random() * 15000,
duration: Math.random() * 5000 + 1000,
a: 25,
row,
col,
});
}

img = ctx.createImageData(width, height);
}

let then = performance.now() * -20;
function animate(now: number) {
requestAnimationFrame(animate);

const diff = now - then;
then = now - diff;

const data = img.data;

if (mouseX !== -1) {
smoothMouseX += (mouseX - smoothMouseX) * 0.13;
smoothMouseY += (mouseY - smoothMouseY) * 0.13;
}

const dd = Math.min(
Math.max(
Math.hypot(smoothMouseX - mouseX, smoothMouseY - mouseY) * 0.7,
25
),
140
);

for (let i = 0; i < rects.length; i++) {
const r = rects[i];
const time = r.duration + r.delay;
const delta = diff % time;
const finished = delta > time;

let alpha = delta > r.delay && !finished ? 50 : 25;

if (mouseX != -1 && Math.random() > 0.9) {
const d = Math.hypot(smoothMouseX - r.x, smoothMouseY - r.y);
if (d < dd * Math.random()) {
alpha = 100 * Math.random() + 25;
}
}

if (r.a > 50) {
r.a -= 1;
alpha = r.a;
}

for (let j = 0; j <= SIZE; j++) {
for (let k = 0; k <= SIZE; k++) {
// const n = (r.x + j + (r.y + k + j) * width) * 4;
const n = (r.x + j + (r.y + k) * width) * 4;
data[n] = 255;
data[n + 1] = 255;
data[n + 2] = 255;
data[n + 3] = alpha;
}
}

r.a = alpha;
}

ctx.putImageData(img, 0, 0);
}

function start() {
onResize();
requestAnimationFrame(animate);
}

addEventListener("resize", onResize);
document.fonts.ready.then(start);
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@fontsource/londrina-solid/400.css";
import "@fontsource/londrina-sketch/400.css";
import "@fontsource/barlow-condensed/400.css";
import "./style.scss";
import "./bg";

let anim: Animation | null = null;

Expand Down Expand Up @@ -44,6 +45,7 @@ function handleToggleTheme(el: HTMLElement) {

anim.addEventListener("finish", function () {
if (anim && anim === this) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).applyTheme();

anim = null;
Expand All @@ -56,3 +58,9 @@ const toggleButton = document.querySelector("button");
toggleButton?.addEventListener("click", (e) => {
handleToggleTheme(e.currentTarget as HTMLElement);
});

document.fonts.ready.then(() => {
requestAnimationFrame(() => {
document.body.classList.add("fonts-loaded");
});
});
38 changes: 17 additions & 21 deletions src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ html {

body {
overflow: hidden;
margin: 0;
&:after {
content: "";
width: 100vw;
Expand Down Expand Up @@ -114,34 +115,29 @@ p {
margin: 0;
}

h1,
p {
@keyframes slideIn {
from {
transform: translateY(150%);
}
.word {
clip-path: inset(0);
display: inline-block;

to {
transform: translateY(0%);
@for $i from 1 through 12 {
&:nth-child(#{$i}) {
> span {
transition-delay: $i * 0.1s;
}
}
}

> span {
clip-path: inset(0);
display: inline-block;
transform: translateY(150%);
transition: transform 0.4s cubic-bezier(0.65, 0, 0.35, 1);
}
}

@for $i from 1 through 12 {
&:nth-child(#{$i}) {
> span {
animation-delay: $i * 0.08s;
}
}
}

> span {
display: inline-block;
transform: translateY(150%);
animation: slideIn 0.4s forwards cubic-bezier(0.65, 0, 0.35, 1);
body.fonts-loaded {
.word {
span {
transform: translateY(0%);
}
}
}
Expand Down

0 comments on commit b4be435

Please sign in to comment.