Clue Mediator

Write text on Canvas using React

📅June 21, 2020

Today we’ll show you how to write text on canvas using React. In the previous article, we taught you how to draw a line and rectangle on Canvas using ReactJS.

Steps to write text on Canvas

  1. Create a react application
  2. Add the canvas and initialize the context
  3. Function to write a text
  4. Write a text
  5. Output

1. Create a react application

First we have to create a simple react application using `create-react-app`. Check out the below link for more details.

Create React Application

2. Add the canvas and initialize the context

In the next step, we have to render the canvas element in the DOM and initialize the context of the canvas. Check out the following link for more details.

How to initialize the context of the canvas

3. Function to write a text

Use the following function to write a text on Canvas using react code.

// write a text
const writeText = (info, style = {}) => {
  const { text, x, y } = info;
  const { fontSize = 20, fontFamily = 'Arial', color = 'black', textAlign = 'left', textBaseline = 'top' } = style;

  ctx.beginPath();
  ctx.font = fontSize + 'px ' + fontFamily;
  ctx.textAlign = textAlign;
  ctx.textBaseline = textBaseline;
  ctx.fillStyle = color;
  ctx.fillText(text, x, y);
  ctx.stroke();
}

In the above code, we are drawing a text by passing two major parameters such as `info` and `style`. We have passed the x and y points via `info` along with the `text` param and manage the color, font family, text align and text baseline by passing into the second param. We have used the `fillText()` method to write a text on canvas.

4. Write a text

Let’s draw a text by using the above method.

useEffect(() => {
  writeText({ text: 'Clue Mediator!', x: 180, y: 70 });

  writeText({ text: 'Welcome to ', x: 180, y: 70 }, { textAlign: 'right' });

  writeText({ text: 'www.cluemediator.com', x: 180, y: 130 }, { fontSize: 30, color: 'green', textAlign: 'center' });

  writeText({ text: 'Like, Share and Subscribe...', x: 180, y: 200 }, { fontSize: 14, fontFamily: 'cursive', color: 'blue', textAlign: 'center' });
}, []);

5. Output

Combine all code together and see how it looks.

App.js

import React, { useRef, useEffect } from 'react';

function App() {
  const canvas = useRef();
  let ctx = null;

  // initialize the canvas context
  useEffect(() => {
    // dynamically assign the width and height to canvas
    const canvasEle = canvas.current;
    canvasEle.width = canvasEle.clientWidth;
    canvasEle.height = canvasEle.clientHeight;

    // get context of the canvas
    ctx = canvasEle.getContext("2d");
  }, []);

  useEffect(() => {
    writeText({ text: 'Clue Mediator!', x: 180, y: 70 });

    writeText({ text: 'Welcome to ', x: 180, y: 70 }, { textAlign: 'right' });

    writeText({ text: 'www.cluemediator.com', x: 180, y: 130 }, { fontSize: 30, color: 'green', textAlign: 'center' });

    writeText({ text: 'Like, Share and Subscribe...', x: 180, y: 200 }, { fontSize: 14, fontFamily: 'cursive', color: 'blue', textAlign: 'center' });
  }, []);

  // write a text
  const writeText = (info, style = {}) => {
    const { text, x, y } = info;
    const { fontSize = 20, fontFamily = 'Arial', color = 'black', textAlign = 'left', textBaseline = 'top' } = style;

    ctx.beginPath();
    ctx.font = fontSize + 'px ' + fontFamily;
    ctx.textAlign = textAlign;
    ctx.textBaseline = textBaseline;
    ctx.fillStyle = color;
    ctx.fillText(text, x, y);
    ctx.stroke();
  }

  return (
    <div class="App">
      <h3>Write text on Canvas - <a href="http://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h3>
      <canvas ref={canvas}></canvas>
    </div>
  );
}

export default App;

Run the project and check the output in the browser.

Output - Write text on Canvas using React - Clue Mediator

Output - Write text on Canvas using React - Clue Mediator

That’s it for today.
Thank you for reading. Happy Coding..!!

Demo & Source Code

Github Repository StackBlitz Project