组件渲染图形:

初始化:                                                                                               滑到时:

验证成功:                                                                                                 验证失败:

 

适用场景:

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

组件内容:

  组件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

Web前端图形滑块检验组件实现的更多相关文章

  1. 二十三、【开源】EFW框架Web前端开发之常用组件(FusionCharts图表、ReportAll报表等)

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...

  2. 前端组件库 - 搭建web app常用的样式/组件等收集列表(移动优先)

    0. 前端自动化(Workflow) 前端构建工具 Webpack - module bundler Yeoman - a set of tools for automating developmen ...

  3. web 前端常用组件【05】ZTree

    web 项目或多或少都会有涉及到什么人员职称树,菜单树,组织机构树等. 历手三四个项目有大有小,采用的树前端都是 Ztree. 有些优秀的J2EE 框架将这些常用的组件都封装起来,作为模块化的组件提供 ...

  4. Web前端性能优化教程04:压缩组件

    本文是Web前端性能优化系列文章中的第四篇,主要讲述内容:压缩组件.完整教程可查看:Web前端性能优化 基础知识 gzip编码:gzip是GUNzip的缩写,是使用无损压缩算法的一种,最早是用于Uni ...

  5. web前端开发常用组件

    web前端开发常用组件 1. 对话框(dialog):jbox(适合对话框等其它功能).colorbox(也很强大,可以弥补jbox图片轮播的落点),      这二者基本能搞定所有对话框的情况 2. ...

  6. Web 前端 UI 组件库文档自动化方案 All In One

    Web 前端 UI 组件库文档自动化方案 All In One 需求 自动化 动态 好用 markdown element-ui 中示例和说明按照一定规则写在md文件中,调用md-loader将md文 ...

  7. 整理六百篇web前端知识混总

    9个有用的和免费的工具来支持动态网页开发 8个基本的引导工具的网页设计师 11款CSS3动画工具的开发 2016年某前端群题目答案参考 9最好的JavaScript压缩工具 创建响应式布局的10款优秀 ...

  8. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】

    2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...

  9. HTML5零基础学习Web前端需要知道哪些?

    HTML零基础学习Web前端网页制作,首先是要掌握一些常用标签的使用和他们的各个属性,常用的标签我总结了一下有以下这些: html:页面的根元素. head:页面的头部标签,是所有头部元素的容器. b ...

随机推荐

  1. BZOJ 3813--奇数国(线段树&欧拉函数&乘法逆元&状态压缩)

    3813: 奇数国 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 755  Solved: 432[Submit][Status][Discuss] ...

  2. linux安装spark-2.3.0集群

    (安装spark集群的前提是服务器已经配置了jdk并且安装hadoop集群(主要是hdfs)并正常启动,hadoop集群安装可参考<hadoop集群搭建(hdfs)>) 1.配置scala ...

  3. CentOS7系统安装 Maria Db(MYSQL)教程

    一.背景Maria Db是流行的跨平台MySQL数据库管理系统的分支,被认为是MySQL 的完全替代品.Maria Db是由Sun在Sun Micro systems合并期间被Oracle收购后,于2 ...

  4. 线索二叉树的理解和实现(Java)

    线索二叉树的基本概念 我们按某种方式对二叉树进行遍历,将二叉树中所有节点排序为一个线性序列,在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结 ...

  5. C#-WebForm-LinQ-条件精确查询、高级查询

    前台界面,并在后台绑定数据 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ca ...

  6. pyserial timeout=1 || timeout=0.01

    昨天在做串口通信时候发现,串口参数(timeout=1 || timeout=0.01)对通信的读数据竟然影响很大,代码如下: self.ser = serial.Serial(port=serial ...

  7. Mac休眠之后唤醒时无法使用鼠标

    Mac休眠之后唤醒时,无法使用鼠标键盘,无法输入密码登录,只能重启. 尝试升级系统,问题依旧,最后在Google的帮助下,问题解决. 解决办法:系统偏好设置->节能->取消硬盘休眠 参考: ...

  8. hibernate关联关系的crud2

    hibernate关联关系的CRUD操作,解释都在注释里了,讲了fetchType.cascade. User类: package com.oracle.hibernate; import javax ...

  9. guava学习:guava集合类型-Bimap

    学习guava让我惊喜的第二个接口就是:Bimap BiMap是一种特殊的映射其保持映射,同时确保没有重复的值是存在于该映射和一个值可以安全地用于获取键背面的倒数映射. 最近开发过程中,经常会有这种根 ...

  10. Mac 安装tensorflow

    一. 安装 TensorFlow谷歌的官网和开源项目都有介绍各个系统的安装和使用(官网:https://www.tensorflow.org/install git: https://github.c ...