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.

Categories
JavaScript Answers

How to convert returned JSON object properties to lower first camelCase with JavaScript?

Sometimes, we want to convert returned JSON object properties to lower first camelCase with JavaScript.

In this article, we’ll look at how to convert returned JSON object properties to lower first camelCase with JavaScript.

How to convert returned JSON object properties to lower first camelCase with JavaScript?

To convert returned JSON object properties to lower first camelCase with JavaScript, we can use the Lodash mapKeys and camelCase functions.

For instance, we write

_.mapKeys(obj, (v, k) => _.camelCase(k));

to call mapKeys with obj and a callback that calls camelCase on key k to convert the keys in obj to camel case.

Conclusion

To convert returned JSON object properties to lower first camelCase with JavaScript, we can use the Lodash mapKeys and camelCase functions.

Categories
JavaScript Answers

How to convert all elements in an array to integer in JavaScript?

Sometimes, we want to convert all elements in an array to integer in JavaScript.

In this article, we’ll look at how to convert all elements in an array to integer in JavaScript.

How to convert all elements in an array to integer in JavaScript?

To convert all elements in an array to integer in JavaScript, w ecan use the array map method.

For instance, we write

const arrayOfNumbers = arrayOfStrings.map(Number);

to call arrayOfStrings.map with Number to return an array with the entries in arrayOfStrings converted to numbers.

Conclusion

To convert all elements in an array to integer in JavaScript, w ecan use the array map method.

Categories
JavaScript Answers

How to make the browser wait to display the page until it’s fully loaded with JavaScript?

Sometimes, we want to make the browser wait to display the page until it’s fully loaded with JavaScript.

In this article, we’ll look at how to make the browser wait to display the page until it’s fully loaded with JavaScript.

How to make the browser wait to display the page until it’s fully loaded with JavaScript?

To make the browser wait to display the page until it’s fully loaded with JavaScript, we run our code in the window.onload method.

For instance, we write

body {
  opacity: 0;
}

to set the body element to have opacity 0 initially.

Then we write

window.onload = () => {
  setTimeout(() => {
    document.body.style.opacity = "100";
  }, 500);
};

to set window.onload to a function that calls setTimeout with a callback that sets the body’s opacity to 100 after 500 milliseconds.

Conclusion

To make the browser wait to display the page until it’s fully loaded with JavaScript, we run our code in the window.onload method.