如何用原生JavaScript制作含图片的长尾二维码?

2026-04-05 20:521阅读0评论SEO基础
  • 内容介绍
  • 相关推荐

本文共计433个文字,预计阅读时间需要2分钟。

如何用原生JavaScript制作含图片的长尾二维码?

本文主要介绍了如何基于原生JavaScript生成带图片的二维码,通过示例代码展示了其应用,适合对二维码生成有兴趣的学习者或工作者参考。需要的伙伴可以参考以下链接生成二维码。

这篇文章主要介绍了如何基于原生javaScript生成带图片的二维码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

使用链接生成二维码主要是使用qr.js或者其他,把链接转化为二维码的形式,在使用canvas时需要设置画布的尺寸,生成的颜色。

<div class="qr_code"> <img src="" id="imgcode" /> <canvas ref="canvas" hidden></canvas> <div> js function createQr () { // 生成带图片二维码 const qrcode = qr('baidu.com') // 转化链接 const canvas = this.$refs.canvas const ctx = canvas.getContext('2d') const size = 128 / qrcode.moduleCount //128设置的二维码尺寸 const scale = window.devicePixelRatio / getPixelRatio(ctx) canvas.height = canvas.width = 128e * scale ctx.scale(scale, scale) qrcode.modules.forEach((row, rdx) => { row.forEach((cell, cdx) => { ctx.fillStyle = cell ? '#000' : '#fff' // 设置二维码颜色和背景颜色 var w = (Math.ceil((cdx + 1) * size) - Math.floor(cdx * size)) ctx.fillRect(Math.round(cdx * size), Math.round(rdx * size), w, w) }) }) var image = document.createElement('img') var imgcode = document.getElementById('imgcode') image.src = 'img.558idc.com/uploadfile/allimg/js/logo.png' //二维码中间图标 image.onload = () => { var dwidth = 128 * 0.2 // 设置图片大小 var dx = (128 - dwidth) / 2 var dheight = image.height / image.width * dwidth var dy = (this.size - dheight) / 2 image.width = dwidth image.height = dheight ctx.drawImage(image, dx, dy, dwidth, dheight) imgcode.src = canvas.toDataURL() } }, getPixelRatio (ctx) { return ctx.webkitBackingStorePixelRatio || ctx.backingStorePixelRatio || 1 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

如何用原生JavaScript制作含图片的长尾二维码?

本文共计433个文字,预计阅读时间需要2分钟。

如何用原生JavaScript制作含图片的长尾二维码?

本文主要介绍了如何基于原生JavaScript生成带图片的二维码,通过示例代码展示了其应用,适合对二维码生成有兴趣的学习者或工作者参考。需要的伙伴可以参考以下链接生成二维码。

这篇文章主要介绍了如何基于原生javaScript生成带图片的二维码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

使用链接生成二维码主要是使用qr.js或者其他,把链接转化为二维码的形式,在使用canvas时需要设置画布的尺寸,生成的颜色。

<div class="qr_code"> <img src="" id="imgcode" /> <canvas ref="canvas" hidden></canvas> <div> js function createQr () { // 生成带图片二维码 const qrcode = qr('baidu.com') // 转化链接 const canvas = this.$refs.canvas const ctx = canvas.getContext('2d') const size = 128 / qrcode.moduleCount //128设置的二维码尺寸 const scale = window.devicePixelRatio / getPixelRatio(ctx) canvas.height = canvas.width = 128e * scale ctx.scale(scale, scale) qrcode.modules.forEach((row, rdx) => { row.forEach((cell, cdx) => { ctx.fillStyle = cell ? '#000' : '#fff' // 设置二维码颜色和背景颜色 var w = (Math.ceil((cdx + 1) * size) - Math.floor(cdx * size)) ctx.fillRect(Math.round(cdx * size), Math.round(rdx * size), w, w) }) }) var image = document.createElement('img') var imgcode = document.getElementById('imgcode') image.src = 'img.558idc.com/uploadfile/allimg/js/logo.png' //二维码中间图标 image.onload = () => { var dwidth = 128 * 0.2 // 设置图片大小 var dx = (128 - dwidth) / 2 var dheight = image.height / image.width * dwidth var dy = (this.size - dheight) / 2 image.width = dwidth image.height = dheight ctx.drawImage(image, dx, dy, dwidth, dheight) imgcode.src = canvas.toDataURL() } }, getPixelRatio (ctx) { return ctx.webkitBackingStorePixelRatio || ctx.backingStorePixelRatio || 1 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

如何用原生JavaScript制作含图片的长尾二维码?