Happy Valentine's Day Animation Using HTML CSS And Js

Hey there, You are most welcome to this article. I hope you will enjoy this article. If you like this article then please share this article with your friends and colleagues. If you have any questions or suggestions regarding this article then please comment down below.

πŸ“™Table Of Content

Project Folder Structure:

  • Create a file called  index.html  to serve as the main file.

  • Create a file called  style.css  for the CSS code.
  • Create a file called  script.js  for the JavaScript code.

Happy Valentine's Day Animation HTML CODE

  
<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>Drag paper proposal For valentine</title>
  <link rel="stylesheet" href="./style.css">

</head>
<body>
<!-- partial:index.partial.html -->
<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>saro-rosy ❤️</title>
  <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Short+Stack&amp;family=Homemade+Apple&amp;display=swap'>
  <link rel="stylesheet" href="style.css">
</head>
<body>

<!-- Developed by CODEWITHSHOBHIT  -->
<div class="paper heart">
</div>
<div class="paper image">
  <p> and I fallen in</p>
    <p>Love with You 😍 </p>
   <img src="https://e0.pxfuel.com/wallpapers/710/292/desktop-wallpaper-according-to-google-momoland-s-nancy-is-53-years-old.jpg" />
</div>
<div class="paper image">
  <p</p>
  <img src="https://iili.io/HSzXbmF.jpg" />
</div>
<div class="paper image">
  <p>How can be </p>
   <p> someone so cute ❤️ </p>
  <img src="https://pm1.aminoapps.com/7037/d8c72b899115e7db75c7015f9a7231e57bb1cffar1-2048-2048v2_00.jpg" />
</div>
<div class="paper red">
<p class="p1"> and My Favorite</p>
<p class="p2">Person 😍</p>
</div>
<div class="paper">
<p class="p1">You are Cute </p>
  <p class="p1">Amazing <span style="color: red !important;">❤️</span></p>
</div>
<div class="paper">
<p class="p1">Happy Valentine's Day, my love!</p>
</div>
<!-- javascript file -->
  <script  src="script.js"></script>
</body>
</html>
<!-- partial -->
  <script  src="./script.js"></script>

</body>
</html>

  
  

Happy Valentine's Day Animation CSS CODE


@import url('https://fonts.googleapis.com/css2?family=Zeyada&display=swap');
body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: 1000px;
  background-image: url("https://www.psdgraphics.com/wp-content/uploads/2022/01/white-math-paper-texture.jpg");
  background-position: center center;
}
.paper {
  background-image: url("https://i0.wp.com/textures.world/wp-content/uploads/2018/10/2-Millimeter-Paper-Background-copy.jpg?ssl=1");
  background-size: 500px;
  background-position: center center;
  padding: 20px 100px;
  transform: rotateZ(-5deg);
  box-shadow: 1px 15px 20px 0px rgba(0,0,0,0.5);
  position: absolute;
}
.paper.heart {
  position: relative;
  width: 200px;
  height: 200px;
  padding: 0;
  border-radius: 50%;
}
.paper.image {
  padding: 10px;
}
.paper.image p {
  font-size: 30px;
}
img {
  max-height: 200px;
  width: 100%;
  user-select: none;
}
.paper.heart::after {
  content: "";
  background-image: url('https://cdn.pixabay.com/photo/2016/03/31/19/25/cartoon-1294994__340.png');
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background-size: 150px;
  background-position: center center;
  background-repeat: no-repeat;
  opacity: 0.6;
}
.paper.red {
/*   filter: hue-rotate(90deg); */
}
.paper:nth-of-type(3n) {
  background-position: left top;
}
.paper:nth-of-type(3n) {
/*   filter: hue-rotate(200deg); */
}
p {
  font-family: 'Zeyada';
  font-size: 50px;
  color: rgb(0,0,100);
  opacity: 0.75;
  user-select: none;
  
}

Happy Valentine's Day Animation Javascript CODE


let highestZ = 1;
class Paper {
  holdingPaper = false;
  mouseTouchX = 0;
  mouseTouchY = 0;
  mouseX = 0;
  mouseY = 0;
  prevMouseX = 0;
  prevMouseY = 0;
  velX = 0;
  velY = 0;
  rotation = Math.random() * 30 - 15;
  currentPaperX = 0;
  currentPaperY = 0;
  rotating = false;
  init(paper) {
    document.addEventListener('mousemove', (e) => {
      if(!this.rotating) {
        this.mouseX = e.clientX;
        this.mouseY = e.clientY;
        this.velX = this.mouseX - this.prevMouseX;
        this.velY = this.mouseY - this.prevMouseY;
      }
      const dirX = e.clientX - this.mouseTouchX;
      const dirY = e.clientY - this.mouseTouchY;
      const dirLength = Math.sqrt(dirX*dirX+dirY*dirY);
      const dirNormalizedX = dirX / dirLength;
      const dirNormalizedY = dirY / dirLength;
      const angle = Math.atan2(dirNormalizedY, dirNormalizedX);
      let degrees = 180 * angle / Math.PI;
      degrees = (360 + Math.round(degrees)) % 360;
      if(this.rotating) {
        this.rotation = degrees;
      }
      if(this.holdingPaper) {
        if(!this.rotating) {
          this.currentPaperX += this.velX;
          this.currentPaperY += this.velY;
        }
        this.prevMouseX = this.mouseX;
        this.prevMouseY = this.mouseY;
        paper.style.transform = `translateX(${this.currentPaperX}px) translateY(${this.currentPaperY}px) rotateZ(${this.rotation}deg)`;
      }
    })
    paper.addEventListener('mousedown', (e) => {
      if(this.holdingPaper) return;
      this.holdingPaper = true;
      paper.style.zIndex = highestZ;
      highestZ += 1;
      if(e.button === 0) {
        this.mouseTouchX = this.mouseX;
        this.mouseTouchY = this.mouseY;
        this.prevMouseX = this.mouseX;
        this.prevMouseY = this.mouseY;
      }
      if(e.button === 2) {
        this.rotating = true;
      }
    });
    window.addEventListener('mouseup', () => {
      this.holdingPaper = false;
      this.rotating = false;
    });
  }
}
const papers = Array.from(document.querySelectorAll('.paper'));
papers.forEach(paper => {
  const p = new Paper();
  p.init(paper);
});

Live Preview Of Happy Valentine's Day Animation

See the Pen Drag paper proposal by Codewithshobhit (@Codewithshobhit) on CodePen.

How to run this Html Css and Js Project in Our Browser?

first, you need a code editor either you can use VS code studio or notepad and then copy the html,css, and javascript code, create separate or different files for coding then combine them, after creating file just click .html file or run from VS Code studio and you can project preview.

Which Code Editor do you use to create those projects?

I am using VS Code Studio.

is this project responsive or not?

Yes! this project is a responsive project.

If you enjoyed reading this post and have found it useful for you, then please give share it with your friends, and follow me to get updates on my upcoming posts. You can connect with me on  Instagram

if you have any confusion Comment below or you can contact us by filling out our Contact Us form from the home section. πŸ€žπŸŽ‰