Categories
JavaScript Answers

How to detect when a window is resized using JavaScript?

Sometimes, we want to detect when a window is resized using JavaScript.

In this article, we’ll look at how to detect when a window is resized using JavaScript.

How to detect when a window is resized using JavaScript?

To detect when a window is resized using JavaScript, we listen for the resize event on window.

For instance, we write

window.addEventListener("resize", onResize);

to call window.addEventListener to listen to the resize event triggered by window.

The onResize function is the resize event listener and it runs every time the window is resized.

Conclusion

To detect when a window is resized using JavaScript, we listen for the resize event on window.

Categories
JavaScript Answers

How to go from Blob to ArrayBuffer with JavaScript?

Sometimes, we want to go from Blob to ArrayBuffer with JavaScript.

In this article, we’ll look at how to go from Blob to ArrayBuffer with JavaScript.

How to go from Blob to ArrayBuffer with JavaScript?

To go from Blob to ArrayBuffer with JavaScript, we can use the Response constructor.

For instance, we write

const arrayBuffer = await new Response(blob).arrayBuffer();

to create a Response object from blob.

And then we call arraybuffer to return a promise with the array buffer converted from the blob.

Conclusion

To go from Blob to ArrayBuffer with JavaScript, we can use the Response constructor.

Categories
JavaScript Answers

How to resize image with JavaScript canvas?

Sometimes, we want to resize image with JavaScript canvas.

In this article, we’ll look at how to resize image with JavaScript canvas.

How to resize image with JavaScript canvas?

To resize image with JavaScript canvas, we call drawImage with the given image width and height.

For instance, we write

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const img = new Image();

img.onload = () => {
  canvas.height = canvas.width * (img.height / img.width);

  const oc = document.createElement("canvas");
  const octx = oc.getContext("2d");
  oc.width = img.width * 0.5;
  oc.height = img.height * 0.5;
  octx.drawImage(img, 0, 0, oc.width, oc.height);

  octx.drawImage(oc, 0, 0, oc.width * 0.5, oc.height * 0.5);

  ctx.drawImage(
    oc,
    0,
    0,
    oc.width * 0.5,
    oc.height * 0.5,
    0,
    0,
    canvas.width,
    canvas.height
  );
};
img.src = "https://picsum.photos/200/300";

to set the img.onload property to a function that runs when the image is loaded.

In it, we set the canvas height and width with

canvas.height = canvas.width * (img.height / img.width);

We resize the the image to 50% of its original size with

const oc = document.createElement("canvas");
const octx = oc.getContext("2d");
oc.width = img.width * 0.5;
oc.height = img.height * 0.5;
octx.drawImage(img, 0, 0, oc.width, oc.height);

Then we resize it to its final size with

ctx.drawImage(
  oc,
  0,
  0,
  oc.width * 0.5,
  oc.height * 0.5,
  0,
  0,
  canvas.width,
  canvas.height
);

We draw the resized image by drawing the image in the oc canvas width 50% of the size of the oc canvas.

Conclusion

To resize image with JavaScript canvas, we call drawImage with the given image width and height.

Categories
JavaScript Answers

How to detect if a page has a vertical scrollbar with JavaScript?

Sometimes, we want to detect if a page has a vertical scrollbar with JavaScript.

In this article, we’ll look at how to detect if a page has a vertical scrollbar with JavaScript.

How to detect if a page has a vertical scrollbar with JavaScript?

To detect if a page has a vertical scrollbar with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

For instance, we write

const hasVScroll = document.body.scrollHeight > document.body.clientHeight;

to check if the body element’s scrollHeight is bigger than its clientHeight to check if the body element has a vertical scrollbar.

Conclusion

To detect if a page has a vertical scrollbar with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

Categories
JavaScript Answers

How to set custom attribute using JavaScript?

Sometimes, we want to set custom attribute using JavaScript.

In this article, we’ll look at how to set custom attribute using JavaScript.

How to set custom attribute using JavaScript?

To set custom attribute using JavaScript, we can use the setAttribute method.

For instance, we write

document
  .getElementById("item1")
  .setAttribute(
    "data",
    "icon: 'base2.gif', url: 'output.htm', target: 'AccessPage', output: '1'"
  );

to select the element with ID item1 with getElementById.

Then we call setAttribute with the attribute name and value to set the attribute.

Conclusion

To set custom attribute using JavaScript, we can use the setAttribute method.