Categories
JavaScript Answers

How to make HTML canvas full screen with JavaScript?

Sometimes, we want to make HTML canvas full screen with JavaScript.

In this article, we’ll look at how to make HTML canvas full screen with JavaScript.

How to make HTML canvas full screen with JavaScript?

To make HTML canvas full screen with JavaScript, we can set the canvas width and height to the page width and height.

For instance, we write

const canvas = document.getElementById("mainCanvas");
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;

to select the canvas with getElementById.

Then we set the canvas width to the body element’s width with

canvas.width = document.body.clientWidth;

And we do the same with the height with

canvas.height = document.body.clientHeight;

Conclusion

To make HTML canvas full screen with JavaScript, we can set the canvas width and height to the page width and height.

Categories
JavaScript Answers

How to match exact string with JavaScript?

Sometimes, we want to match exact string with JavaScript.

In this article, we’ll look at how to match exact string with JavaScript.

How to match exact string with JavaScript?

To match exact string with JavaScript, we use the ^ and $ characters in the regex.

For instance, we write

const r = /^a$/;

to create the regex r that matches ‘a’ exactly.

^ means the start of the string and $ means the end of the string.

Conclusion

To match exact string with JavaScript, we use the ^ and $ characters in the regex.

Categories
JavaScript Answers

How to convert Unicode string to hex with JavaScript?

Sometimes, we want to convert Unicode string to hex with JavaScript.

In this article, we’ll look at how to convert Unicode string to hex with JavaScript.

How to convert Unicode string to hex with JavaScript?

To convert Unicode string to hex with JavaScript, we can use the decodeURIComponent function.

For instance, we write

const fromHex = (hex) => {
  let str;
  try {
    str = decodeURIComponent(hex.replace(/(..)/g, "%$1"));
  } catch (e) {
    console.log("invalid hex input: " + hex);
  }
  return str;
};

to call decodeURIComponent on the string with the hex string.

Then we return it.

To decode it, we write

const toHex = (str) => {
  let hex;
  try {
    hex = unescape(encodeURIComponent(str))
      .split("")
      .map((v) => {
        return v.charCodeAt(0).toString(16);
      })
      .join("");
  } catch (e) {
    console.log("invalid text input: " + str);
  }
  return hex;
};

to call encodeURIComponent to encode the str string.

Then we call unescape on the encoded string to unescape the URI encoded string.

We then split the string with split and map the characters into hex with map.

Then we call join to join the mapped characters back together.

Conclusion

To convert Unicode string to hex with JavaScript, we can use the decodeURIComponent function.

Categories
JavaScript Answers

How to select a value in a select dropdown with JavaScript?

Sometimes, we want to select a value in a select dropdown with JavaScript.

In this article, we’ll look at how to select a value in a select dropdown with JavaScript.

How to select a value in a select dropdown with JavaScript?

To select a value in a select dropdown with JavaScript, we can set the selectedIndex value.

For instance, we write

<select style="width: 280px" id="mobility" name="mobility">
  <option selected="">Please Select</option>
  <option>K</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

to add a select drop down.

Then we write

document.getElementById("mobility").selectedIndex = 2;

to select the drop down with getElementById.

Then we set its selectedIndex property to the index of the option we want to select.

Conclusion

To select a value in a select dropdown with JavaScript, we can set the selectedIndex value.

Categories
JavaScript Answers

How to iterate array keys in JavaScript?

Sometimes, we want to iterate array keys in JavaScript.

In this article, we’ll look at how to iterate array keys in JavaScript.

How to iterate array keys in JavaScript?

To iterate array keys in JavaScript, we can get the keys with the keys method.

For instance, we write

for (const k of Array.from(Array(10).keys())) {
  console.log(k);
}

to get the keys of the Array(10) array as an array of keys with Array.from(Array(10).keys()).

Then we loop through each of them with a for-of loop.

Conclusion

To iterate array keys in JavaScript, we can get the keys with the keys method.