Categories
JavaScript Answers

How to scale an image to fit on canvas with JavaScript?

Sometimes, we want to scale an image to fit on canvas with JavaScript.

In this article, we’ll look at how to scale an image to fit on canvas with JavaScript.

How to scale an image to fit on canvas with JavaScript?

To scale an image to fit on canvas with JavaScript, we call the canvas context drawImage method.

For instance, we write

ctx.drawImage(
  img,
  0,
  0,
  img.width,
  img.height,
  0,
  0,
  canvas.width,
  canvas.height
);

to call drawImage with the img image, the img image width and height, and the canvas width and height to scale the img image into the canvas width and height.

Conclusion

To scale an image to fit on canvas with JavaScript, we call the canvas context drawImage method.

Categories
JavaScript Answers

How to save HTML page as PDF using JavaScript?

Sometimes, we want to save HTML page as PDF using JavaScript.

In this article, we’ll look at how to save HTML page as PDF using JavaScript.

How to save HTML page as PDF using JavaScript?

To save HTML page as PDF using JavaScript, we can use the html2pdf library.

We can add it with

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Then we use it by writing

const element = document.getElementById("element-to-print");
const opt = {
  margin: 1,
  filename: "myfile.pdf",
  image: { type: "jpeg", quality: 0.98 },
  html2canvas: { scale: 2 },
  jsPDF: { unit: "in", format: "letter", orientation: "portrait" },
};

await html2pdf().set(opt).from(element).save();

in an async function to select the element with getElementById.

Then we set some options in the opt object.

We set the filename of the PDF, image type and quality, scaling, margin and format.

Then we use html2pdf().set(opt).from(element).save() to save the element’s content as a PDF.

save returns a promise.

Conclusion

To save HTML page as PDF using JavaScript, we can use the html2pdf library.

Categories
JavaScript Answers

How to access JavaScript property case-insensitively?

Sometimes, we want to access JavaScript property case-insensitively.

In this article, we’ll look at how to access JavaScript property case-insensitively.

How to access JavaScript property case-insensitively?

To access JavaScript property case-insensitively, we can use the Object.keys method to get an array of keys.

And then we use the array find method to find the key.

For instance, we write

const myObject = { mIxeDCaSEKeY: "value" };

const searchKey = "mixedCaseKey";
const asLowercase = searchKey.toLowerCase();
const k = Object.keys(myObject).find(
  (key) => key.toLowerCase() === asLowercase
);
const val = myObject[k];

to call Object.keys with myObject to get an array of keys from myObject.

Then we call find with a callback that converts key to lower case with toLowerCase.

And we compare that with asLowercase to find the key k.

Next, we get the val with myObject[key].

Conclusion

To access JavaScript property case-insensitively, we can use the Object.keys method to get an array of keys.

And then we use the array find method to find the key.

Categories
JavaScript Answers

How to get return value from setTimeout with JavaScript?

To get return value from setTimeout with JavaScript, we can create a promise from the setTimeout code.

For instance, we write

const x = () => {
  const promise = new Promise((resolve, reject) => {
    window.setTimeout(() => {
      resolve("done!");
    });
  });
  return promise;
};

const y = async () => {
  const result = await x();
  console.log(result);
};

to define function x that returns a promise created with the Promise constructor.

We call it with a callback that calls setTimeout with a callback that calls resolve to return the resolve value of the promise.

Then we return the promise.

Next, we define function y that calls x and gets the resolved value with await.

Therefore, result is 'done!'.

Categories
JavaScript Answers

How to capture tab key in text box with JavaScript?

Sometimes, we want to capture tab key in text box with JavaScript.

In this article, we’ll look at how to capture tab key in text box with JavaScript.

How to capture tab key in text box with JavaScript?

To capture tab key in text box with JavaScript, we can use the event keyCode property.

For instance, we write

document.onkeydown = (evt) => {
  const tabKey = 9;
  if (evt.keyCode === tabKey) {
    // ...
  }
};

to listen to the keydown event on the document by setting the document.onkeydown property to a function when we press a key anywhere on the page.

In the function, we check if evt.keyCode is 9.

If it is, then the tab key is pressed on the page.

Conclusion

To capture tab key in text box with JavaScript, we can use the event keyCode property.