Categories
JavaScript Answers

How to get the width and height of a HTML5 canvas with JavaScript?

Sometimes, we want to get the width and height of a HTML5 canvas with JavaScript.

In this article, we’ll look at how to get the width and height of a HTML5 canvas with JavaScript.

How to get the width and height of a HTML5 canvas with JavaScript?

To get the width and height of a HTML5 canvas with JavaScript, we can use the getBoundingClientRect method.

For instance, we write

const canvas = document.getElementById("mycanvas");
const { width, height } = canvas.getBoundingClientRect();

to select the canvas with getElementById.

Then we call getBoundingClientRect to return an object with the width and height of the canvas.

Conclusion

To get the width and height of a HTML5 canvas with JavaScript, we can use the getBoundingClientRect method.

Categories
TypeScript Answers

How to extending error class with TypeScript?

To extending error class with TypeScript, we call Object.setPrototypeOf to set the prototype of our error class.

For instance, we write

class FooError extends Error {
  constructor(m: string) {
    super(m);
    Object.setPrototypeOf(this, FooError.prototype);
  }

  sayHello() {
    return "hello " + this.message;
  }
}

to create a FooError class that extends the Error class.

Then we call Object.setPrototypeOf to set the prototype of this to FooError.prototype in the constructor.

this then inherits from FooError.prototype.

Then we can use Error class methods without errors.

Conclusion

To extending error class with TypeScript, we call Object.setPrototypeOf to set the prototype of our error class.

Categories
JavaScript Answers

How to get start and end of given month with moment.js and JavaScript?

Sometimes, we want to get start and end of given month with moment.js and JavaScript.

In this article, we’ll look at how to get start and end of given month with moment.js and JavaScript.

How to get start and end of given month with moment.js and JavaScript?

To get start and end of given month with moment.js and JavaScript, we call the startOf and endOf methods.

For instance, we write

const d1 = new moment().startOf("month").format("YYYY-DD-MM");
const d2 = new moment().endOf("month").format("YYYY-DD-MM");

to call startOf with 'month' to return a moment object with the start of the current month.

Likewise, we call startOf with 'month' to return a moment object with the end of the current month.

Then we call format to return a date string in YYYY-DD-MM format.

Conclusion

To get start and end of given month with moment.js and JavaScript, we call the startOf and endOf methods.

Categories
JavaScript Answers

How to fix Stdout buffer issue using Node child_process with JavaScript?

Sometimes, we want to fix Stdout buffer issue using Node child_process with JavaScript.

In this article, we’ll look at how to fix Stdout buffer issue using Node child_process with JavaScript.

How to fix Stdout buffer issue using Node child_process with JavaScript?

To fix Stdout buffer issue using Node child_process with JavaScript, we can listen to the data event when we call process.spawn.

For instance, we write

const child = process.spawn("<process>", []);

child.stdout.on("data", (data) => {
  console.log("stdout: " + data);
});

child.stderr.on("data", (data) => {
  console.log("stderr: " + data);
});

child.on("close", (code) => {
  console.log("child process exited with code " + code);
});

to call process.spawn to run a command.

Then we listen to the data event emitted by stdout with

child.stdout.on("data", (data) => {
  console.log("stdout: " + data);
});

We do the same with stderr with

child.stderr.on("data", (data) => {
  console.log("stderr: " + data);
});

Then we listen for the close event to check when the command is done with

child.on("close", (code) => {
  console.log("child process exited with code " + code);
});

Conclusion

To fix Stdout buffer issue using Node child_process with JavaScript, we can listen to the data event when we call process.spawn.

Categories
JavaScript Answers

How to prevent form from submitting multiple times from client side with JavaScript?

Sometimes, we want to prevent form from submitting multiple times from client side with JavaScript.

In this article, we’ll look at how to prevent form from submitting multiple times from client side with JavaScript.

How to prevent form from submitting multiple times from client side with JavaScript?

To prevent form from submitting multiple times from client side with JavaScript, we can disable the button when the form is being submitted.

For instance, we write

<form method="POST" action="...">
  ...
  <input type="submit" name="myButton" value="Submit" />
</form>

to add a form element.

Then we add

const checkForm = (e) => {
  e.preventDefault();
  e.target.myButton.disabled = true;
  e.target.myButton.value = "Please wait...";
};

const form = document.querySelector("form");
form.onsubmit = checkForm;

to select the form with querySelector.

Then we set the onsubmit property of the form to the checkForm function.

In it, we call preventDefault to stop the default server side form submission behavior.

Then disable the submit button with

e.target.myButton.disabled = true;

Next, we set the button’s text with

e.target.myButton.value = "Please wait...";

Conclusion

To prevent form from submitting multiple times from client side with JavaScript, we can disable the button when the form is being submitted.