Categories
JavaScript Answers

How to fix Node.js: SyntaxError: Cannot use import statement outside a module with JavaScript?

To fix Node.js: SyntaxError: Cannot use import statement outside a module with JavaScript, we can create and import CommonJS modules.

For instance, we write

mod.js

module.exports.func = () => {
  console.log("Hello World");
};

We export the func function in the mod.js module file.

Then we write

const myMod = require("./mod");
myMod.func();

to call require to import mod.js.

And then we call myMod.func, which should log 'Hello World'.

Categories
JavaScript Answers

How to limit panning in Google maps API V3 and JavaScript?

Sometimes, we want to limit panning in Google maps API V3 and JavaScript.

In this article, we’ll look at how to limit panning in Google maps API V3 and JavaScript.

How to limit panning in Google maps API V3 and JavaScript?

To limit panning in Google maps API V3 and JavaScript, we can create a bounds object and check against it.

For instance, we write

const allowedBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(70.33956792419954, 178.01171875),
  new google.maps.LatLng(83.86483689701898, -88.033203125)
);
const lastValidCenter = map.getCenter();

google.maps.event.addListener(map, "center_changed", () => {
  if (allowedBounds.contains(map.getCenter())) {
    lastValidCenter = map.getCenter();
    return;
  }

  map.panTo(lastValidCenter);
});

to call addListener to listen to the center_changed event.

Then event handler is run when we pan the map.

In the event handler, we check if the center is in the bounds with allowedBounds.contains(map.getCenter()).

If it is, we pan with panTo.

Otherwise, we move back to lastValidCenter.

We create the allowedBounds object with the LatLngBounds constructor called with 2 LatLng objects.

Conclusion

To limit panning in Google maps API V3 and JavaScript, we can create a bounds object and check against it.

Categories
JavaScript Answers

How to resize a Base64 image in JavaScript without using canvas?

Sometimes, we want to resize a Base64 image in JavaScript without using canvas.

In this article, we’ll look at how to resize a Base64 image in JavaScript without using canvas.

How to resize a Base64 image in JavaScript without using canvas?

To resize a Base64 image in JavaScript without using canvas, we can create an off-screen canvas component.

For instance, we write

const imageToDataUri = (img, width, height) => {
  const canvas = document.createElement("canvas"),
    ctx = canvas.getContext("2d");

  canvas.width = width;
  canvas.height = height;

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

to define the imageToDataUri function.

In it, we create a canvas with createElement.

Then we get the canvas with canvas.getContext.

Next, we set the width and height of the canvas to the size we want.

Then we call drawImage to draw the image with the width and height.

And finally we call canvas.toDataURL to return the resized image as a base64 string.

Conclusion

To resize a Base64 image in JavaScript without using canvas, we can create an off-screen canvas component.

Categories
JavaScript Answers

How to get the height of an element minus padding, margin, border widths with JavaScript?

Sometimes, we want to get the height of an element minus padding, margin, border widths with JavaScript.

In this article, we’ll look at how to get the height of an element minus padding, margin, border widths with JavaScript.

How to get the height of an element minus padding, margin, border widths with JavaScript?

To get the height of an element minus padding, margin, border widths with JavaScript, we get the offsetWidth and offsetHeight and subtract them by the padding and border width and height.

For instance, we write

const cs = getComputedStyle(element);

const paddingX = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
const paddingY = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);

const borderX =
  parseFloat(cs.borderLeftWidth) + parseFloat(cs.borderRightWidth);
const borderY =
  parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth);

const elementWidth = element.offsetWidth - paddingX - borderX;
const elementHeight = element.offsetHeight - paddingY - borderY;

to call getComputedStyle to get an object with the computed styles of element.

Then we get the horizontal padding with

const paddingX = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);

We get the vertical padding with parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom).

Horizontal border we get with parseFloat(cs.borderLeftWidth) + parseFloat(cs.borderRightWidth).

Vertical border we get with parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth).

Then we get the width and height without the border and padding with

const elementWidth = element.offsetWidth - paddingX - borderX;
const elementHeight = element.offsetHeight - paddingY - borderY;

Conclusion

To get the height of an element minus padding, margin, border widths with JavaScript, we get the offsetWidth and offsetHeight and subtract them by the padding and border width and height.

Categories
JavaScript Answers

How to fix PassportJS in Node not removing session on logout with JavaScript?

Sometimes, we want to fix PassportJS in Node not removing session on logout with JavaScript.

In this article, we’ll look at how to fix PassportJS in Node not removing session on logout with JavaScript.

How to fix PassportJS in Node not removing session on logout with JavaScript?

To fix PassportJS in Node not removing session on logout with JavaScript, we can use the req.session.destroy method.

For instance, we write

app.get("/logout", (req, res) => {
  req.session.destroy((err) => {
    res.redirect("/");
  });
});

to call req.session.destroy to destroy the session.

The callback is run when the session is destroyed.

Conclusion

To fix PassportJS in Node not removing session on logout with JavaScript, we can use the req.session.destroy method.