@syncfusion/ej2-barcode-generator
Version:
Barcode generator component is a pure JavaScript library which will convert a string to Barcode and show it to the user. This supports major 1D and 2D barcodes including coda bar, code 128, QR Code.
79 lines (78 loc) • 2.86 kB
JavaScript
import { createHtmlElement } from '../utility/dom-util';
/**
* canvas renderer
*/
/** @private */
var BarcodeCanvasRenderer = /** @class */ (function () {
function BarcodeCanvasRenderer() {
}
/**
* Get the context value for the canvas.\
*
* @returns {CanvasRenderingContext2D} Get the context value for the canvas .
* @param {HTMLCanvasElement} canvas - Provide the canvas element .
* @private
*/
BarcodeCanvasRenderer.getContext = function (canvas) {
return canvas.getContext('2d');
};
/**
* Draw the root element for the barcode.\
*
* @returns {HTMLElement} Draw the barcode SVG .
* @param {Object} attribute - Provide the canvas element .
* @param {string} backGroundColor - Provide the canvas element .
* @param {number} width - Provide the canvas element .
* @param {number} height - Provide the canvas element .
* @private
*/
// eslint-disable-next-line
BarcodeCanvasRenderer.prototype.renderRootElement = function (attribute, backGroundColor, width, height) {
var canvasObj = createHtmlElement('canvas', attribute);
var ctx = canvasObj.getContext('2d');
ctx.fillStyle = backGroundColor;
ctx.fillRect(0, 0, width, height);
return canvasObj;
};
/**
* Draw the rect for the barcode.\
*
* @returns {HTMLElement} Draw the barcode SVG .
* @param {Object} canvas - Provide the canvas element .
* @param {Object} attribute - Provide the canvas element .
* @private
*/
BarcodeCanvasRenderer.prototype.renderRect = function (canvas, attribute) {
var ctx = canvas.getContext('2d');
if (attribute.imageSource) {
var image_1 = new Image();
image_1.src = attribute.imageSource;
image_1.onload = function () {
ctx.drawImage(image_1, attribute.x, attribute.y, attribute.width, attribute.height);
};
}
else {
ctx.fillStyle = attribute.color;
ctx.fillRect(attribute.x, attribute.y, attribute.width, attribute.height);
}
return canvas;
};
/**
* Draw the text for the barcode.\
*
* @returns {HTMLElement} Draw the barcode SVG .
* @param {Object} canvas - Provide the canvas element .
* @param {Object} attribute - Provide the canvas element .
* @private
*/
BarcodeCanvasRenderer.prototype.renderText = function (canvas, attribute) {
var ctx = canvas.getContext('2d');
ctx.save();
ctx.font = (attribute.stringSize) + 'px ' + attribute.fontStyle;
ctx.fillStyle = attribute.color;
ctx.fillText(attribute.string, attribute.x, attribute.y);
return canvas;
};
return BarcodeCanvasRenderer;
}());
export { BarcodeCanvasRenderer };