jquery canvas generates posters with QR codes
- 2021-12-01 20:39:30
- OfStack
In this paper, we share the specific code of jquery canvas poster with 2-dimensional code for your reference. The specific contents are as follows
Requirements: Click on the picture pop-up window to generate posters with 2D codes.
Related problems encountered:
1. The generated picture will be blurred and unclear.
2. There are differences in text position and font size between Apple and Android phones.
Introduce the required files
//jquery.js
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
// Solve the problem that the generated picture is fuzzy and unclear
<script src="/images/202010/hidpi-canvas.min.js"></script>
Generate poster pictures
<script>
// Execute code
$(function () {
// Pixel density Without this code, the generated picture may be blurred
function getPixelRatio(context) {
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
return (window.devicePixelRatio || 1) / backingStore;
};
// Click event
$(".myImg").click(function () {
$(".dialog").fadeIn();
var dialogSrc = $(this).attr("src")
var csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
//2. Send a request
$.ajax({
url: "xxxx",
type: "post",
dataType: 'json',
headers: {
'X-CSRF-TOKEN': csrfToken
}, // Set the request header
success: function (res) {
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
var ratio = getPixelRatio(context)
canvas.width = 262 * ratio;
canvas.height = 450 * ratio;
context.rect(0, 0, canvas.width * ratio, canvas.height * ratio);
context.fillStyle = "#fff";
context.fill();
var myImage2 = new Image();
// Background map
myImage2.src = dialogSrc
// Acquisition terminal
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android Terminal
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios Terminal
myImage2.onload = function () {
context.drawImage(myImage2, 0, 0, 262 * ratio, 450 * ratio);
// Text
var text = res.data.company + ',' + res.data.sales;
var str = new Array();
str = text.split(",");
var uphight = 0
// Solve Apple phones and Android phones Display difference between text position and font size
for (var i = 0; i < str.length; i++) {
if (isAndroid) {
context.font = "12px Calibri";
context.fillText(str[i], 70, 390 + uphight)
uphight += 20
}
if (isiOS) {
context.font = "20px Calibri";
context.fillText(str[i], 140, 450 * ratio - 120 + uphight)
uphight += 40
}
}
var myImage = new Image();
//2 Dimension code picture
myImage.src = res.data.wxcode
myImage.crossOrigin = 'Anonymous';
myImage.onload = function () {
context.drawImage(myImage, 30, 380 * ratio, 48 * ratio, 50 * ratio);
var base64 = canvas.toDataURL("image/jpeg", 1.0);
var img = document.getElementById('myPoster');
img.setAttribute('src', base64);
}
}
},
error: function (e) {
console.log('ajax Request an exception with the following exception information: ', e);
}
});
});
// Close the pop-up window
$(".close").click(function () {
$(".dialog").fadeOut();
})
});
</script>