
本文详解如何使用 canvas api 将基于邮箱等数据动态生成的 qr 码(通过 qr server api)准确绘制到画布上,重点解决因错误创建 dom 元素导致图像无法渲染的常见问题,并提供完整、健壮的实现方案。
本文详解如何使用 canvas api 将基于邮箱等数据动态生成的 qr 码(通过 qr server api)准确绘制到画布上,重点解决因错误创建 dom 元素导致图像无法渲染的常见问题,并提供完整、健壮的实现方案。
在 Web 项目中,将用户输入信息(如姓名、职位、电话、邮箱)与动态生成的 QR 码合成一张电子名片(Business Card),是常见的前端需求。许多开发者尝试通过 qrserver.com 这类免费 QR 生成服务(如 https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=xxx)获取图片 URL,并用
✅ 正确做法是:使用 new Image() 构造函数创建合法的 元素,并在其 onload 回调中执行 drawImage()。否则,qr_img 不具备 src 和 onload 的标准行为,导致异步加载失败、绘图被跳过。
以下是优化后的核心逻辑(已整合进完整流程):
传媒企业网站系统使用热腾CMS(RTCMS),根据网站板块定制的栏目,如果修改栏目,需要修改模板相应的标签。站点内容均可在后台网站基本设置中添加。全站可生成HTML,安装默认动态浏览。并可以独立设置SEO标题、关键字、描述信息。源码包中带有少量测试数据,安装时可选择演示安装或全新安装。如果全新安装,后台内容充实后,首页才能完全显示出来。(全新安装后可以删除演示数据用到的图片,目录在https://
function generateImage() {
const name_Ar = document.getElementById("name_ar").value;
const name_Eng = document.getElementById("name_eng").value;
const jop_Ar = document.getElementById("jop_ar").value;
const jop_Eng = document.getElementById("jop_eng").value;
const phone = document.getElementById("phone").value;
const email = document.getElementById("email").value.trim();
// ✅ 正确:创建标准 Image 对象(非 createElement)
const qrImg = new Image();
// 清空画布
context.clearRect(0, 0, canvas.width, canvas.height);
// 加载背景图(示例为本地 PNG)
const bgImg = new Image();
bgImg.src = 'style/images/busseCard.png';
bgImg.onload = function () {
// 绘制背景
context.drawImage(bgImg, 0, 0, canvas.width, canvas.height);
// 绘制文字(略,保持原逻辑)
context.fillStyle = 'black';
context.font = 'bold 24px Arial';
context.textAlign = 'center';
context.fillText(name_Ar, canvas.width / 3.2, canvas.height / 2.2);
context.fillText(name_Eng, canvas.width / 1.5, canvas.height / 2.2);
context.fillText(jop_Ar, canvas.width / 3.1, canvas.height / 1.9);
context.fillText(jop_Eng, canvas.width / 1.5, canvas.height / 1.9);
context.fillText(phone, canvas.width / 3.31, canvas.height / 1.555);
context.fillText(phone, canvas.width / 1.51, canvas.height / 1.529);
context.fillText(email, canvas.width / 3.31, canvas.height / 1.423);
context.fillText(email, canvas.width / 1.51, canvas.height / 1.4);
// ✅ 关键:设置 QR 图片源并绑定 onload
// 注意:需对 email 进行 URI 编码,避免特殊字符(如 @、+)导致 URL 失效
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${encodeURIComponent(email)}`;
qrImg.onload = function () {
// 在指定位置绘制 QR 码(x, y, width, height)
context.drawImage(qrImg, canvas.width / 1.51, canvas.height / 1.4, 150, 150);
};
qrImg.onerror = function () {
console.error('QR code image failed to load:', qrUrl);
context.fillStyle = 'red';
context.font = '16px monospace';
context.fillText('QR ERROR', canvas.width / 1.51, canvas.height / 1.4 + 20);
};
qrImg.src = qrUrl; // ⚠️ 必须在 onload 绑定后赋值 src!
};
}? 关键注意事项总结:
立即学习“前端免费学习笔记(深入)”;
- new Image() 是唯一可靠方式:document.createElement("qrImage") 创建的是无效自定义标签,无图像加载能力;必须使用 Image 构造函数。
- onload 必须在 src 赋值前绑定:否则若图片缓存命中,onload 可能同步触发而未被捕获。
- 务必 encodeURIComponent() 数据:邮箱含 @、+、. 等字符,不编码会导致 QR 服务解析失败或生成错误码。
- 添加 onerror 处理:网络异常或非法 data 时提供降级反馈,提升健壮性。
- 坐标与尺寸需校准:drawImage(qrImg, x, y, width, height) 中宽高建议显式指定(如 150x150),避免拉伸失真;位置应结合实际画布尺寸调试。
完成上述修正后,点击生成按钮即可在 Canvas 上稳定显示动态 QR 码,且支持后续导出为 PNG(通过 canvas.toDataURL("image/png") 实现下载)。该方案轻量、兼容性好(支持所有现代浏览器),是构建交互式电子名片的推荐实践。










