I am using 2 canvas elements. I am using one element as a main canvas element which has all the elements. I am generating a barcode in a separate canvas and would like to add it to the main canvas. Trying for a long time. Adding an example here fiddle
$(document).ready(function() {
JsBarcode('#barcode', "1234", {
width: 4,
height: 20//displayValue: false
});
var bc = document.getElementById("barcode");
var canvasx = bc.getContext("2d");
var img1 = new Image();
img1.src = bc.toDataURL("image/png");
var c = document.getElementById("labelDesigner");
var canvas = bc.getContext("2d");
c.drawImage(img1);
});
I am using facric.js and JsBarcode. Thanks for the help in advance. If there is any other better way kindly suggest
The
drawImage
-function can take another canvas as an argument, so you do not have to convert to an image inbetween. However, it always requires the x and y coordinates as the second and third parameters.You had also swapped your
c
andcanvas
variables:you can use fabric.Image.fromURL and pass the barcode canvas data as url.
DEMO