/

How to Create and Save an Image with Node.js and Canvas

How to Create and Save an Image with Node.js and Canvas

Introduction

In this blog post, I will guide you through the process of programmatically generating custom banner images for your blog posts using Node.js and the Canvas package. This will allow you to have unique and visually appealing images when sharing your blog posts on Twitter.

The Problem

When sharing blog posts on Twitter, many posts end up looking the same due to the absence of custom banner images. To address this issue, I decided to explore the idea of generating banner images programmatically, inspired by Indie Hackers’ approach of creating images for forum blog posts.

Installing Dependencies

To get started, we need to install the canvas package from npm:

1
npm install canvas

The canvas package provides us with a Node.js implementation of the Canvas API, allowing us to create and manipulate images programmatically.

Creating the Image

Once we have the canvas package installed, we can begin creating our custom banner image. First, we need to import the createCanvas function from the canvas package and initialize a canvas with the desired width and height:

1
2
3
4
5
6
7
const { createCanvas } = require('canvas')

const width = 1200
const height = 600

const canvas = createCanvas(width, height)
const context = canvas.getContext('2d')

Next, we’ll fill the canvas with a background color. In this example, we’ll use the color white:

1
2
context.fillStyle = '#fff'
context.fillRect(0, 0, width, height)

Adding Text

To add text to our banner image, we’ll set the desired font, alignment, and color, and then use the fillText method to draw the text on the canvas:

1
2
3
4
5
6
const text = 'Hello, World!'

context.font = 'bold 70pt Menlo'
context.textAlign = 'center'
context.fillStyle = '#fff'
context.fillText(text, 600, 170)

Styling the Text

To make the text stand out, let’s add a background box behind it. We’ll set the desired color and size, and then draw the box and the text on the canvas:

1
2
3
4
5
6
7
8
const text = 'Hello, World!'

context.textBaseline = 'top'
context.fillStyle = '#3574d4'
const textWidth = context.measureText(text).width
context.fillRect(600 - textWidth / 2 - 10, 170 - 5, textWidth + 20, 120)
context.fillStyle = '#fff'
context.fillText(text, 600, 170)

Adding Website URL and Logo

To add your website URL and logo to the banner image, we’ll first import the loadImage function from the canvas package and load the logo image file:

1
2
3
4
5
const { createCanvas, loadImage } = require('canvas')

loadImage('./logo.png').then(image => {
// draw the image on the canvas
})

Once the image is loaded, we can use the drawImage method to place it on the canvas:

1
2
3
loadImage('./logo.png').then(image => {
context.drawImage(image, 340, 515, 70, 70)
})

Saving the Image

To save the final banner image as a PNG file, we can use the toBuffer method to get the image data as a buffer, and then write it to a file:

1
2
const buffer = canvas.toBuffer('image/png')
fs.writeFileSync('./image.png', buffer)

Conclusion

With the help of the canvas package, we can dynamically generate custom banner images for our blog posts using Node.js. This allows us to have visually appealing images when sharing our posts on Twitter. Feel free to experiment with different styles and designs to create unique and eye-catching banners for your blog.

Tags

Node.js, Canvas, Image Manipulation, Programmatically Generating Images