Categories
JavaScript Answers

How to pixelate an image with canvas and JavaScript?

Sometimes, we want to pixelate an image with canvas and JavaScript.

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

How to pixelate an image with canvas and JavaScript?

To pixelate an image with canvas and JavaScript, we can call drawImage with a size smaller than the canvas size.

For instance, we write:

<canvas style='width: 200px; height: 200px'></canvas>

to add a canvas.

Then we write:

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
const img = new Image()
img.onload = () => {
  const size = 0.1,
    w = canvas.width * size,
    h = canvas.height * size;

  ctx.drawImage(img, 0, 0, w, h);

  ctx.msImageSmoothingEnabled = false;
  ctx.mozImageSmoothingEnabled = false;
  ctx.webkitImageSmoothingEnabled = false;
  ctx.imageSmoothingEnabled = false;

  ctx.drawImage(canvas, 0, 0, w, h, 0, 0, canvas.width, canvas.height);
}
img.src = 'https://picsum.photos/200/300'

We select the canvas with document.querySelector.

Then we get the context with getContext.

Next, we create a new Image instance and set the onload property to a function that calls drawImage with width w and height h that is smaller than the canvas size.

We then set image smoothing settings all to false.

Finally, we call drawImage with width w and height h again to draw the image.

Now we should see a pixelated image drawn.

Conclusion

To pixelate an image with canvas and JavaScript, we can call drawImage with a size smaller than the canvas size.

Categories
JavaScript Answers

How to create an array of empty strings with JavaScript?

Sometimes, we want to create an array of empty strings with JavaScript.

In this article, we’ll look at how to create an array of empty strings with JavaScript.

How to create an array of empty strings with JavaScript?

To create an array of empty strings with JavaScript, we can use the Array function and the fill method.

For instance, we write:

const arr = Array(20).fill("");
console.log(arr)

to create an array with 20 empty slots with Array(20).

Then we call fill with an empty string to fill the empty slots with empty strings.

Therefore, arr is an array with 20 empty strings.

Conclusion

To create an array of empty strings with JavaScript, we can use the Array function and the fill method.

Categories
JavaScript Answers

How to get all elements by class name and the change class name for each with JavaScript?

Sometimes, we want to get all elements by class name and the change class name for each with JavaScript.

In this article, we’ll look at how to get all elements by class name and the change class name for each with JavaScript.

How to get all elements by class name and the change class name for each with JavaScript?

To get all elements by class name and the change class name for each with JavaScript, we can select them all with document.querySelectorAll.

Then we spread the selected elements into an array with the spread operator and use the for-of loop to loop through them and change the class name for each element.

For instance, we write:

<div class='foo'>
  foo
</div>
<div class='foo'>
  foo
</div>
<div class='foo'>
  foo
</div>

to add 3 elements with class foo.

Then we write:

const els = document.querySelectorAll('.foo')
for (const el of [...els]) {
  el.className = "bar";
}

We select all the elements with:

const els = document.querySelectorAll('.foo')

Then we spread els into an array and loop through them with a for-of loop.

And in the body, we set el.className to 'bar' to change the class of each to bar.

Conclusion

To get all elements by class name and the change class name for each with JavaScript, we can select them all with document.querySelectorAll.

Then we spread the selected elements into an array with the spread operator and use the for-of loop to loop through them and change the class name for each element.

Categories
JavaScript Answers Vue

How to run a component method on load with Vue.js?

Sometimes, we want to run a component method on load with Vue.js.

In this article, we’ll look at how to run a component method on load with Vue.js.

How to run a component method on load with Vue.js?

To run a component method on load with Vue.js, we can call them in the created hook.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `<p>hello</p>`,
  data: {
    content: 'hello world'
  },
  methods: {
    hello() {
      console.log('hello');
    },
  },
  created() {
    this.hello()
  }
})

to define the hello method in the methods object property.

Then we call this.hello in the created hook.

Therefore, we see 'hello' logged when the component loads.

Conclusion

To run a component method on load with Vue.js, we can call them in the created hook.

Categories
JavaScript Answers

How to convert a textarea’s string value to a JavaScript array separated by new lines?

Sometimes, we want to convert a textarea’s string value to a JavaScript array separated by new lines.

In this article, we’ll look at how to convert a textarea’s string value to a JavaScript array separated by new lines.

How to convert a textarea’s string value to a JavaScript array separated by new lines?

To convert a textarea’s string value to a JavaScript array separated by new lines, we can get the value of the text area.

Then we call split on the string with '\n' to split it by the new line character into an array.

For instance, we write:

<textarea></textarea>

to add a text area.

Then we write:

const textarea = document.querySelector("textarea");
textarea.onchange = (e) => {
  const arr = e.target.value.split('\n')
  console.log(arr)
}

to listen to the change event by setting the textarea.onchange property to a function that gets the input value from e.target.value.

Then we call split with '\n' to split the string by the newline character into an array.

Conclusion

To convert a textarea’s string value to a JavaScript array separated by new lines, we can get the value of the text area.

Then we call split on the string with '\n' to split it by the new line character into an array.