Categories
JavaScript Answers

How to set image to fit width of the page using jsPDF and JavaScript?

Sometimes, we want to set image to fit width of the page using jsPDF and JavaScript.

In this article, we’ll look at how to set image to fit width of the page using jsPDF and JavaScript.

How to set image to fit width of the page using jsPDF and JavaScript?

To set image to fit width of the page using jsPDF and JavaScript, we call the addImage method.

For instance, we write

const doc = new jsPDF("p", "mm", "a4");

const width = doc.internal.pageSize.getWidth();
const height = doc.internal.pageSize.getHeight();
const imgData = "data:image/jpeg;base64,/9j/4AAQSkZJ......";

doc.addImage(imgData, "JPEG", 0, 0, width, height);

to get the width and height of the page with getWidth and getHeight.

Then we call addImage with imgData, width and height to add the image with the given width and height.

0 and 0 are the x and y coordinates of the top left corner of the image.

Conclusion

To set image to fit width of the page using jsPDF and JavaScript, we call the addImage method.

Categories
JavaScript Answers

How to call a JavaScript function in a click event handler?

Sometimes, we want to call a JavaScript function in a click event handler.

In this article, we’ll look at how to call a JavaScript function in a click event handler.

How to call a JavaScript function in a click event handler?

To call a JavaScript function in a click event handler, we call the function we want in the click handler.

For instance, we write

const f1 = () => {
  console.log("f1 called");
};

window.onload = () => {
  document.getElementById("save").onclick = () => {
    f1();
  };
};

to get the element with ID save with getElementById.

In it, we set its onclick property to a function that calls f1 when the element is clicked.

Conclusion

To call a JavaScript function in a click event handler, we call the function we want in the click handler.

Categories
JavaScript Answers

How to access microphone from a browser with JavaScript?

Sometimes, we want to access microphone from a browser with JavaScript.

In this article, we’ll look at how to access microphone from a browser with JavaScript.

How to access microphone from a browser with JavaScript?

To access microphone from a browser with JavaScript, we use the navigator.mediaDevices.getUserMedia method.

For instance, we write

const getMedia = async (constraints) => {
  let stream = null;
  try {
    stream = await navigator.mediaDevices.getUserMedia(constraints);
    console.log(stream);
  } catch (err) {
    document.write(err);
  }
};

getMedia({ audio: true, video: true });

to define the getMedia function.

In it, we call navigator.mediaDevices.getUserMedia with the constraints object with the options for what to get permission for.

It returns a promise with the stream.

We call getMedia with { audio: true, video: true } to get access to the microphone and camera.

Conclusion

To access microphone from a browser with JavaScript, we use the navigator.mediaDevices.getUserMedia method.

Categories
JavaScript Answers

How to invert a regular expression in JavaScript?

Sometimes, we want to invert a regular expression in JavaScript.

In this article, we’ll look at how to invert a regular expression in JavaScript.

How to invert a regular expression in JavaScript?

To invert a regular expression in JavaScript, we use negative lookahead.

For instance, we write

const notFoobar = /^(?!.*foobar)/.test("foobar@example.com");

to check if "foobar@example.com" does not have 'foobar'

We use ?! right after the opening parenthesis to start negative lookahead to look for the negation of the pattern after it.

How to invert a regular expression in JavaScript?

To invert a regular expression in JavaScript, we use negative lookahead.

Categories
JavaScript Answers

How to zip files using JavaScript?

Sometimes, we want to zip files using JavaScript.

In this article, we’ll look at how to zip files using JavaScript.

How to zip files using JavaScript?

To zip files using JavaScript, we use the jszip and file-saver packages.

To install them, we run

npm install jszip --save
npm install file-saver --save

Then we write

import JSZip from "jszip";
import FileSaver from "file-saver";

const zip = new JSZip();
zip.file("idlist.txt", "PMID:29651880\r\nPMID:29303721");

(async () => {
  const content = await zip.generateAsync({ type: "blob" });
  FileSaver.saveAs(content, "download.zip");
})();

to call zip.file to zip the files listed in the path arguments.

Then we call generateAsync to return a promise with the blob of the zip file.

Finally we call FileSaver.saveAs to save content as 'download.zip'.

Conclusion

To zip files using JavaScript, we use the jszip and file-saver packages.