Web前端图形滑块检验组件实现

组件渲染图形:

初始化:                                                                                               滑到时:

Web前端图形滑块检验组件实现Web前端图形滑块检验组件实现

验证成功:                                                                                                 验证失败:

Web前端图形滑块检验组件实现 Web前端图形滑块检验组件实现

适用场景:

  相信大家看了此组件图片后已经明白它的使用场景,在此不做过多介绍

组件内容:

  组件CSS(sliderImgPuzzle.css):

 .block {
position: absolute;
left:;
top: 0
} .canvasContainer {
/*display: none;*/
position: absolute;
bottom: 65px;
background: #fff
} .sliderImgPuzzleContainer {
position: relative;
text-align: center;
width: 325px;
height: 50px;
font-size: 14px;
line-height: 50px;
background: #f7f9fa;
color: #45494c;
border: 1px solid #e4e7eb
} .sliderImgPuzzleContainer_active .sliderImgPuzzle {
height: 48px;
top: -1px;
border: 1px solid #1991fa
} .sliderImgPuzzleContainer_active .sliderImgPuzzleMask {
height: 48px;
border-width: 1px
} .sliderImgPuzzleContainer_success .sliderImgPuzzle {
height: 48px;
top: -1px;
border: 1px solid #52ccba;
background-color: #52ccba !important
} .sliderImgPuzzleContainer_success .sliderImgPuzzleMask {
height: 48px;
border: 1px solid #52ccba;
background-color: #d2f4ef
} .sliderImgPuzzleContainer_success .sliderImgPuzzleIcon {
background-position: 0 0 !important
} .sliderImgPuzzleContainer_fail .sliderImgPuzzle {
height: 48px;
top: -1px;
border: 1px solid #f57a7a;
background-color: #f57a7a !important
} .sliderImgPuzzleContainer_fail .sliderImgPuzzleMask {
height: 48px;
border: 1px solid #f57a7a;
background-color: #fce1e1
} .sliderImgPuzzleContainer_fail .sliderImgPuzzleIcon {
top: 19px;
background-position: 0 -82px !important
} .sliderImgPuzzleContainer_active .sliderImgPuzzleText, .sliderImgPuzzleContainer_success .sliderImgPuzzleText, .sliderImgPuzzleContainer_fail .sliderImgPuzzleText {
visibility: hidden
} .sliderImgPuzzleMask {
position: absolute;
left:;
top:;
height: 50px;
border: 0 solid #1991fa;
background: #d1e9fe
} .sliderImgPuzzle {
position: absolute;
top:;
left:;
width: 50px;
height: 48px;
background: #fff;
box-shadow: 0 0 3px rgba(0, 0, 0, .3);
cursor: pointer;
transition: background .2s linear
} .sliderImgPuzzle:hover {
background: #1991fa
} .sliderImgPuzzle:hover .sliderImgPuzzleIcon {
background-position: 0 -13px
} .sliderImgPuzzleIcon {
position: absolute;
top: 19px;
left: 17px;
width: 14px;
height: 12px;
background: url(../images/icon.png) 0 -26px;
background-size: 34px 471px
} .refreshIcon {
position: absolute;
right:;
top:;
width: 34px;
height: 34px;
cursor: pointer;
background: url(../images/icon.png) 0 -437px;
background-size: 34px 471px
}

  组件js(sliderImgPuzzle.js)

 ;(function ($) {
$.fn.SliderImgPuzzle = function (setting) {
var defaults = {callback: false}
var setting = $.extend(defaults, setting);
const l = 42, r = 10, w = 325, h = 155, PI = Math.PI
const L = l + r * 2 function getRandomNumberByRange(start, end) {
return Math.round(Math.random() * (end - start) + start)
} function createCanvas(width, height) {
const canvas = createElement('canvas')
canvas.width = width
canvas.height = height
return canvas
} function createImg(onload) {
const img = createElement('img')
img.crossOrigin = "Anonymous"
img.onload = onload
img.onerror = () => {
img.src = getRandomImg()
}
img.src = getRandomImg()
return img
} function createElement(tagName) {
return document.createElement(tagName)
} function addClass(tag, className) {
tag.classList.add(className)
} function removeClass(tag, className) {
tag.classList.remove(className)
} function getRandomImg() {
return 'https://picsum.photos/300/150/?image=' + getRandomNumberByRange(0, 200)
} function draw(ctx, operation, x, y, type) {
if (type == 1)
draw1(ctx, operation, x, y); else if (type == 2)
draw2(ctx, operation, x, y); else if (type == 3)
draw3(ctx, operation, x, y); else if (type == 4)
draw4(ctx, operation, x, y); else if (type == 5)
draw5(ctx, operation, x, y); else if (type == 6)
draw6(ctx, operation, x, y);
} function draw6(ctx, operation, x, y) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + l, y)
ctx.lineTo(x + l, y + l / 2)
ctx.arc(x + l + r - 4, y + l / 2, r, 0, 2 * PI)
ctx.lineTo(x + l, y + l / 2)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x + l / 2, y + l)
ctx.arc(x + l / 2, y + l + 4, r, 0, 2 * PI)
ctx.lineTo(x + l / 2, y + l)
ctx.lineTo(x, y + l)
ctx.lineTo(x, y)
ctx.fillStyle = '#fff'
ctx[operation]()
ctx.beginPath()
ctx.arc(x + l / 2, y, r, 2 * PI, 1 * PI)
ctx.globalCompositeOperation = "xor"
ctx.fill()
} function draw5(ctx, operation, x, y) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + l / 2, y)
ctx.arc(x + l / 2, y - r + 4, r, 0, 2 * PI)
ctx.lineTo(x + l / 2, y)
ctx.lineTo(x + l, y)
ctx.lineTo(x + l, y + l / 2)
ctx.arc(x + l + r - 4, y + l / 2, r, 0, 2 * PI)
ctx.lineTo(x + l, y + l / 2)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x, y + l)
ctx.lineTo(x, y)
ctx.fillStyle = '#fff'
ctx[operation]()
ctx.beginPath()
ctx.arc(x + l / 2, y + l, r, 1 * PI, 2 * PI)
ctx.globalCompositeOperation = "xor"
ctx.fill()
} function draw4(ctx, operation, x, y) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + l / 2, y)
ctx.arc(x + l / 2, y - r + 4, r, 0, 2 * PI)
ctx.lineTo(x + l / 2, y)
ctx.lineTo(x + l, y)
ctx.lineTo(x + l, y + l / 2)
ctx.arc(x + l + r - 4, y + l / 2, r, 0, 2 * PI)
ctx.lineTo(x + l, y + l / 2)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x + l / 2, y + l)
ctx.arc(x + l / 2, y + l + 4, r, 0, 2 * PI)
ctx.lineTo(x + l / 2, y + l)
ctx.lineTo(x, y + l)
ctx.lineTo(x, y)
ctx.fillStyle = '#fff'
ctx[operation]()
ctx.beginPath()
ctx.arc(x, y + l / 2, r, 1.5 * PI, 0.5 * PI)
ctx.globalCompositeOperation = "xor"
ctx.fill()
} function draw3(ctx, operation, x, y) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + l / 2, y)
ctx.lineTo(x + l, y)
ctx.lineTo(x + l, y + l / 2)
ctx.arc(x + l + r - 4, y + l / 2, r, 0, 2 * PI)
ctx.lineTo(x + l, y + l / 2)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x + l / 2, y + l)
ctx.arc(x + l / 2, y + l + 4, r, 0, 2 * PI)
ctx.lineTo(x + l / 2, y + l)
ctx.lineTo(x, y + l)
ctx.lineTo(x, y)
ctx.fillStyle = '#fff'
ctx[operation]()
ctx.beginPath()
ctx.arc(x, y + l / 2, r, 1.5 * PI, 0.5 * PI)
ctx.globalCompositeOperation = "xor"
ctx.fill()
} function draw2(ctx, operation, x, y) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + l / 2, y)
ctx.lineTo(x + l, y)
ctx.lineTo(x + l, y + l / 2)
ctx.arc(x + l + r - 4, y + l / 2, r, 0, 2 * PI)
ctx.lineTo(x + l, y + l / 2)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x, y + l)
ctx.lineTo(x, y)
ctx.fillStyle = '#fff'
ctx[operation]()
ctx.beginPath()
ctx.arc(x, y + l / 2, r, 1.5 * PI, 0.5 * PI)
ctx.globalCompositeOperation = "xor"
ctx.fill()
} function draw1(ctx, operation, x, y) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + l / 2, y)
ctx.arc(x + l / 2, y - r + 4, r, 0, 2 * PI)
ctx.lineTo(x + l / 2, y)
ctx.lineTo(x + l, y)
ctx.lineTo(x + l, y + l / 2)
ctx.arc(x + l + r - 4, y + l / 2, r, 0, 2 * PI)
ctx.lineTo(x + l, y + l / 2)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x, y + l)
ctx.lineTo(x, y)
ctx.fillStyle = '#fff'
ctx[operation]()
ctx.beginPath()
ctx.arc(x, y + l / 2, r, 1.5 * PI, 0.5 * PI)
ctx.globalCompositeOperation = "xor"
ctx.fill()
} function sum(x, y) {
return x + y
} function square(x) {
return x * x
} class sliderImgPuzzle {
constructor({el, onSuccess, onFail, onRefresh}) {
this.el = el
this.onSuccess = onSuccess
this.onFail = onFail
this.onRefresh = onRefresh
this.isEnable = true
} init() {
this.initDOM()
this.initImg()
this.draw()
this.bindEvents()
return this;
} initDOM() {
const canvas = createCanvas(w, h)
const block = canvas.cloneNode(true)
const canvasContainer = createElement('div')
const sliderImgPuzzleContainer = createElement('div')
const refreshIcon = createElement('div')
const sliderImgPuzzleMask = createElement('div')
const sliderImgPuzzle = createElement('div')
const sliderImgPuzzleIcon = createElement('span')
const text = createElement('span')
canvasContainer.className = 'canvasContainer'
block.className = 'block'
sliderImgPuzzleContainer.className = 'sliderImgPuzzleContainer'
refreshIcon.className = 'refreshIcon'
sliderImgPuzzleMask.className = 'sliderImgPuzzleMask'
sliderImgPuzzle.className = 'sliderImgPuzzle'
sliderImgPuzzleIcon.className = 'sliderImgPuzzleIcon'
text.innerHTML = '向右滑动滑块填充拼图'
text.className = 'sliderImgPuzzleText'
const el = this.el
canvasContainer.appendChild(canvas)
canvasContainer.appendChild(refreshIcon)
canvasContainer.appendChild(block)
el.appendChild(canvasContainer)
sliderImgPuzzle.appendChild(sliderImgPuzzleIcon)
sliderImgPuzzleMask.appendChild(sliderImgPuzzle)
sliderImgPuzzleContainer.appendChild(sliderImgPuzzleMask)
sliderImgPuzzleContainer.appendChild(text)
el.appendChild(sliderImgPuzzleContainer)
Object.assign(this, {
canvas,
block,
canvasContainer,
sliderImgPuzzleContainer,
refreshIcon,
sliderImgPuzzle,
sliderImgPuzzleMask,
sliderImgPuzzleIcon,
text,
canvasCtx: canvas.getContext('2d'),
blockCtx: block.getContext('2d')
})
} initImg() {
const img = createImg(() => {
this.canvasCtx.drawImage(img, 0, 0, w, h)
this.blockCtx.drawImage(img, 0, 0, w, h)
const y = this.y - r * 2 + 2
const ImageData = this.blockCtx.getImageData(this.x, y, L + r + 4, L + r + 4)
this.block.width = L
this.blockCtx.putImageData(ImageData, 0, y)
})
this.img = img
} draw() {
this.x = getRandomNumberByRange(L + 10, w - (L + 10))
this.y = getRandomNumberByRange(10 + r * 2, h - (L + 10))
var num = Math.floor(Math.random() * 6 + 1);
draw(this.canvasCtx, 'fill', this.x, this.y, num)
draw(this.blockCtx, 'clip', this.x, this.y, num)
} clean() {
this.canvasCtx.clearRect(0, 0, w, h)
this.blockCtx.clearRect(0, 0, w, h)
this.block.width = w
} bindEvents() {
this.el.onselectstart = () => false
this.refreshIcon.onclick = () => {
this.reset()
typeof this.onRefresh === 'function' && this.onRefresh()
}
let originX, originY, trail = [], isMouseDown = false
this.sliderImgPuzzle.ctrl = this;
this.sliderImgPuzzle.addEventListener('mousedown', function (e) {
if (!this.parentElement.parentElement.classList.contains("sliderImgPuzzleContainer_success")) {
originX = e.x, originY = e.y
isMouseDown = true
}
else {
isMouseDown = false
}
if (!this.ctrl.isEnable) isMouseDown = false;
})
// this.sliderImgPuzzle.addEventListener('mouseover', function (e) {
// if (this.ctrl.isEnable)
// this.parentElement.parentElement.parentElement.querySelector(".canvasContainer").style.display = "block";
// })
// this.canvasContainer.parentElement.addEventListener('mouseleave', function (e) {
// this.querySelector(".canvasContainer").style.display = "none";
// })
document.addEventListener('mousemove', (e) => {
if (!isMouseDown) return false
const moveX = e.x - originX
const moveY = e.y - originY
if (moveX < 0 || moveX + 38 >= w) return false
this.sliderImgPuzzle.style.left = moveX + 'px'
var blockLeft = (w - 40 - 20) / (w - 40) * moveX
this.block.style.left = blockLeft + 'px'
addClass(this.sliderImgPuzzleContainer, 'sliderImgPuzzleContainer_active')
this.sliderImgPuzzleMask.style.width = moveX + 'px'
trail.push(moveY)
})
document.addEventListener('mouseup', (e) => {
if (!isMouseDown) return false
isMouseDown = false
if (e.x == originX) return false
removeClass(this.sliderImgPuzzleContainer, 'sliderImgPuzzleContainer_active')
this.trail = trail
const {spliced, TuringTest} = this.verify()
if (spliced) {
if (TuringTest) {
addClass(this.sliderImgPuzzleContainer, 'sliderImgPuzzleContainer_success')
typeof this.onSuccess === 'function' && this.onSuccess()
} else {
addClass(this.sliderImgPuzzleContainer, 'sliderImgPuzzleContainer_fail')
this.text.innerHTML = '再试一次'
this.reset()
}
} else {
addClass(this.sliderImgPuzzleContainer, 'sliderImgPuzzleContainer_fail')
typeof this.onFail === 'function' && this.onFail()
setTimeout(() => {
this.reset()
}, 1000)
}
})
} verify() {
const arr = this.trail
const average = arr.reduce(sum) / arr.length
const deviations = arr.map(x => x - average)
const stddev = Math.sqrt(deviations.map(square).reduce(sum) / arr.length)
const left = parseInt(this.block.style.left)
return {spliced: Math.abs(left - this.x) < 10, TuringTest: average !== stddev,}
} reset() {
this.sliderImgPuzzleContainer.className = 'sliderImgPuzzleContainer'
this.sliderImgPuzzle.style.left = 0
this.block.style.left = 0
this.sliderImgPuzzleMask.style.width = 0
this.clean()
this.img.src = getRandomImg()
this.draw()
} enablePuzzle(enable) {
if (enable) {
this.isEnable = true;
}
else this.isEnable = false;
}
} this.each(function () {
var $SliderImgPuzzle = $(this);
setting.el = $SliderImgPuzzle[0];
var sip = new sliderImgPuzzle(setting).init();
$SliderImgPuzzle.data("LsSliderImgPuzzle", sip);
});
}
})(jQuery);

  组件HTML(sliderImgPuzzle.html)

 <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>滑动拼图验证码</title>
