js canvas實(shí)現(xiàn)滑塊驗(yàn)證
本文實(shí)例為大家分享了js canvas實(shí)現(xiàn)滑塊驗(yàn)證的具體代碼,供大家參考,具體內(nèi)容如下
滑塊驗(yàn)證
話不多說先上代碼想用的小伙伴可以直接使用,想了解的我后面會(huì)說下我的思路

<template>
<div class="sliderContent">
<div class="imgDev" :style="'width:' + width + 'px;'">
<canvas :id="id" :width="width" :height="height"></canvas>
<canvas
class="slider"
:id="id + 'sliderBlock'"
:width="width"
:height="height"
:style="'left:' + sliderLeft + 'px;'"
></canvas>
</div>
<div class="moveSLider" :style="'width:' + width + 'px'">
<div class="react" @mousedown.stop="moveBall($event)">
<div
class="yuan"
:style="'left:' + (sliderLeft + 10) + 'px;'"
></div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
width: 200,//盒子的寬度
height: 200,//盒子的高度,當(dāng)設(shè)置圖片原比例顯示的時(shí)候這個(gè)參數(shù)就不好使了
id: new Date().getTime(),
r: 9, //半圓的半徑
w: 40, //滑塊的寬度
imgOriginalScale: true, //圖片是否顯示原來比例
sliderLeft: 0,//滑塊的初始位置
rangeValue:4,//當(dāng)滑塊到什么范圍內(nèi)算正確
imgsrc:require("../assets/img/ver-2.png")//引入你想要的背景圖片
};
},
mounted() {
this.init();
},
methods: {
init() {
this.loadImage();
},
loadImage() {//加載圖片
let mainDom = document.getElementById(this.id);
let bg = mainDom.getContext("2d");
let blockDom = document.getElementById(this.id + "sliderBlock");
let block = blockDom.getContext("2d");
let imgsrc = this.imgsrc;
let img = document.createElement("img");
img.style.objectFit = "scale-down";
img.src = imgsrc;
img.onload = () => {
if (this.imgOriginalScale) {
//根據(jù)圖片的尺寸變化計(jì)算一下圖片原來的比例
mainDom.height = (img.height / img.width) * mainDom.width;
blockDom.height = (img.height / img.width) * blockDom.width;
}
bg.drawImage(img, 0, 0, mainDom.width, mainDom.height);
this.drawBlock(bg, mainDom.width, mainDom.height, "fill"); //繪制滑塊部分
this.drawBlock(block, blockDom.width, blockDom.height, "clip", img); //繪制滑塊部分 這里注意一定要先剪裁然后在繪制圖片(這里圖片要傳進(jìn)去不然沒有辦法控制)
};
},
drawBlock(ctx, width, height, type, img) {//這里是二合一函數(shù),可以畫出陰影部分也切割出拼圖形狀的函數(shù)
let { w, r, sliderLeft } = this;
//這地方用隨機(jī)數(shù)每次顯示的位置都不同
var x = this.random(30, width - w - r - 1); //這里最大值為了不讓滑塊進(jìn)入隱藏所以要減去滑塊的寬度 有個(gè)半圓所以要減去半圓位置
var y = this.random(10, height - w - r - 1);
if (type == "clip") {//這里要保證在兩個(gè)東西要在同一個(gè)y值上
x = sliderLeft;
y = this.y;
} else {
this.x = x;
this.y = y;
}
let PI = Math.PI;
//繪制
ctx.beginPath();
//left
ctx.moveTo(x, y);
//top
ctx.arc(x + (w + 5) / 2, y, r, -PI, 0, true);
ctx.lineTo(x + w + 5, y);
//right
ctx.arc(x + w + 5, y + w / 2, r, 1.5 * PI, 0.5 * PI, false);
ctx.lineTo(x + w + 5, y + w);
//bottom
ctx.arc(x + (w + 5) / 2, y + w, r, 0, PI, false);
ctx.lineTo(x, y + w);
ctx.arc(x, y + w / 2, r, 0.5 * PI, 1.5 * PI, true);
ctx.lineTo(x, y);
if (type == "clip") {
ctx.shadowBlur = 10;
ctx.shadowColor = "black";
}
ctx.lineWidth = 1;
ctx.fillStyle = "rgba(0, 0, 0, 0.4)"; //設(shè)置背景顏色
ctx.stroke();
ctx[type]();
if (img) {
ctx.drawImage(img, -this.x, 0, width, height);
}
ctx.globalCompositeOperation = "xor";
},
random(min, max) {
return parseInt(Math.floor(Math.random() * (max - min)) + min);
},
moveBall(e) {//當(dāng)點(diǎn)擊小紅球的時(shí)候
var oldx = e.pageX;
document.onmousemove = (e) => {//這里要綁定document對(duì)象不然你離開的他就不動(dòng)了
var x = e.pageX;
if(this.sliderLeft+x-oldx<=0){//這里判斷左邊界
this.sliderLeft = 0;
}else if(this.sliderLeft+x-oldx>=this.width-this.r*2-this.w){//這里判斷右邊界
this.sliderLeft = this.width-this.r*2-this.w;
}else{
this.sliderLeft += x - oldx;
}
oldx = x;
};
this.laveBall();
},
laveBall() {//鼠標(biāo)松開的時(shí)候清空狀態(tài)
document.onmouseup = ()=> {
document.onmousemove = null;
if(this.sliderLeft<(this.x+this.rangeValue)&&this.sliderLeft>(this.x-this.rangeValue)){
console.log("恭喜你成功了")
}else{//當(dāng)沒用選中的時(shí)候重置一下滑塊的位置
this.sliderLeft = 0;
}
};
},
},
};
</script>
<style lang="scss" scoped>
.moveSLider {
position: relative;
margin: 0 auto;
height: 50px;
.react {
.yuan {
position: absolute;
left: 0;
top: 50%;
transform: translate(0, -50%);
width: 30px;
height: 30px;
background-color: red;
border-radius: 50%;
cursor: pointer;
}
position: absolute;
left: 0;
top: 50%;
transform: translate(0, -50%);
width: 100%;
height: 20px;
background-color: rosybrown;
}
}
.imgDev {
position: relative;
margin: 0 auto;
.slider {
position: absolute;
left: 0;
top: 0;
background-color: transparent;
}
}
</style>
這里我總結(jié)了下我遇到的難點(diǎn)
1.在開始的時(shí)候我不知道怎么畫這個(gè)拼圖的形狀,后來百度發(fā)現(xiàn)其實(shí)很簡(jiǎn)單,就是用半圓和線拼接起來形成的圖形就是拼圖的形狀
2.怎么能把一個(gè)圖片只顯示拼圖那一塊呢,這也非常簡(jiǎn)單就是用ctx.clip()這個(gè)函數(shù)就可以實(shí)現(xiàn),這里需要注意的是,你要先剪裁然后再加載圖片在canvas中不然他就無法剪裁。
關(guān)鍵代碼
drawBlock(ctx, width, height, type, img) {//這里是二合一函數(shù),可以畫出陰影部分也切割出拼圖形狀的函數(shù)
let { w, r, sliderLeft } = this;//w寬度,r圓的半徑sliderLeft是滑塊的初始位置
//這地方用隨機(jī)數(shù)每次顯示的位置都不同
var x = this.random(30, width - w - r - 1); //這里最大值為了不讓滑塊進(jìn)入隱藏所以要減去滑塊的寬度 有個(gè)半圓所以要減去半圓位置
var y = this.random(10, height - w - r - 1);
if (type == "clip") {//這里要保證在兩個(gè)東西要在同一個(gè)y值上
x = sliderLeft;
y = this.y;
} else {
this.x = x;
this.y = y;
}
let PI = Math.PI;
//繪制
ctx.beginPath();
//left
ctx.moveTo(x, y);
//top
ctx.arc(x + (w + 5) / 2, y, r, -PI, 0, true);
ctx.lineTo(x + w + 5, y);
//right
ctx.arc(x + w + 5, y + w / 2, r, 1.5 * PI, 0.5 * PI, false);
ctx.lineTo(x + w + 5, y + w);
//bottom
ctx.arc(x + (w + 5) / 2, y + w, r, 0, PI, false);
ctx.lineTo(x, y + w);
ctx.arc(x, y + w / 2, r, 0.5 * PI, 1.5 * PI, true);
ctx.lineTo(x, y);
if (type == "clip") {
ctx.shadowBlur = 10;
ctx.shadowColor = "black";
}
ctx.lineWidth = 1;
ctx.fillStyle = "rgba(0, 0, 0, 0.4)"; //設(shè)置背景顏色
ctx.stroke();
ctx[type]();
if (img) {//這里為什么要在這里加載圖片呢,因?yàn)檫@個(gè)高度是動(dòng)態(tài)的必須計(jì)算完之后在放進(jìn)去
//還有個(gè)原因是你要先剪裁在加載圖片
ctx.drawImage(img, -this.x, 0, width, height);
}
},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
頁面圖片浮動(dòng)左右滑動(dòng)效果的簡(jiǎn)單實(shí)現(xiàn)案例
本篇文章主要是對(duì)頁面圖片浮動(dòng)左右滑動(dòng)效果的簡(jiǎn)單實(shí)現(xiàn)案例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02
利用JS實(shí)現(xiàn)機(jī)器人總動(dòng)員小游戲
這篇文章主要介紹了如何利用HTML+CSS+JS編寫一個(gè)機(jī)器人總動(dòng)員小游戲,代碼簡(jiǎn)單易懂對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
JavaScript利用normalizr實(shí)現(xiàn)復(fù)雜數(shù)據(jù)轉(zhuǎn)換
當(dāng)我們需要進(jìn)行數(shù)據(jù)轉(zhuǎn)換以便拆分和維護(hù)時(shí),可以使用redux作者 Dan Abramov 編寫的normalizr來處理數(shù)據(jù),本文將為大家詳細(xì)講講其用法,感興趣的可以了解一下2022-07-07
JavaScript實(shí)現(xiàn)簡(jiǎn)單的倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
將HTML的左右尖括號(hào)等轉(zhuǎn)義成實(shí)體形式的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了將HTML的左右尖括號(hào)等轉(zhuǎn)義成實(shí)體形式的兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2014-05-05
JavaScript實(shí)現(xiàn)購(gòu)物車基本功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)購(gòu)物車的基本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
基于JS分頁控件實(shí)現(xiàn)簡(jiǎn)單美觀仿淘寶分頁按鈕效果
這篇文章主要介紹了基于JS分頁控件實(shí)現(xiàn)簡(jiǎn)單美觀仿淘寶分頁按鈕效果的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
ES6 Array常用擴(kuò)展的應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 Array常用擴(kuò)展的應(yīng)用,結(jié)合實(shí)例形式分析各種常見擴(kuò)展方法針對(duì)Array數(shù)組的轉(zhuǎn)換、遍歷、查找、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
BootStrap中關(guān)于Select下拉框選擇觸發(fā)事件及擴(kuò)展
Select下拉框的問題,想在選擇一個(gè)選項(xiàng)后,前臺(tái)顯示做出變動(dòng),并且知道選擇的是第幾個(gè)選項(xiàng)。 怎么解決這個(gè)問題呢?下面小編給大家?guī)砹薆ootStrap中關(guān)于Select下拉框選擇觸發(fā)事件及擴(kuò)展,需要的朋友參考下吧2016-11-11

