[英]Merging two canvas together
so I am using a JS library to generate a QR Code. 所以我正在使用JS库来生成QR码。 The JS library generates the QR code by rendering it to a canvas.
JS库通过将QR代码呈现到画布来生成QR代码。
However I would like to have a background behind this QR Code, and I tried merging them together by calling the QR code library after the background has been drawn. 但是,我希望此QR代码具有背景,因此我尝试在绘制背景后通过调用QR代码库将它们合并在一起。 Yet this doesnt work, and the QR code replaces the background.
但这行不通,QR码取代了背景。
Can someone help me out pls? 有人可以帮我吗? Here's the code to call the QR code library:
这是调用QR代码库的代码:
QrCodeWithLogo.toCanvas({
canvas: temp_canvas,
content: 'https://www.google.com',
width: 510,
logo: {
src: '/assets/mylogo.png',
borderRadius: 10,
borderSize : 0
}
Thanks so much! 非常感谢!
This can be done by using two canvases. 这可以通过使用两个画布来完成。 The first being your main canvas where the final composed image should be drawn onto and a temporary canvas which is used to generate the qr code.
第一个是您要在其上绘制最终合成图像的主画布,以及一个用于生成qr代码的临时画布。
You just have to make sure that 您只需要确保
Afterwards you can compose the final image by using the canvas's .drawImage() method. 之后,您可以使用画布的.drawImage()方法合成最终图像。
Here's an example: 这是一个例子:
var tempCanvas = document.createElement("canvas"); var realCanvas = document.getElementById("realCanvas"); var context = realCanvas.getContext("2d"); QrCodeWithLogo.toCanvas({ canvas: tempCanvas, content: 'https://www.google.com', width: 256, logo: { src: 'https://picsum.photos/id/76/100/100', borderRadius: 10, borderSize: 0 } }).then(function() { var image = new Image(); image.onload = function() { context.drawImage(this, 0, 0, 400, 400); context.drawImage(tempCanvas, realCanvas.width / 2 - tempCanvas.width / 2, realCanvas.height / 2 - tempCanvas.height / 2) } image.src = "https://picsum.photos/id/79/400/400"; })
<script src="https://unpkg.com/qr-code-with-logo@1.1.0/lib/qr-code-with-logo.browser.min.js"></script> <canvas id="realCanvas" width="400" height="400"></canvas>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.