Web Development Animation Events এবং Custom Animations তৈরি গাইড ও নোট

254

Riot.js-এ Animation Events এবং Custom Animations তৈরি করার জন্য কিছু সহজ পদ্ধতি রয়েছে, যার মাধ্যমে আপনি কম্পোনেন্টের মধ্যে এনিমেশন যুক্ত করতে পারেন। Riot.js নিজেই এনিমেশন পরিচালনার জন্য কোনো বিল্ট-ইন লাইব্রেরি সরবরাহ করে না, তবে আপনি সাধারণ CSS Animations বা JavaScript Events ব্যবহার করে এনিমেশন তৈরি এবং কাস্টমাইজ করতে পারেন।

এখানে Riot.js-এ এনিমেশন ইভেন্ট এবং কাস্টম এনিমেশন তৈরি করার কিছু পদ্ধতি দেখানো হচ্ছে।

১. CSS Animations ব্যবহার করা

CSS Animation এবং Transition এর মাধ্যমে আপনি Riot.js কম্পোনেন্টে সুন্দর এবং স্মুথ এনিমেশন তৈরি করতে পারেন। এটা বিশেষ করে সিম্পল এনিমেশন জন্য খুবই কার্যকর।

উদাহরণ: CSS Animation

<!-- AnimatedComponent.riot -->
<animated-component>
  <button onclick={toggleAnimation}>Click me to Animate</button>
  <div class="box" if={showBox}></div>

  <script>
    export default {
      onMounted() {
        this.showBox = false; // Initial state
      },

      toggleAnimation() {
        this.showBox = !this.showBox; // Toggle the visibility of the box
      }
    }
  </script>

  <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: #3498db;
      margin-top: 20px;
      transition: transform 0.5s ease-in-out; /* Smooth transition for animation */
    }

    .box[if] {
      transform: scale(1.5); /* Apply scaling animation when box is visible */
    }
  </style>
</animated-component>

ব্যাখ্যা:

  • এখানে একটি <div class="box"> তৈরি করা হয়েছে, যা একটি বাটনের ক্লিক ইভেন্টে এনিমেটেড হবে।
  • transition এবং transform প্রপার্টি ব্যবহার করে CSS এনিমেশন তৈরি করা হয়েছে।
  • showBox স্টেট পরিবর্তন হলে, বক্সের স্কেল পরিবর্তিত হবে, যা একটি স্মুথ এনিমেশন প্রদর্শন করবে।

২. JavaScript Animations ব্যবহার করা

আপনি যদি JavaScript ব্যবহার করে কাস্টম এনিমেশন তৈরি করতে চান, তবে Riot.js এ requestAnimationFrame বা অন্য কোনো এনিমেশন ফ্রেমওয়ার্ক ব্যবহার করতে পারেন।

উদাহরণ: JavaScript Animation

<!-- JavaScriptAnimatedComponent.riot -->
<js-animated-component>
  <button onclick={startAnimation}>Start Animation</button>
  <div class="box" if={showBox}></div>

  <script>
    export default {
      onMounted() {
        this.showBox = false;
      },

      startAnimation() {
        this.showBox = !this.showBox;
        if (this.showBox) {
          this.animateBox();
        }
      },

      animateBox() {
        let box = document.querySelector('.box');
        let scale = 1;
        let interval = setInterval(() => {
          if (scale >= 1.5) {
            clearInterval(interval);
          } else {
            scale += 0.05;
            box.style.transform = `scale(${scale})`;
          }
        }, 20);
      }
    }
  </script>

  <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: #e74c3c;
      margin-top: 20px;
      transform: scale(1);
      transition: transform 0.2s ease-in-out;
    }
  </style>
</js-animated-component>

ব্যাখ্যা:

  • startAnimation ফাংশনটি বাটন ক্লিক হলে এনিমেশন শুরু করে।
  • animateBox ফাংশনটি setInterval ব্যবহার করে ধীরে ধীরে বক্সের স্কেল বৃদ্ধি করে এবং একটি কাস্টম এনিমেশন তৈরি করে।
  • box.style.transform এর মাধ্যমে এনিমেশন বাস্তবায়িত হচ্ছে।

