How to write text on an HTML5 canvas element with JavaScript?

Sometimes, we want to write text on an HTML5 canvas element with JavaScript.

In this article, we’ll look at how to write text on an HTML5 canvas element with JavaScript.

How to write text on an HTML5 canvas element with JavaScript?

To write text on an HTML5 canvas element with JavaScript, we call the fillText method.

For instance, we write

<canvas id="my-canvas" width="200" height="120"></canvas>

to add a canvas element.

Then we write

const canvas = document.getElementById("my-canvas");
const context = canvas.getContext("2d");

context.fillStyle = "blue";
context.font = "bold 16px Arial";
context.fillText("hello", canvas.width / 2 - 17, canvas.height / 2 + 8);

to select the canvas with getElementById.

We get the canvas’ context with getContext.

And then we set the text style by setting the fillStyle property.

We set the font by setting the font property.

Finally, we call fillText to write the 'hello' at the given x and y coordinates.

Conclusion

To write text on an HTML5 canvas element with JavaScript, we call the fillText method.

How to make text bold in HTML?

Sometimes, we want to make text bold in HTML.

In this article, we’ll look at how to make text bold in HTML.

How to make text bold in HTML?

To make text bold in HTML, we use the strong or b tag.

For instance, we write

<b>This text is bold</b>

or

<strong>This text is strong</strong>

to make the text inside the b or strong tags bold.

Conclusion

To make text bold in HTML, we use the strong or b tag.

How to disable the double-tap “zoom” option in the browser on touch devices with CSS?

Sometimes, we want to disable the double-tap "zoom" option in the browser on touch devices with CSS.

In this article, we’ll look at how to disable the double-tap "zoom" option in the browser on touch devices with CSS.

How to disable the double-tap "zoom" option in the browser on touch devices with CSS?

To disable the double-tap "zoom" option in the browser on touch devices with CSS, we set the touch-action property.

For instance, we write

<p class="disable-dbl-tap-zoom">
  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
  voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
</p>

to add a p element.

Then we write

.disable-dbl-tap-zoom {
  touch-action: manipulation;
}

to add the touch-action: manipulation; style to disable the double-tap "zoom" option in the browser on touch devices on the p element.

Conclusion

To disable the double-tap "zoom" option in the browser on touch devices with CSS, we set the touch-action property.

How to create a link using JavaScript?

Sometimes, we want to create a link using JavaScript.

In this article, we’ll look at how to create a link using JavaScript.

How to create a link using JavaScript?

To create a link using JavaScript, we use various DOM methods.

For instance, we write

const a = document.createElement("a");
const linkText = document.createTextNode("my title text");
a.appendChild(linkText);
a.title = "my title text";
a.href = "http://example.com";
document.body.appendChild(a);

to call createElement to create an a element.

Then we call createTextNode to create a text node.

Next we call a.appendChild to append the linkText text node to the a element.

We set the title attribute of the a element by setting the a.title property.

And we set the href attribute of the a element by setting the a.href property.

Finally, we call document.body.appendChild with a to append a as the last child of the body element.

Conclusion

To create a link using JavaScript, we use various DOM methods.

How to wrap a react-router Link in an HTML button with JavaScript?

Sometimes, we want to wrap a react-router Link in an HTML button with JavaScript.

In this article, we’ll look at how to wrap a react-router Link in an HTML button with JavaScript.

How to wrap a react-router Link in an HTML button with JavaScript?

To wrap a react-router Link in an HTML button with JavaScript, we create a component.

For instance, we write

import React from "react";
import PropTypes from "prop-types";
import { withRouter } from "react-router";

const LinkButton = (props) => {
  const { history, location, match, staticContext, to, onClick, ...rest } =
    props;
  return (
    <button
      {...rest}
      onClick={(event) => {
        onClick && onClick(event);
        history.push(to);
      }}
    />
  );
};

LinkButton.propTypes = {
  to: PropTypes.string.isRequired,
  children: PropTypes.node.isRequired,
};

export default withRouter(LinkButton);

to create the LinkButton component that calls onClick if it’s defined when we click on the button.

And we call history.push toi navigate to the to URL.

We call withRouter with LinkButton to add the history prop to the component.

We then use it by writing

