Introduction

The Barcode shape allows the programmer to add a barcode into the label. This document describes the operation flow of this object.

Object overview

The barcode is basically a number, encoded using a specific format, that gets rendered into an svg representation using the packagage https://www.npmjs.com/package/jsbarcode . The Barcode object should be able to:

  • Render a specific barcode from his numeric representation

  • Render a text at the bottom, with the content

  • Render a placeholder when no code is specified (allow the user to not specify it)

  • Expose utils for setting the number, changing the encoding and check if there is a value

  • Be edited as all fabric objects (borders, text options, etc…)

Code examples

In the following example, we can get a url of the image representing the barcode. Barcode will load the image into an Image object and display it.

function textToBase64Barcode(text){
      var canvas = document.createElement("canvas");
      JsBarcode(canvas, text, {format: "CODE39"});
      return canvas.toDataURL("image/png");
    }
JS

Adding object to layer

The following snippet can add a Barcode to a layer.

layer.add(new Barcode({
      placeHolder: 'Barcode',
      code: 'myfjfkls',
      encoding: 'CODE96',
      width: 300,
      height: 50,
      codeLineWidth: 2,
    }));
TYPESCRIPT

Barcode properties

Property

Description

Type

Optional

Default

code

The code to transform into a barcode

string

(tick)

Empty

transparentBackground

Indicates if the background of a Barcode is transparent or specifies a color for it.

boolean

(tick)

true

codeLineWidth

The width of the lines for code rendering

number

(tick)

2

encoding

The encoding specification name following https://www.npmjs.com/package/jsbarcode

string

(tick)

CODE-128

showContent

Indicates if this object should render also a string containing the code attribute or not.

boolean

(tick)

false

contentOptions

Specifies the formatting of the code label, if it is rendered

ITextConfiguration

(tick)

{
  textAlign: Alignment.Center,
  bold: false,
  fontName: 'arial',
  italic: false,
  textColor: 'black',
  fontSize: 10,
  padding: 0,
}
CODE

placeHolder

The text to be rendered when there is no code specified

string

(tick)

Empty

placeHolderOptions

Text options for the placeholder text

ITextConfiguration

(tick)

{
  textAlign: Alignment.Center,
  bold: false,
  fontName: 'arial',
  italic: false,
  textColor: 'black',
  fontSize: 10,
  padding: 0,
}
CODE