vue 画二维码及长按保存

需求

想要做如下图的二维码带文字,且能够长按保存

前期准备

  1. 一个canvas
  2. 安装qrcode(命令:npm i qrcode)

画二维码及文字

初始化画布

html 复制代码
<template>
    <div>
		<canvas ref="canvas" width="300" height="400"></canvas>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
const canvas = ref(null);
onMounted(() => {
	// 初始化画布
    let ctx = canvas.value.getContext('2d');
})
</script>

画文字

html 复制代码
<template>
    <div>
		<canvas ref="canvas" width="300" height="400"></canvas>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
const canvas = ref(null);
onMounted(() => {
	// 初始化画布
    let ctx = canvas.value.getContext('2d');
    // 填充白色
    ctx.fillStyle = "#fff";
    ctx.fillRect(0, 0, 300, 400);
    // 画文字
    ctx.font = "14px Microsoft YaHei"
    ctx.textBaseline = "middle";
    ctx.textAlign = "center";
    ctx.fillStyle = "#333333";
    ctx.fillText("简单教程,简单编程", canvas.value.width / 2, 40);
})
</script>

画二维码

二维码需要使用qrcode转换

html 复制代码
<template>
    <div>
		<canvas ref="canvas" width="300" height="400"></canvas>
    </div>
</template>

<script setup>
import QRCode from 'qrcode';
import { ref, onMounted } from 'vue';
const canvas = ref(null);
onMounted(() => {
	// 初始化画布
    let ctx = canvas.value.getContext('2d');
    // 填充白色
    ctx.fillStyle = "#fff";
    ctx.fillRect(0, 0, 300, 400);
    // 画文字
    ctx.font = "14px Microsoft YaHei"
    ctx.textBaseline = "middle";
    ctx.textAlign = "center";
    ctx.fillStyle = "#333333";
    ctx.fillText("简单教程,简单编程", canvas.value.width / 2, 40);
    // 画二维码
    QRCode.toDataURL('I am a pony!', function (err, url) {
        let img = new Image()
        img.src = url;
        img.onload = function () {
            ctx.drawImage(img, 50, 50, 200, 200);
        }
    })
})
</script>

长按保存

通过监听触摸事件的时间判定长按

html 复制代码
<template>
    <div>
        <div class="friend" @touchstart="gtouchstart()" @touchmove="gtouchmove()" @touchend="showDeleteButton()">
            <canvas ref="canvas" width="300" height="400"></canvas>
        </div>
    </div>
</template>

<script setup>
import QRCode from 'qrcode';
import { ref, onMounted } from 'vue';
const canvas = ref(null);
onMounted(() => {
    let ctx = canvas.value.getContext('2d');
    // 填充白色
    ctx.fillStyle = "#fff";
    ctx.fillRect(0, 0, 300, 400);
    // 画文字
    ctx.font = "14px Microsoft YaHei"
    ctx.textBaseline = "middle";
    ctx.textAlign = "center";
    ctx.fillStyle = "#333333";
    ctx.fillText("简单教程,简单编程", canvas.value.width / 2, 40);
    // 画二维码
    QRCode.toDataURL('I am a pony!', function (err, url) {
        let img = new Image()
        img.src = url;
        img.onload = function () {
            ctx.drawImage(img, 50, 50, 200, 200);
        }
    })
})
let timeOutEvent = null //定时器 
//长按事件设置定时器
let gtouchstart = () => {
    timeOutEvent = setTimeout(() => {
        longPress()
    }, 700)
}

//手释放,如果在200毫秒内就释放,则取消长按事件,此时可以执行onclick应该执行的事件
let showDeleteButton = () => {
    clearTimeout(timeOutEvent); //清除定时器
    if (timeOutEvent != 0) {
        timeOutEvent = 0;
    }
    return false;
}
//如果手指有移动,则取消所有事件,此时说明用户只是要移动而不是长按
let gtouchmove = () => {
    clearTimeout(timeOutEvent); //清除定时器
    timeOutEvent = 0
}
//真正长按后应该执行的内容
let longPress = () => {
    timeOutEvent = 0;
    // 创建一个链接元素
    const link = document.createElement("a");
    // 将Canvas转换为数据URL
    const dataURL = canvas.value.toDataURL();

    // 设置链接的href属性为数据URL
    link.href = dataURL;

    // 设置链接的下载属性和文件名
    link.download = "canvas_image.png";

    // 模拟点击链接进行下载
    link.click();
}
</script>

<style scoped></style>
相关推荐
十一吖i1 天前
vue3表格显示隐藏列全屏拖动功能
前端·javascript·vue.js
徐同保1 天前
tailwindcss暗色主题切换
开发语言·前端·javascript
生莫甲鲁浪戴1 天前
Android Studio新手开发第二十七天
前端·javascript·android studio
细节控菜鸡1 天前
【2025最新】ArcGIS for JS 实现随着时间变化而变化的热力图
开发语言·javascript·arcgis
菜鸟una1 天前
【微信小程序 + 消息订阅 + 授权】 微信小程序实现消息订阅流程介绍,代码示例(仅前端)
前端·vue.js·微信小程序·小程序·typescript·taro·1024程序员节
拉不动的猪1 天前
h5后台切换检测利用visibilitychange的缺点分析
前端·javascript·面试
桃子不吃李子1 天前
nextTick的使用
前端·javascript·vue.js
Devil枫1 天前
HarmonyOS鸿蒙应用:仓颉语言与JavaScript核心差异深度解析
开发语言·javascript·ecmascript
惺忪97981 天前
回调函数的概念
开发语言·前端·javascript
小二·1 天前
从零开始:使用 Vue-ECharts 实现数据可视化图表功能
vue.js·信息可视化·echarts