Better Zhen animation

This commit is contained in:
BOTAlex 2024-03-31 01:02:29 +01:00
parent 03cfd5a617
commit 8ed6d26c40
2 changed files with 88 additions and 41 deletions

View File

@ -1,75 +1,101 @@
<script lang="ts">
import { onMount } from 'svelte';
import { onMount } from "svelte";
import { Vector2 } from "./Utils/Vector2";
// Params
let mouseMoveDevider = 1.5;
let mouseMoveScale: number = 0.5;
// Site variables
let mouseX = 0;
let mouseY = 0;
let mousePos: Vector2;
// Element binded variables
let animatedTopBackgroundX = 0;
let animatedTopBackgroundY = 0;
let mouseRelativeScaled: Vector2 = new Vector2(0, 0);
let windowWidth = 0;
let windowHeight = 0;
let screenCenter: Vector2;
let StartPageAnimated: Element;
let windowRef: Window;
function handleMouseMove(event: MouseEvent) {
// Calculate the mouse position relative to the center of the div
const centerX = StartPageAnimated.clientWidth / 2;
const centerY = StartPageAnimated.clientHeight / 2;
function onMouseMoved(event: MouseEvent) {
mousePos = new Vector2(event.clientX, event.clientY);
mouseX = event.clientX;
mouseY = event.clientY;
animatedTopBackgroundX = mouseX - StartPageAnimated.clientLeft - centerX;
animatedTopBackgroundY = mouseY - StartPageAnimated.clientTop - centerY;
//console.log(mouseX+"\n"+mouseY);
updateAnimation(mousePos);
}
// let windowWidth = window.innerWidth;
// let windowHeight = window.innerHeight;
function updateAnimation(mousePos: Vector2) {
let mouseRelativePos = mousePos.Sub(screenCenter);
mouseRelativeScaled = mouseRelativePos.Scale(mouseMoveScale);
// // Update window dimensions on mount
// onMount(() => {
// const updateDimensions = () => {
// windowWidth = window.innerWidth;
// windowHeight = window.innerHeight;
// };
//console.log(mouseRelativePos.x+"\n"+mouseRelativePos.y);
}
// window.addEventListener('resize', updateDimensions);
onMount(() => {
windowRef = window;
// return () => {
// window.removeEventListener('resize', updateDimensions);
// };
// });
const updateDimensions = () => {
windowWidth = windowRef.innerWidth;
windowHeight = windowRef.innerHeight;
screenCenter = new Vector2(windowWidth / 2, windowHeight / 2);
//console.log("Window size changed: (" + windowWidth + ", " + windowHeight + ")");
};
updateDimensions(); // On first pass
windowRef.addEventListener("resize", updateDimensions);
return () => {
windowRef.removeEventListener("resize", updateDimensions);
};
});
</script>
<svelte:body on:mousemove={handleMouseMove}/>
<svelte:window on:mousemove={onMouseMoved} />
<div class="StartPageContainer">
<div
class="StartPageAnimated"
bind:this={StartPageAnimated}
style="transform: translate(calc({mouseRelativeScaled.x}px), {mouseRelativeScaled.y}px);"
>
<div class="StartPageAnimated" bind:this={StartPageAnimated} style="transform: translate({animatedTopBackgroundX}px, {animatedTopBackgroundY}px);">
<span class="rotate45" style="display: flex; justify-content: center; align-items: center;">TEXT!!!</span>
<span
class="rotate45"
style="display: flex; justify-content: center; align-items: center;"
>TEXT!!!</span
>
</div>
</div>
<style>
.StartPageContainer{
.StartPageContainer {
height: 40vh;
background-color: burlywood;
overflow: hidden;
position: relative;
justify-content: center;
align-items: center;
display: flex;
padding: 0;
}
.StartPageAnimated{
.StartPageAnimated {
background: url("https://i1.adis.ws/i/canon/future_of_forests_header_16x9_dc14bbe1e35040f79bf566eedaf5c8f7?$hero-header-half-16by9-dt$");
position: static;
height: 100vh;
height: 100vw;
background-color: aqua;
position: absolute;
height: 150vh;
width: 150vw;
transition: all 1000ms cubic-bezier(0.16,1.63,0.01,0.99);
padding: 0;
transition: all 1000ms cubic-bezier(0.16, 1.63, 0.01, 0.99);
justify-content: center;
vertical-align: middle;

View File

@ -0,0 +1,21 @@
export class Vector2 {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
Add(vec2: Vector2){
return new Vector2(this.x + vec2.x, this.y + vec2.y);
}
Sub(vec2: Vector2){
return new Vector2(this.x - vec2.x, this.y - vec2.y);
}
Scale(mult: number){
return new Vector2(this.x * mult, this.y * mult);;
}
}