美文网首页
滑块验证PC端,手机端需稍作改动

滑块验证PC端,手机端需稍作改动

作者: alongzjl | 来源:发表于2018-09-20 17:14 被阅读0次

;

(function($) {

$.fn.SliderImgPuzzle = function(setting) {

var defaults = {

callback: false

}

var setting = $.extend(defaults, setting);

const l = 42,

r = 10,

w = 300,

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 './images/random/' + getRandomNumberByRange(1,8) + '.jpg'

}

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

}

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()

}, 500)

}

})

}

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);

.block {

    position: absolute;

    left: 0;

    top: 0

}

.canvasContainer {

  /* display: none;*/

    position: absolute;

    bottom: 65px;

    background: #fff

}

.sliderImgPuzzleContainer {

    position: relative;

    text-align: center;

    width: 300px;

    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: 0;

    top: 0;

    height: 50px;

    border: 0 solid #1991fa;

    background: #d1e9fe

}

.sliderImgPuzzle {

    position: absolute;

    top: 0;

    left: 0;

    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: 0;

    top: 0;

    width: 34px;

    height: 34px;

    cursor: pointer;

    background: url(../images/icon.png) 0 -437px;

    background-size: 34px 471px

}

相关文章

网友评论

      本文标题:滑块验证PC端,手机端需稍作改动

      本文链接:https://www.haomeiwen.com/subject/yfennftx.html