Categories
JavaScript Answers

How to draw text with an outer stroke with HTML5 canvas and JavaScript?

Sometimes, we want to draw text with an outer stroke with HTML5 canvas and JavaScript.

In this article, we’ll look at how to draw text with an outer stroke with HTML5 canvas and JavaScript.

How to draw text with an outer stroke with HTML5 canvas and JavaScript?

To draw text with an outer stroke with HTML5 canvas and JavaScript, we can use the the strokeText and fillText methods.

For instance, we write:

<canvas widrth='400' height='400'></canvas>

to add a canvas.

Then we write:

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
const text = 'hello'
ctx.font = '80px Sans-serif';
ctx.strokeStyle = 'black';
ctx.lineWidth = 8;
ctx.strokeText(text, 10, 70);
ctx.fillStyle = 'white';
ctx.fillText(text, 10, 70);

to select the canvas with querySelector and the context with getContext.

Then we set the font to the font style we want.

We set `strokeStyle to set the text stroke color.

lineWidth we set to the width of the line.

Then we call strokeText to draw text with an outer stroke.

And finally, we call fillText to add the filling of the text.

Conclusion

To draw text with an outer stroke with HTML5 canvas and JavaScript, we can use the the strokeText and fillText methods.

Categories
JavaScript Answers Vue

How to create a simple 10 seconds countdown in Vue.js?

Sometimes, we want to create a simple 10 seconds countdown in Vue.js.

In this article, we’ll look at how to create a simple 10 seconds countdown in Vue.js.

How to create a simple 10 seconds countdown in Vue.js?

To create a simple 10 seconds countdown in Vue.js, we can use the setInterval function.

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 the app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `<p>{{timerCount}}</p>`,
  data: {
    timerCount: 10
  },
  mounted() {
    const timer = setInterval(() => {
      this.timerCount--;
      if (this.timerCount === 0) {
        clearInterval(timer)
      }
    }, 1000);
  }
})

We call setInterval with a callback to decrement this.timerCount every second until it reaches 0.

When it reaches 0, then we call clearInterval with timer to clear the timer.

We call setInterval in the mounted hook to start the timer when the component is mounted.

Counclsion

To create a simple 10 seconds countdown in Vue.js, we can use the setInterval function.

Categories
JavaScript Answers

How to create a string with n characters with JavaScript?

Sometimes, we want to create a string with n characters with JavaScript.

In this article, we’ll look at how to create a string with n characters with JavaScript.

How to create a string with n characters with JavaScript?

To create a string with n characters with JavaScript we can use the JavaScript string’s repeat instance method.

For instance, we write:

const result = "x".repeat(20);
console.log(result)

We call 'x'.repeat with 20 to return a string that repeats ‘x’ 20 times.

Therefore, result is 'xxxxxxxxxxxxxxxxxxxx'.

Conclusion

To create a string with n characters with JavaScript we can use the JavaScript string’s repeat instance method.

Categories
JavaScript Answers

How to convert a string to regex object with JavaScript?

Sometimes, we want to convert a string to regex object with JavaScript.

In this article, we’ll look at how to convert a string to regex object with JavaScript.

How to convert a string to regex object with JavaScript?

To convert a string to regex object with JavaScript, we can use the RegExp constructor.

For instance, we write:

const value = "someone@example.com";
const pattern = "^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}$"
const re = new RegExp(pattern);
const match = re.test(value);
console.log(match)

to convert the pattern string to a regex object with the RegExp constructor.

Then we can call re.test to check if value matches the re regex pattern.

Therefore, match is true since value matches the pattern, which is an email regex.

Conclusion

To convert a string to regex object with JavaScript, we can use the RegExp constructor.

Categories
JavaScript Answers

How to dynamically create HTML elements using JavaScript?

Sometimes, we want to dynamically create HTML elements using JavaScript

In this article, we’ll look at how to dynamically create HTML elements using JavaScript.

How to dynamically create HTML elements using JavaScript?

To dynamically create HTML elements using JavaScript, we can use the createElement method.

For instance, we write:

<div id="main"></div>

to add a container div.

Then we write:

const tree = document.createDocumentFragment();
const link = document.createElement("a");
link.setAttribute("id", "id1");
link.setAttribute("href", "http://example.com");
link.appendChild(document.createTextNode("linkText"));

tree.appendChild(link);
document.getElementById("main").appendChild(tree);

to call createElement with 'a‘ to create an anchor element.

Then we call setAttribute to set the id and href attributes.

Next, we call link.appendChild to append the text node with the link text into it.

And then we call appendChild with link and tree to append the link and tree as child of elements tree and the the div with id main respectively.

Conclusion

To dynamically create HTML elements using JavaScript, we can use the createElement method.