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,










