JavaScript QR Code Generator: Build It From Scratch

Build a JavaScript QR code generator: page setup, library code, canvas drawing, a tested no-library encoder, a live demo and mistakes that break scanning.

Share this post
8 min read

Last updated December 3, 2026

To build a QR code generator in JavaScript, you need a text box and a canvas. You also need code that turns text into a grid of dark and light squares. You can use a free library, or write the encoder yourself.

This guide shows both ways. The live demo below runs on Digieh’s own encoder, written for this article with no outside library.

Key Takeaways

  • A QR code is a square grid of modules, from 21 by 21 up to 177 by 177.
  • The fastest route is a library such as qrcode-generator on npm.
  • Building your own encoder teaches error correction, masks and layout.
  • Always leave a blank border of 4 modules around the code.
  • Test every code with a real phone camera before you share it.

Live demo: Digieh no-library QR code generator

Type a link or short text. This demo uses the encoder from this guide, with no outside library.

Digieh tool. Byte mode, error correction level M, versions 1 to 10, up to about 210 bytes. It runs in your browser and sends nothing to a server.

Tested before publishing. On December 3, 2026, Digieh encoded 35 sample texts with this demo’s encoder. A second program, the OpenCV QR decoder, read all 35 back correctly.

How Does a QR Code Work?

A QR code stores data in small squares called modules. Some squares are fixed patterns that help a camera find and read the code.

PartWhat it does
Finder patternsThree big squares in the corners so a camera can find the code
Timing patternsDotted lines that help the reader count rows and columns
Alignment patternsSmall squares that help read bent or tilted codes, from version 2
Format informationStores the error correction level and the mask used
Version informationStores the version number, from version 7
Data and error correctionYour text plus extra bytes that repair damage

Source: Digieh summary of the encoder in this guide. Version sizes from Denso Wave, QR Code versions, read December 3, 2026.

QR codes have 40 versions. Version 1 is 21 by 21 modules. Each version adds 4 modules per side, up to 177 by 177 at version 40.

35 of 35Digieh tested its demo encoder on December 3, 2026. Every one of the 35 sample codes, across versions 1 to 10, was read back correctly.

Which Way Should You Build It?

RouteBest forTrade-off
Use a libraryReal projects and quick resultsYou add an outside file to your page
Write your own encoderLearning how QR codes workMore code, and it needs careful testing

Source: Digieh comparison, December 3, 2026.

Most people should use a library for a live product. Writing your own is a great way to learn, and it removes an outside dependency.

Step 1: How Do You Set Up the Page?

Start with a text box, a button, a canvas to draw on, and a download link.

<label for="qr-text">Text or link</label>
<input id="qr-text" type="text" placeholder="https://example.com">
<button id="qr-go">Make QR code</button>
<canvas id="qr-canvas" width="264" height="264"></canvas>
<a id="qr-dl" download="qr-code.png">Download PNG</a>

The canvas is where the QR code will appear. A size of 264 pixels works well on phones and laptops.

Free updates

Join the Digieh list

Be the first to know when our weekly brief launches.

No spam. Unsubscribe anytime. Read our Privacy Policy.

Step 2: How Do You Make the QR Matrix With a Library?

The qrcode-generator package by Kazuhiko Arase is small and has no other dependencies. Version 1.4.4 was the latest on npm when we checked.

<script src="qrcode.js"></script>
<script>
document.getElementById('qr-go').addEventListener('click', function () {
  var text = document.getElementById('qr-text').value;
  var qr = qrcode(0, 'M');   // 0 = pick the smallest version, M = 15% recovery
  qr.addData(text);
  qr.make();
  drawQR(qr.getModuleCount(), function (r, c) { return qr.isDark(r, c); });
});
</script>

Passing 0 lets the library pick the smallest version that fits your text. The letter M sets the error correction level.

Its documentation lists the methods used here, including addData, make, getModuleCount and isDark.

Step 3: How Do You Draw the Code on a Canvas?

This function works with any encoder. It paints each dark module as a small black square, with a white border around the code.

function drawQR(size, isDark) {
  var canvas = document.getElementById('qr-canvas');
  var ctx = canvas.getContext('2d');
  var quiet = 4;                                   // blank border, in modules
  var cell = Math.floor(canvas.width / (size + quiet * 2));
  ctx.fillStyle = '#ffffff';
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = '#000000';
  for (var r = 0; r < size; r++) {
    for (var c = 0; c < size; c++) {
      if (isDark(r, c)) {
        ctx.fillRect((c + quiet) * cell, (r + quiet) * cell, cell, cell);
      }
    }
  }
  document.getElementById('qr-dl').href = canvas.toDataURL('image/png');
}

The blank border is called the quiet zone. Without it, many phone cameras fail to read the code.

Try this today. Copy the three code blocks into one web page file and open it. Then scan the result with your phone camera.

