Categories
JavaScript Answers

How to restart animation in CSS3 and JavaScript?

Sometimes, we want to restart animation in CSS3 and JavaScript.

In this article, we’ll look at how to restart animation in CSS3 and JavaScript.

How to restart animation in CSS3 and JavaScript?

To restart animation in CSS3 and JavaScript, we use the Animation API.

For instance, we write

const effect = new KeyframeEffect(
  el,
  [{ transform: "translateY(0%)" }, { transform: "translateY(100%)" }],
  { duration: 3000, direction: "alternate", easing: "linear" }
);

const animation = new Animation(effect, document.timeline);

animation.play();

to create the effect object with the KeyframeEffect constructor.

We call it with the el element we want to animate, an array of effects, and some animation options.

Options includes the animation duration, direction, and easing.

Then we create the Animation object with effect and document.timeline.

document.timeline is the default timeline of the current document.

Then we call play to play the animation.

Conclusion

To restart animation in CSS3 and JavaScript, we use the Animation API.

Categories
JavaScript Answers

How to create date regex that matches DD/MM/YYYY format with JavaScript?

Sometimes, we want to create date regex that matches DD/MM/YYYY format with JavaScript.

In this article, we’ll look at how to create date regex that matches DD/MM/YYYY format with JavaScript.

How to create date regex that matches DD/MM/YYYY format with JavaScript?

To create date regex that matches DD/MM/YYYY format with JavaScript, we can create a regex that matches the date pattern.

For instance, we write

/[0-9]{2}[/][0-9]{2}[/][0-9]{4}$/;

to match DD and MM with [0-9]{2}.

Then we match YYYY with [/][0-9]{4}.

We make sure YYYY is at the end of the string with $.

Conclusion

To create date regex that matches DD/MM/YYYY format with JavaScript, we can create a regex that matches the date pattern.

Categories
JavaScript Answers

How to limit the length of an array in JavaScript?

Sometimes, we want to limit the length of an array in JavaScript.

In this article, we’ll look at how to limit the length of an array in JavaScript.

How to limit the length of an array in JavaScript?

To limit the length of an array in JavaScript, we can set the length property to the max length of the array.

For instance, we write

arr.length = Math.min(arr.length, 5);

to set arr.length to the minimum number between arr.length and 5.

Then arr can’t have length more than 5.

Conclusion

To limit the length of an array in JavaScript, we can set the length property to the max length of the array.

Categories
JavaScript Answers

How to draw an oval in HTML5 canvas and JavaScript?

Sometimes, we want to draw an oval in HTML5 canvas and JavaScript.

In this article, we’ll look at how to draw an oval in HTML5 canvas and JavaScript.

How to draw an oval in HTML5 canvas and JavaScript?

To draw an oval in HTML5 canvas and JavaScript, we can use the canvas context ellipse method.

For instance, we write

ctx.ellipse(100, 100, 10, 15, 0, 0, Math.PI * 2);
ctx.fill();

to call ellipse to draw an ellipse with x coordinate of the center, y coordinate of the center, x radius, y radius, rotation, start angle, end angle, and the anticlockwise angle.

Then we call fill to fill the ellipse with the color we set.

Conclusion

To draw an oval in HTML5 canvas and JavaScript, we can use the canvas context ellipse method.

Categories
JavaScript Answers

How to extract “456” from “abc_456” where abc is of indefinite length with JavaScript?

Sometimes, we want to extract "456" from "abc_456" where abc is of indefinite length with JavaScript.

In this article, we’ll look at how to extract "456" from "abc_456" where abc is of indefinite length with JavaScript.

How to extract "456" from "abc_456" where abc is of indefinite length with JavaScript?

To extract "456" from "abc_456" where abc is of indefinite length with JavaScript, we can use the string match method.

For instance, we write

const pat = /([0-9]{1,})$/;
const m = str.match(pat);

to call str.match with the pat regex to return an array of number matches in the str string.

We use [0-9]{1,} to match numbers.

And we put it before $ to get numbers at the end of the string.

Conclusion

To extract "456" from "abc_456" where abc is of indefinite length with JavaScript, we can use the string match method.