<LinkButton
  to="/path/to/page"
  onClick={(event) => {
    console.log("custom event here!", event);
  }}
>
  Push My Buttons
</LinkButton>

to add the button.

Conclusion

To wrap a react-router Link in an HTML button with JavaScript, we create a component.

How to prevent flex items from stretching with CSS?

Sometimes, we want to prevent flex items from stretching with CSS.

In this article, we’ll look at how to prevent flex items from stretching with CSS.

How to prevent flex items from stretching with CSS?

To prevent flex items from stretching with CSS, we set the align-items property.

For instance, we write

<div>
  <span>This is some text.</span>
</div>

to add some elements.

Then we write

div {
  align-items: flex-start;
  background: tan;
  display: flex;
  height: 200px;
}

span {
  background: red;
}

to make the div a flex container with display: flex;.

Then we add align-items: flex-start; to stop the contents from stretching vertically.

Conclusion

To prevent flex items from stretching with CSS, we set the align-items property.

How to close img tag with HTML?

Sometimes, we want to close img tag with HTML.

In this article, we’ll look at how to close img tag with HTML.

How to close img tag with HTML?

To close img tag with HTML, we add a slash at the end before the closing bracket.

For instance, we write

<img src="example.png" />

to add /> to close the img tag.

Conclusion

To close img tag with HTML, we add a slash at the end before the closing bracket.

How to add Font Awesome icon inside text input element with CSS?

Sometimes, we want to add Font Awesome icon inside text input element with CSS.

In this article, we’ll look at how to add Font Awesome icon inside text input element with CSS.

How to add Font Awesome icon inside text input element with CSS?

To add Font Awesome icon inside text input element with CSS, we add the icon to a span.

For instance, we write

<input name="txtName" id="txtName" />
<span class="fa fa-info-circle errspan"></span>

to add a span with the icon after the input.

Then we write

.errspan {
  float: right;
  margin-right: 6px;
  margin-top: -20px;
  position: relative;
  z-index: 2;
  color: red;
}

to make the span movable with position: relative;.

Then we move it to the position we want by setting the margins.

We make it stay on the right side of the input with float: right;.

Conclusion

To add Font Awesome icon inside text input element with CSS, we add the icon to a span.

How to control the width of a label tag with CSS?

Sometimes, we want to control the width of a label tag with CSS.

In this article, we’ll look at how to control the width of a label tag with CSS.

How to control the width of a label tag with CSS?

To control the width of a label tag with CSS, we set the width property.

For instance, we write

label {
  display: block;
  width: 100px;
}

to set the width of label elements to 100px.

Conclusion

To control the width of a label tag with CSS, we set the width property.

How to style HTML5 range input to have a different color before and after slider with CSS and JavaScript?

Sometimes, we want to style HTML5 range input to have a different color before and after slider with CSS and JavaScript.

In this article, we’ll look at how to style HTML5 range input to have a different color before and after slider with CSS and JavaScript.

How to style HTML5 range input to have a different color before and after slider with CSS and JavaScript?

To style HTML5 range input to have a different color before and after slider with CSS and JavaScript, we style the background with JavaScript.

For instance, we write

<div class="chrome">
  <input id="myinput" min="0" max="60" type="range" value="30" />
</div>

to add an input.

Then we write

#myinput {
  background: linear-gradient(
    to right,
    #82cfd0 0%,
    #82cfd0 50%,
    #fff 50%,
    #fff 100%
  );
  border: solid 1px #82cfd0;
  border-radius: 8px;
  height: 7px;
  width: 356px;
  outline: none;
  transition: background 450ms ease-in;
  -webkit-appearance: none;
}

to add some styling to the input.

We then change the background color dynamically when the input value changes by writing

document.getElementById("myinput").oninput = function () {
  const value = ((this.value - this.min) / (this.max - this.min)) * 100;
  this.style.background =
    "linear-gradient(to right, #82CFD0 0%, #82CFD0 " +
    value +
    "%, #fff " +
    value +
    "%, white 100%)";
};

We listen to the input event by setting oninput to a function that gets the value and the put it in the linear gradient value when we change this.input.value.

this is the range input.

Conclusion

To style HTML5 range input to have a different color before and after slider with CSS and JavaScript, we style the background with JavaScript.