Categories
JavaScript Answers

How to use Lodash to sort array of object by value with JavaScript?

To use Lodash to sort array of object by value with JavaScript, we use the orderBy method.

For instance, we write

const sortedChars = _.orderBy(chars, ["name"], ["asc"]);

to call orderBy to sort the chars array by the name property of each object in chars.

We use ['asc'] to specify that we sort by ascending order.

Categories
JavaScript Answers

How to get real mouse position in canvas with JavaScript?

To get real mouse position in canvas with JavaScript, we use the canvas getBoundingClientRect method.

For instance, we write

const getMousePos = (canvas, evt) => {
  const rect = canvas.getBoundingClientRect();
  return {
    x: ((evt.clientX - rect.left) / (rect.right - rect.left)) * canvas.width,
    y: ((evt.clientY - rect.top) / (rect.bottom - rect.top)) * canvas.height,
  };
};

to call canvas.getBoundingClientRect to get an object with the dimensions of the canvas’ bounding rectangle.

Then we get the x and y coordinates of the mouse in the canvas with

((evt.clientX - rect.left) / (rect.right - rect.left)) * canvas.width and ((evt.clientY – rect.top) / (rect.bottom – rect.top)) * canvas.height`.

This takes into account changing coordinates and scaling.

Categories
JavaScript Answers

How to access JPEG EXIF rotation data in JavaScript on the client side?

To access JPEG EXIF rotation data in JavaScript on the client side, we can use the exif-js library.

To add it, we run

<script src="https://cdn.jsdelivr.net/npm/exif-js"></script>

Then we use it by writing

const img1 = document.getElementById("img1");
EXIF.getData(img1, function () {
  const make = EXIF.getTag(this, "Make");
  const model = EXIF.getTag(this, "Model");
  const makeAndModel = document.getElementById("makeAndModel");
  makeAndModel.innerHTML = `${make} ${model}`;
});

to get the img element with getElementById.

Then we call getData with the img element and a callback that gets the exif data.

We call EXIF.getTag to get the exif tag we’rew looking for.

Categories
JavaScript Answers

How to reset (clear) form through JavaScript?

Sometimes, we want to reset (clear) form through JavaScript.

In this article, we’ll look at how to reset (clear) form through JavaScript.

How to reset (clear) form through JavaScript?

To reset (clear) form through JavaScript, we call the form element’s reset method.

For instance, we write

document.forms["frm_id"].reset();

to get the form with ID frm_id with document.forms["frm_id"].

Then we call reset to clear the values in the form.

Conclusion

To reset (clear) form through JavaScript, we call the form element’s reset method.

Categories
JavaScript Answers

How to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch?

Sometimes, we want to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch.

In this article, we’ll look at how to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch.

How to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch?

To fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch, we call clone on the response object before we use the response object multiple times.

For instance, we write

const res = await fetch("yourfile.json");
const data = await res.clone().json();

to call res.clone before we call json to clone the response object before we return a promise with the JSON response body.

Conclusion

To fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch, we call clone on the response object before we use the response object multiple times.