使用konva和vue-konva库实现拖拽滑块验证功能
程序员文章站
2022-07-04 17:02:08
1. 在vue项目中安装konva和vue-konva库npm install konva vue-konva --save-dev2. 引入vue-konva库import vuekonva fro...
1. 在vue项目中安装konva
和vue-konva
库
npm install konva vue-konva --save-dev
2. 引入vue-konva
库
import vuekonva from ‘vue-konva';
vue.use(vuekonva)
3. 创建单独的滑块验证组件 captcha.vue
,在相应的页面中引入使用即可
<template> <v-stage :config="config.stage"> <v-layer ref="layer"> <!-- 背景组 --> <v-group :config="config.group"> <v-rect :config="config.rect"></v-rect> <v-text :config="config.text"></v-text> </v-group> <!-- 遮罩层组 --> <v-group :config="config.group"> <v-rect :config="config.coverrect" ref="coverrect"></v-rect> <v-text :config="config.covertext" v-if="success" ref="covertext"></v-text> </v-group> <!-- 滑块组 --> <v-group :config="config.movegroup" ref="movegroup" @mouseover="movegroupmouseover" @mouseout="movegroupmouseout" @mousedown="movegroupmousedown" @mouseup="movegroupstop"> <v-rect :config="config.moverect" ref="moverect"></v-rect> <!-- 验证成功组 --> <v-group :config="config.group" v-if="success"> <v-circle :config="config.succcircle" ref="succcircle"></v-circle> <v-line :config="config.succline"></v-line> </v-group> <v-group :config="config.movegroup_l" v-else> <v-line :config="config.moveline1"></v-line> <v-line :config="config.moveline2"></v-line> </v-group> </v-group> </v-layer> </v-stage> </template> <script> /* * captchaconfig // 属性 {width:330, height: 36} 组件的宽高 * eventcaptcha // 验证成功的回调 */ let _$mousedown = false; // 鼠标是否在滑块组中按下,因为和html没有绑定,所以没有放在data中,并以_$开头 export default { props: { captchaconfig: { type: object, default: () => ({ width: 330, // 宽度 height: 36, // 高度 }), }, }, data() { const { width, height } = this.captchaconfig; let config = { stage: { width: width, height: height, }, group: { x: 0, y: 0, }, rect: { width: width, height: height, fill: '#e8e8e8', }, text: { x: 0, y: 0, width: width, height: height, text: '请按住滑块,拖动到最右边', fontsize: 14, fontfamily: '微软雅黑', align: 'center', lineheight: parsefloat(height / 14), }, //滑块组 movegroup: { draggable: true, }, moverect: { x: 0.5, y: 0.5, width: height - 1, height: height - 1, fill: '#fff', stroke: '#8d92a1', strokewidth: 1, }, movegroup_l: { x: height / 3, y: height / 3, }, moveline1: { x: 0, y: 0, points: [0, 0, height / 6, height / 6, 0, height / 3], stroke: '#8d92a1', strokewidth: 1, linecap: 'round', linejoin: 'round', }, moveline2: { x: height / 6, y: 0, points: [0, 0, height / 6, height / 6, 0, height / 3], stroke: '#8d92a1', strokewidth: 1, linecap: 'round', linejoin: 'round', }, //创建遮罩层组 coverrect: { width: height / 2, height: height, fill: '#8d92a1', opacity: 0.8, }, covertext: { x: 0, y: 0, width: width - height, height: height, align: 'center', text: '验证成功', fontsize: 14, fontfamily: '微软雅黑', fontstyle: 'bold', fill: '#fff', lineheight: parsefloat(height / 14), }, //验证成功组 succcircle: { x: height / 2, y: height / 2, radius: height / 4, fill: '#8d92a1', }, succline: { points: [height / 2 - height / 4 / 2, height / 2, height / 2 - height / 4 / 8, height / 2 + height / 4 / 2, height / 2 + height / 4 / 2, height / 2 - height / 4 / 2], stroke: '#fff', strokewidth: 1, linecap: 'round', linejoin: 'round', }, }; return { config, success: 0, // 标记是否验证成功 0 失败 1 成功 }; }, mounted() { // 给document绑定鼠标抬起事件 document.addeventlistener('mouseup', this.movegroupstop); // 在组件注销的时候取消绑定 this.$once('hook:beforedestroy', () => { document.removeeventlistener('mouseup', this.movegroupstop); }); // 给滑块组绑定拖拽监听 this.$refs.movegroup.getnode().dragboundfunc((pos) => { const { width, height } = this.captchaconfig; let movegroup = this.$refs.movegroup.getnode(); let moverect = this.$refs.moverect.getnode(); let coverrect = this.$refs.coverrect.getnode(); let movex = movegroup.getattrs().x ? movegroup.getattrs().x : 0; coverrect.width(movex + height / 2); if (pos.x >= width - height) { if (this.success == 0) { this.success = 1; this.$emit('eventcaptcha'); } coverrect.opacity(1); } if (this.success == 0) { if (pos.x < 0) { return { x: 0, y: movegroup.getabsoluteposition().y, }; } else if (pos.x > width - height) { return { x: width - height, y: movegroup.getabsoluteposition().y, }; } else { return { x: pos.x, y: movegroup.getabsoluteposition().y, }; } } else { return { x: width - height, y: movegroup.getabsoluteposition().y, }; } }); }, methods: { // 鼠标进入滑块组 movegroupmouseover() { document.body.style.cursor = 'pointer'; }, // 鼠标移出滑块组 movegroupmouseout() { document.body.style.cursor = 'default'; }, // 鼠标按下 movegroupmousedown() { _$mousedown = true; // 只有在滑块组点击鼠标才被视作要点击滑动验证 }, // 鼠标抬起 movegroupstop(e) { if (!_$mousedown) return; _$mousedown = false; document.body.style.cursor = 'default'; // 鼠标恢复指针状态 if (this.success == 0) { this.$refs.movegroup.getnode().to({ x: 0, duration: 0.3, }); this.$refs.coverrect.getnode().to({ width: this.captchaconfig.height / 2, duration: 0.3, }); } }, }, }; </script>
4. 最终效果
简单的滑块验证功能实现,可直接在vue页面中引入使用。konva库:
到此这篇关于使用konva和vue-konva完成前端拖拽滑块验证功能的实现代码的文章就介绍到这了,更多相关konva和vue-konva拖拽滑块验证内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: CountDownLatch源码解析
下一篇: jedis