<link rel="stylesheet" href="../css/sliderImgPuzzle.css">
<style>
.container {
width: 310px;
margin: 300px auto;
}
input {
display: block;
width: 290px;
line-height: 40px;
margin: 10px 0;
padding: 0 10px;
outline: none;
border:1px solid #c8cccf;
border-radius: 4px;
color:#6a6f77;
}
#msg {
width: 100%;
line-height: 40px;
font-size: 14px;
text-align: center;
}
a:link,a:visited,a:hover,a:active {
margin-left: 100px;
color: #0366D6;
} </style>
</head>
<body>
<div class="container">
<div id="silderpuzzle" style="position: relative;padding-bottom: 15px;"></div>
<div id="msg"></div>
<button id="setenable">请先设置控件为可用</button>
<button id="setdisabled">设置控件不可用</button>
<button id="reflash">刷新控件</button>
</div>
<script src="http://www.jq22.com/jquery/jquery-2.1.1.js"></script>
<script src="../js/sliderImgPuzzle.js"></script>
<script>
//--------------------滑块拼图验证控件初始化-----
var option = {
onSuccess: function () {
document.getElementById('msg').innerHTML = '验证成功!'
},
onFail: cleanMsg,
onRefresh: cleanMsg
}
$("#silderpuzzle").SliderImgPuzzle(option);
//--------------------滑块拼图验证控件初始化完成----- //--------------------获取控件对象-----
var imgPuzzle = $("#silderpuzzle").data("LsSliderImgPuzzle"); //--------------------回调函数
function cleanMsg() {
alert("回调函数")
} $("#setenable").on("click",function(){
//--------------------对象提供的enable的方法
imgPuzzle.enablePuzzle(true);
})
$("#setdisabled").on("click",function(){
//--------------------对象提供的enable的方法
imgPuzzle.enablePuzzle(false);
})
$("#reflash").on("click",function(){
//--------------------对象提供的enable的方法
imgPuzzle.reset();
})
</script>
</body>
</html>

总结:

  该组件为轻量级组件,使用简单,一看便知。支持自定义扩展,如有不会的朋友可以拿来即用,非常方便!

GitHub地址:

  https://github.com/TopSkyhua/SliderImgPuzzle

上一篇:M1事后分析汇报总结


下一篇:destoon分页