Step 4: How Do You Use Your Own Encoder Instead?

Our demo encoder is a single function called dgQR. It returns the grid size and a list of rows, with 1 for dark and 0 for light.

var result = dgQR('https://example.com');   // Digieh encoder from this page
// result.size    -> modules per side, for example 25
// result.modules -> rows of 1 (dark) and 0 (light)
drawQR(result.size, function (r, c) { return result.modules[r][c] === 1; });

You can view the full encoder by opening this page’s source in your browser. It is about 80 lines and handles versions 1 to 10.

What Happens Inside a QR Encoder?

  1. Turn the text into bytes, using Unicode encoding so all languages work.
  2. Add a mode marker and the length, then the data bits.
  3. Pad the data to fill the chosen version.
  4. Split the data into blocks and add Reed-Solomon error correction bytes.
  5. Place finder, timing and alignment patterns on the grid.
  6. Fill the data bits in a zigzag from the bottom right.
  7. Try all 8 mask patterns and keep the one with the lowest penalty score.
  8. Write the format and version information.

Reed-Solomon is the math that lets a damaged code still work. Here is the core loop in simple form.

// Reed-Solomon error correction bytes for one block (simplified)
function rsRemainder(data, generator, ecCount) {
  var rem = new Array(ecCount).fill(0);
  data.forEach(function (byte) {
    var factor = byte ^ rem.shift();
    rem.push(0);
    for (var i = 0; i < ecCount; i++) rem[i] ^= gfMul(generator[i + 1], factor);
  });
  return rem;
}

For a deeper walk-through of each step, see Project Nayuki’s guide to creating a QR code step by step.

Which Error Correction Level Should You Choose?

QR codes have four levels: L, M, Q and H. Higher levels repair more damage but hold less data.

Denso Wave says level M, which can restore about 15 percent, is the one most often chosen. Its example for level Q restores about 25 percent.

Source: Denso Wave, error correction feature, read December 3, 2026.

Use a higher level if the code will be printed small, placed outdoors, or covered by a logo.

Can You Use QR Codes Freely?

Yes. Denso Wave created the QR code in 1994. It says you can use it freely if you follow the Japanese Industrial Standards (JIS) or ISO standards.

The word QR Code is a registered trademark of Denso Wave Incorporated. Its help page says a trademark note is not needed when you use only the code image.

Source: Denso Wave help page and QR Code history, read December 3, 2026.

What Are Common Mistakes When Building a QR Generator?

  • No quiet zone around the code.
  • Low contrast, such as light gray on white.
  • Too much text, which makes the code dense and hard to scan.
  • Stretching the image so modules are not square.
  • Not testing with a real phone before printing.

Printing codes for a shop? Read our tips on QR code labels for your shop.

Just need a code now, with no coding? Use the free Digieh QR code maker, or browse all our free online tools.

Teach people to scan safely too. Our guide on spotting phishing covers fake links.

Building more tools? Our guide to speeding up a WordPress site helps your tool pages load fast.

Which route will you take?

  • Use a library
  • Write my own encoder
  • Use a ready-made tool
  • Not sure

Next project: build a JavaScript word counter that works in every language.

Test Yourself

How many modules wide is a version 1 QR code?

  1. 17
  2. 21
  3. 25

Denso Wave says version 1 is 21 by 21 modules.

Why do QR codes need a quiet zone?

  1. To store extra data
  2. So cameras can find the edges
  3. To make the code look nicer

The blank border helps the camera tell where the code starts and ends.

Frequently Asked Questions

Can I make a QR code in JavaScript without a library?

Yes. You need byte encoding, Reed-Solomon error correction, the fixed patterns and masking. Our demo does this in about 80 lines.

How do I download the QR code as an image?

Draw it on a canvas, then use canvas.toDataURL with the PNG type and a download link.

How much text can a QR code hold?

It depends on the version and error correction level. Our demo holds about 210 bytes at level M.

Is it free to use QR codes?

Denso Wave says you can use QR codes freely if you follow the Japanese Industrial Standards (JIS) or ISO standards.

Conclusion

Start with a library for speed, or write your own encoder to learn. Either way, draw it on a canvas with a quiet zone and test it with a real phone.

Editor’s Note

This guide is accurate as of December 3, 2026. Facts about QR codes come from Denso Wave pages linked above, and library details from npm and GitHub. Digieh wrote and tested the demo encoder on this page.

Spot an error in this article? Please contact us at support@digieh.com or read our Corrections Policy.

Keep exploring

Your next read is waiting

Found this useful?

Share this post
Avatar photo

Syed Wasif Raza

Syed Wasif Raza is the founder and editor of Digieh. He writes about health, food, lifestyle, news and sports in plain, easy English. Every guide he publishes is checked against official sources such as government and health agency pages. Connect with him on LinkedIn.

Articles: 998

Leave a Reply

Your email address will not be published. Required fields are marked *