৩. Custom Animation Events

এনিমেশন চলাকালীন সময়ে কিছু Custom Animation Events ট্রিগার করতে চাইলে, আপনি JavaScript Event Listeners ব্যবহার করতে পারেন, যা এনিমেশন সম্পূর্ণ হওয়ার পর কিছু কাস্টম কাজ করে।

উদাহরণ: Custom Animation Events

<!-- CustomAnimationEventComponent.riot -->
<custom-animation-event>
  <button onclick={startAnimation}>Start Custom Animation</button>
  <div class="box" if={showBox} onanimationend={onAnimationEnd}></div>

  <script>
    export default {
      onMounted() {
        this.showBox = false;
      },

      startAnimation() {
        this.showBox = !this.showBox;
      },

      onAnimationEnd() {
        alert('Animation Ended');
      }
    }
  </script>

  <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: #2ecc71;
      margin-top: 20px;
      animation: scaleBox 2s ease-in-out;
    }

    @keyframes scaleBox {
      0% {
        transform: scale(1);
      }
      100% {
        transform: scale(1.5);
      }
    }
  </style>
</custom-animation-event>

ব্যাখ্যা:

  • onanimationend ইভেন্ট ব্যবহার করা হয়েছে, যা এনিমেশন শেষ হওয়ার পর onAnimationEnd ফাংশনকে কল করবে এবং একটি এলার্ট শো করবে।
  • CSS @keyframes ব্যবহার করে এনিমেশন তৈরি করা হয়েছে, যেখানে বক্সের স্কেল 1 থেকে 1.5 বৃদ্ধি পায়।

৪. Transition Events ব্যবহার করা

Riot.js-এ transitionend ইভেন্ট ব্যবহার করে আপনি যখন ট্রান্সিশন সম্পূর্ণ হয়, তখন নির্দিষ্ট কিছু কাজ করতে পারেন।

উদাহরণ: Using Transition Events

<!-- TransitionEventComponent.riot -->
<transition-event>
  <button onclick={startTransition}>Start Transition</button>
  <div class="box" if={showBox} ontransitionend={onTransitionEnd}></div>

  <script>
    export default {
      onMounted() {
        this.showBox = false;
      },

      startTransition() {
        this.showBox = !this.showBox;
      },

      onTransitionEnd() {
        console.log('Transition Ended!');
      }
    }
  </script>

  <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: #9b59b6;
      margin-top: 20px;
      transition: transform 1s ease-in-out;
    }

    .box[if] {
      transform: scale(1.5);
    }
  </style>
</transition-event>

ব্যাখ্যা:

  • ontransitionend ইভেন্ট ব্যবহার করে ট্রান্সিশন শেষ হলে একটি কাস্টম ফাংশন কল করা হচ্ছে।
  • CSS transition ব্যবহার করে বক্সের স্কেল পরিবর্তন করা হচ্ছে।

Riot.js-এ Animation Events এবং Custom Animations তৈরি করতে আপনি বিভিন্ন পদ্ধতি ব্যবহার করতে পারেন:

  1. CSS Animations এবং Transitions: সহজ এনিমেশন এবং স্টাইল পরিবর্তন।
  2. JavaScript Animations: কাস্টম এনিমেশন তৈরি করতে setInterval, requestAnimationFrame, বা অন্যান্য JavaScript ফিচার ব্যবহার করা।
  3. Custom Events: এনিমেশন ইভেন্টগুলির শেষে কাস্টম কাজ করার জন্য ইভেন্ট ব্যবহার করা (যেমন onanimationend, ontransitionend)।

এগুলো Riot.js-এ এনিমেশন সংক্রান্ত কাজগুলো সহজ এবং কার্যকরভাবে করতে সাহায্য করবে।

Content added By
Promotion

Are you sure to start over?

Loading...