Categories
JavaScript Answers

How to convert data URL to image data with JavaScript?

Sometimes, we want to convert data URL to image data with JavaScript.

In this article, we’ll look at how to convert data URL to image data with JavaScript.

How to convert data URL to image data with JavaScript?

To convert data URL to image data with JavaScript, we can create a canvas with the image.

For instance, we write:

const convertURIToImageData = (url) => {
  return new Promise((resolve, reject) => {
    if (!url) {
      return reject();
    }
    const canvas = document.createElement('canvas')
    const context = canvas.getContext('2d')
    const image = new Image();
    image.onload = () => {
      canvas.width = image.width;
      canvas.height = image.height;
      context.drawImage(image, 0, 0, canvas.width, canvas.height);
      resolve(context.getImageData(0, 0, canvas.width, canvas.height));
    }
    image.crossOrigin = "Anonymous";
    image.src = url;
  });
}
const url = "https://picsum.photos/200/300";
const load = async () => {
  const img = await convertURIToImageData(url)
  console.log(img)
}
load()

to create the convertURIToImageData function that takes a url.

In it, we return a promise that checks if the url is set.

If it’s not, we reject the promise.

Otherwise, we create a canvas with createElement.

Then we get its context with getContext.

Next, we create an img element with the Image constructor.

We set image.onload to a function to draws the image in the canvas bvy setting the canvas’ size to the image size.

Then we call drawImage to draw the image.

And then we call resolve with the image data we get from getImageData.

Next, we set image.crossOrigin to 'Anonymous' to let us get data from cross domain images.

And we src to url to load the image.

onload runs when the image is loaded.

Next, we define the load function that calls convertURIToImageData with url.

And then we get the img data from the promise with await.

Conclusion

To convert data URL to image data with JavaScript, we can create a canvas with the image.

Categories
JavaScript Answers

How to fix form onsubmit not working with JavaScript?

Sometimes, we want to fix form onsubmit not working with JavaScript.

In this article, we’ll look at how to fix form onsubmit not working with JavaScript.

How to fix form onsubmit not working with JavaScript?

To fix form onsubmit not working with JavaScript, we can set the form’s onsubmit property to a function that’s run when we click the submit button.

For instance, we write:

<form>
  <input type="file" name="file">
  <input type="submit">
</form>

to add a form.

We add the submit button for the form with <input type="submit">.

Next, we write:

const form = document.querySelector('form')
form.onsubmit = (e) => {
  e.preventDefault()
  console.log('submitted')
}

to select the form with querySelector.

And then we set form.onsubmit to a function that calls preventDefault to stop the default server side behavior and log 'submitted'.

Therefore, when we click on Submit, we see 'submitted' logged.

Conclusion

To fix form onsubmit not working with JavaScript, we can set the form’s onsubmit property to a function that’s run when we click the submit button.

Categories
JavaScript Answers

How to use addEventListener and getElementsByClassName to pass the element ID to the event listener with JavaScript?

Sometimes, we want to use addEventListener and getElementsByClassName to pass the element ID to the event listener with JavaScript.

In this article, we’ll look at how to use addEventListener and getElementsByClassName to pass the element ID to the event listener with JavaScript.

How to use addEventListener and getElementsByClassName to pass the element ID to the event listener with JavaScript?

To use addEventListener and getElementsByClassName to pass the element ID to the event listener with JavaScript, we can use event delegation.

For instance, we write:

<ul>
  <li class="menu" id="bob">Robert Smith</li>
  <li class="menu" id="jane">Jane Doe</li>
  <li class="menu" id="sue">Susan Carter</li>
</ul>

to add an ul with some li elements.

Then we write:

document.onclick = (e) => {
  if (e.target.className === 'menu') {
    console.log(e.target.id)
  }
}

to set document.onclick to a function that checks the className of the clicked element.

If the className is 'menu', then we get the ID of the element we clicked on with e.target.id.

Conclusion

To use addEventListener and getElementsByClassName to pass the element ID to the event listener with JavaScript, we can use event delegation.

Categories
JavaScript Answers

How to show just the first line of text of a div and expand on click with JavaScript?

Sometimes, we want to show just the first line of text of a div and expand on click with JavaScript.

In this article, we’ll look at how to show just the first line of text of a div and expand on click with JavaScript.

How to show just the first line of text of a div and expand on click with JavaScript?

To show just the first line of text of a div and expand on click with JavaScript, we can set the height of the div.

For instance, we write:

<div>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh tortor, consectetur in felis nec, elementum condimentum felis. Aliquam et condimentum dui. Quisque luctus purus mauris, non fringilla lectus mollis sit amet. Vivamus vitae lectus ut ex tristique tincidunt eget ac urna.
</div>

to add a div with text.

Then we write:

div {
  border: 1px solid red;
  height: 1em;
  padding: 2px;
  overflow: hidden
}

to set the height of the div to show a single line.

Next, we write:

const div = document.querySelector('div')
div.onclick = (e) => {
  e.target.style.height = 'auto'
}

to select the div with querySelector.

And then we set div.onclick to a function that sets the height of the div to 'auto'.

Conclusion

To show just the first line of text of a div and expand on click with JavaScript, we can set the height of the div.

Categories
JavaScript Answers

How to get last week’s date with JavaScript?

Sometimes, we want to get last week’s date with JavaScript.

In this article, we’ll look at how to get last week’s date with JavaScript.

How to get last week’s date with JavaScript?

To get last week’s date with JavaScript, we can get the timestamp of the original date and subtract 7 * 24 * 60 * 60 * 1000 from it.

For instance, we write:

const firstDay = new Date(2022, 1, 2)
const previousWeek = new Date(firstDay.getTime() - 7 * 24 * 60 * 60 * 1000);
console.log(previousWeek)

to create the firstDay date.

Then we get the timestamp of firstDay in milliseconds with getTime.

And then we subtract 7 days from firstDay in milliseconds and create a new Date instance from the returned timestamp.

Therefore, previousWeek is Wed Jan 26 2022 00:00:00 GMT-0800 (Pacific Standard Time).

Conclusion

To get last week’s date with JavaScript, we can get the timestamp of the original date and subtract 7 * 24 * 60 * 60 * 1000 from it.