Categories
JavaScript Answers

How to validate digits, including floats, in JavaScript?

Sometimes, we want to validate digits, including floats, in JavaScript.

In this article, we’ll look at how to validate digits, including floats, in JavaScript.

How to validate digits, including floats, in JavaScript?

To validate digits, including floats, in JavaScript, we can use the isNaN function to check if a value can be converted to a number.

For instance, we write:

isNaN('0');

which returns false, which means '0' can be converted to a number.

Or:

isNaN('foo')

which returns true, which means 'foo' can’t be converted to a number.

Conclusion

To validate digits, including floats, in JavaScript, we can use the isNaN function to check if a value can be converted to a number.

Categories
JavaScript Answers Vue Vue Answers

How to inject elements into the head element with Vue.js and JavaScript?

Sometimes, we want to inject elements into the head element with Vue.js and JavaScript.

In this article, we’ll look at how to inject elements into the head element with Vue.js and JavaScript.

How to inject elements into the head element with Vue.js and JavaScript?

To inject elements into the head element with Vue.js and JavaScript, we can use the vue-head library.

To use it, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.rawgit.com/ktquez/vue-head/master/vue-head.js"></script>


<div id='app'>

</div>

to add the Vue and vue-head scripts with the app container element.

Then we write:

const v = new Vue({
  el: '#app',
  template: '<p>hello</p>',
  data: {
    title: 'My Title'
  },
  head: {
    title() {
      return {
        inner: this.title
      }
    },
    meta: [{
      name: 'description',
      content: 'My description'
    }]
  }
})

to add the head property which is set to an object with the title method which returns the title we want to render.

We also have a meta property set to an array to set the meta tag data.

As a result, we should see the title of the page set to My Title and:

<meta name="description" content="My description">

rendered in the head tag.

We can also install by running npm install vue-head --save and register it with:

import Vue from 'vue'
 
Vue.use(VueHead)
//...

Then we can follow the same steps as before.

Conclusion

To inject elements into the head element with Vue.js and JavaScript, we can use the vue-head library.

Categories
JavaScript Answers

How to run code when drawing is finished drawing on an HTML5 Canvas with JavaScript?

Sometimes, we want to run code when drawing is finished drawing on an HTML5 Canvas with JavaScript.

In this article, we’ll look at how to run code when drawing is finished drawing on an HTML5 Canvas with JavaScript.

How to run code when drawing is finished drawing on an HTML5 Canvas with JavaScript?

To run code when drawing is finished drawing on an HTML5 Canvas with JavaScript, we can call drawImage in the image’s onload method and run the code we want after drawImage is called.

For instance, we write:

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

to add a canvas element.

Then we write:

const img = new Image();
img.src = "https://i.picsum.photos/id/496/200/300.jpg?hmac=demLRv0UMwDhQHH6AEmbkJqlYuX27lnRH5N9FYcHBgw";
img.onload = () => {
  const canvas = document.querySelector('canvas');
  const context = canvas.getContext('2d');
  context.drawImage(img, 0, 0);
  console.log('finished drawing')
};

to draw the image in the canvas by selecting the canvas element.

Then we create an Image instance and set its onload property to a function that calls drawImage.

We select the canvas with document.querySelector and its context with getContext.

Then we call drawImage to draw the image.

Finally, we log 'finished drawing' after the image is drawn in the canvas.

Conclusion

To run code when drawing is finished drawing on an HTML5 Canvas with JavaScript, we can call drawImage in the image’s onload method and run the code we want after drawImage is called.

Categories
JavaScript Answers

How to add smoothly changing text with JavaScript?

Sometimes, we want to add smoothly changing text with JavaScript.

In this article, we’ll look at how to add smoothly changing text with JavaScript.

How to add smoothly changing text with JavaScript?

To add smoothly changing text with JavaScript, we can call the animate method on an element.

For instance, we write:

<p>
  hello
</p>

to add a p element.

Then we write:

const p = document.querySelector('p')
p.animate([{
    transform: 'translate(100px, -100%)'
  },
  {
    transform: 'translate(300px, 200px)'
  }
], 1500);

to select the p element with document.querySelector.

Then we call p.animate with an array of CSS rule objects to set how to animate the p element.

It’ll animate according to the CSS rules listed in the same order they’re listed.

The 2nd argument is the duration in milliseconds.

As a result, we see ‘hello’ slides from to left to bottom right.

Conclusion

To add smoothly changing text with JavaScript, we can call the animate method on an element.

Categories
JavaScript Answers

How to add multiple Chart.js charts in the same page with JavaScript?

Sometimes, we want to add multiple Chart.js charts in the same page with JavaScript.

In addition, we’ll look at how to add multiple Chart.js charts in the same page with JavaScript.

How to add multiple Chart.js charts in the same page with JavaScript?

To add multiple Chart.js charts in the same page with JavaScript, we can add multiple canvas elements and render a chart in each.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<canvas id="myChart" width="400" height="400"></canvas>
<canvas id="myChart2" width="400" height="400"></canvas>

to add the Chart.js script tag and 2 canvas elements.

Then we render a chart in each by writing:

const ctx = document.getElementById('myChart');
const ctx2 = document.getElementById('myChart2');

const myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      fill: true,
      borderWidth: 1,
    }]
  },
});

const myChart2 = new Chart(ctx2, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      fill: true,
      borderWidth: 1,
    }]
  },
});

We select the canvas elements with document.getElementById.

Then we call the Chart constructors with each canvas element and objects with some chart options.

Now we should see 2 charts displayed.

Conclusion

To add multiple Chart.js charts in the same page with JavaScript, we can add multiple canvas elements and render a chart in each.