Friday, December 27, 2019

Lab 30 Mouse Move Shadow

In this lab, I followed the instruction from JavaScript30 and recreated the Mouse Move Shadow activity. First off, I made the HTML, CSS, and JavaScript files. Secondly, the <html>, <head>, <body>, <meta>, and <title> tags were already inserted in the HTML file. Thirdly, I typed in <link rel="stylesheet" type="text/css" href="Lab_30.css"> inside the <head> tags to link the CSS file with my HTML file. Fourthly, I typed in <script src="Lab_30.js" type="text/javascript"></script> inside the <body> tags which linked my JavaScript file with my HTML file. Inside the <body> tags, I typed the heading for the lab which has my name, date, and lab number with <div id="heading"><h1>Lab 30</h1><h2>Wei Jian Zhen</h2><h3>12/24/2019</h3></div>. Next I typed <div class="hero"><h1 contenteditable>šŸ”„WOAH!</h1></div>. Lastly, I made the copyright notice with <p> and <div> tags. For the CSS I did html {width: 100%; height: auto;} body {background-color: #00FFA8; min-width: 100%; min-height: 100%;} to give the webpage a background color and make the webpage responsive to screen size changes. #heading {text-align: center; font-size: 16px;} to center the heading of the lab. #copyright {font-size: 16px; font-family: Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", "serif"; font-weight: normal; padding-left: 1%; padding-right: 1%;} to change the font of the copyright notice. .hero {min-height: 100vh; display: flex; justify-content: center; align-items: center;color: black;} h1 {text-shadow: 10px 10px 0 rgba(0,0,0,1); font-size: 100px;} to make the text "WOAH!" big, colorized, and animated. For the JavaScript, I typed:

const hero = document.querySelector('.hero');
  const text = hero.querySelector('h1');
  const walk = 500; // 500px
  function shadow(e) {
    const { offsetWidth: width, offsetHeight: height } = hero;
    let { offsetX: x, offsetY: y } = e;
    if (this !== e.target) {
      x = x + e.target.offsetLeft;
      y = y + e.target.offsetTop;
    }
    const xWalk = Math.round((x / width * walk) - (walk / 2));
    const yWalk = Math.round((y / height * walk) - (walk / 2));
    text.style.textShadow = `
      ${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
      ${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
      ${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
      ${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
    `;
  }
  hero.addEventListener('mousemove', shadow);

This helps move the text and animate it. Knowing how to make text shadow move is useful to a web designer when making unique websites that require such skills.

No comments:

Post a Comment