"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Replace Deprecated SMIL Animations with CSS or Web Animations?

How Can I Replace Deprecated SMIL Animations with CSS or Web Animations?

Published on 2024-11-03
Browse:508

How Can I Replace Deprecated SMIL Animations with CSS or Web Animations?

Hover Effect: CSS Animation or Web Animations

Problem:
SMIL's animate tag is deprecated, and CSS hover transitions need to be implemented to replace it.

Solution:
Remove the set tags and add CSS to the element_tpl:hover pseudo-class:

.element_tpl:hover {
    stroke-opacity: 0.5;
}

Scaling Effect upon Change

Problem:
Animate the element to scale a few times upon a committed change.

Solution:
Consider the following options:

  1. CSS Animation: Use CSS keyframes to achieve scaling:
@keyframes scale-animation {
    0% { transform: scale(1); }
    50% { transform: scale(1.12); }
    100% { transform: scale(1); }
}

Then apply the animation to the element:

.element_tpl {
    animation: scale-animation 0.5s infinite alternate;
}
  1. Web Animations: Use the Web Animations API to programmatically control the scaling:
// Create a new animation
const animation = document.timeline.addAnimation();

// Define keyframes
const keyframes = [
    { transform: 'scale(1)', offset: 0 },
    { transform: 'scale(1.12)', offset: 0.5 },
    { transform: 'scale(1)', offset: 1 }
];

// Apply keyframes to the animation
animation.effect = keyframes;

// Target the element
animation.target = document.querySelector('.element_tpl');

Scale Up and Down on Click

Problem:
Animate the element to scale up and down upon clicking.

Solution:

  1. CSS Transitions: Use CSS transitions to trigger the scale changes on mousedown and mouseup events:
.element_tpl {
    transition: transform 0.2s;
}

.element_tpl:active {
    transform: scale(1.1);
}
  1. Web Animations: Use the Web Animations API to handle the click event and scale the element accordingly:
// Add event listener
document.querySelector('.element_tpl').addEventListener('click', (event) => {

    // Create a new animation
    const animation = document.timeline.addAnimation();

    // Define keyframes
    const keyframes = [
        { transform: 'scale(1)', offset: 0 },
        { transform: 'scale(1.1)', offset: 0.2 },
        { transform: 'scale(1)', offset: 0.4 },
    ];

    // Apply keyframes to the animation
    animation.effect = keyframes;

    // Target the element
    animation.target = event.target;
});

Saving SMIL Animations

Problem:
Transfer SMIL animations to CSS or Web animations.

Solution:
Use the SMIL polyfill created by Eric Willigers: https://github.com/ericwilligers/svg-animation. This polyfill converts SMIL animations to Web Animations, providing an alternative way to implement them.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3