<template>
<div class="s-canvas">
<canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
</div>
</template>
<script>
export default {
name: 'SIdentify',
props: {
//默認(rèn)注冊(cè)碼
identifyCode: {
type: String,
default: '1234'
},
// 字體最小值
fontSizeMin: {
type: Number,
default: 25
},
// 字體最大值
fontSizeMax: {
type: Number,
default: 35
},
// 驗(yàn)證碼圖片背景色最小值
backgroundColorMin: {
type: Number,
default: 200
},
// 驗(yàn)證碼圖片背景色最大值
backgroundColorMax: {
type: Number,
default: 220
},
// 背景干擾點(diǎn)最小值
dotColorMin: {
type: Number,
default: 60
},
// 背景干擾點(diǎn)最大值
dotColorMax: {
type: Number,
default: 120
},
//容器寬度
contentWidth: {
type: Number,
default: 116
},
//容器高度
contentHeight: {
type: Number,
default: 38
}
},
methods: {
// 生成一個(gè)隨機(jī)數(shù)
randomNum(min, max) {
return Math.floor(Math.random() * (max - min) + min)
},
// 生成一個(gè)隨機(jī)的顏色
randomColor(min, max) {
let r = this.randomNum(min, max)
let g = this.randomNum(min, max)
let b = this.randomNum(min, max)
return 'rgb(' + r + ',' + g + ',' + b + ')'
},
drawPic() {
let canvas = document.getElementById('s-canvas')
let ctx = canvas.getContext('2d')
ctx.textBaseline = 'bottom'
// 繪制背景
ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
// 繪制文字
for (let i = 0; i < this.identifyCode.length; i++) {
this.drawText(ctx, this.identifyCode[i], i)
}
this.drawLine(ctx)
this.drawDot(ctx)
},
drawText(ctx, txt, i) {
ctx.fillStyle = this.randomColor(50, 160) //隨機(jī)生成字體顏色
ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei' //隨機(jī)生成字體大小
let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
var deg = this.randomNum(-30, 30)
// 修改坐標(biāo)原點(diǎn)和旋轉(zhuǎn)角度
ctx.translate(x, y)
ctx.rotate(deg * Math.PI / 180)
ctx.fillText(txt, 0, 0)
// 恢復(fù)坐標(biāo)原點(diǎn)和旋轉(zhuǎn)角度
ctx.rotate(-deg * Math.PI / 180)
ctx.translate(-x, -y)
},
drawLine(ctx) {
// 繪制干擾線
for (let i = 0; i < 4; i++) {
ctx.strokeStyle = this.randomColor(100, 200)
ctx.beginPath()
ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
ctx.stroke()
}
},
drawDot(ctx) {
// 繪制干擾點(diǎn)
for (let i = 0; i < 30; i++) {
ctx.fillStyle = this.randomColor(0, 255)
ctx.beginPath()
ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
ctx.fill()
}
}
},
watch: {
identifyCode() {
this.drawPic()
}
},
mounted() {
this.drawPic()
}
}
</script>
vue生成數(shù)字驗(yàn)證碼組件
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诈豌,“玉大人仆救,你說我怎么就攤上這事〗糜妫” “怎么了彤蔽?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)庙洼。 經(jīng)常有香客問我顿痪,道長(zhǎng),這世上最難降的妖魔是什么油够? 我笑而不...
- 正文 為了忘掉前任蚁袭,我火速辦了婚禮,結(jié)果婚禮上石咬,老公的妹妹穿的比我還像新娘揩悄。我一直安慰自己,他們只是感情好碌补,可當(dāng)我...
- 文/花漫 我一把揭開白布虏束。 她就那樣靜靜地躺著棉饶,像睡著了一般厦章。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上照藻,一...
- 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼栏尚!你這毒婦竟也來了起愈?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎抬虽,沒想到半個(gè)月后官觅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡阐污,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年休涤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片笛辟。...
- 正文 年R本政府宣布弯菊,位于F島的核電站纵势,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏管钳。R本人自食惡果不足惜钦铁,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望才漆。 院中可真熱鬧牛曹,春花似錦、人聲如沸醇滥。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽鸳玩。三九已至阅虫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間不跟,已是汗流浹背颓帝。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像虐译,于是被迫代替她去往敵國(guó)和親瘪板。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 背景:我們是一家toB的人工智能企業(yè)漆诽,交付給用戶的dashboard的注冊(cè)入口并不對(duì)外開放侮攀,而且dashboard...
- 樣式自調(diào)史侣,最終效果如圖: 實(shí)現(xiàn)效果:點(diǎn)擊右邊input框會(huì)自動(dòng)切換,如果輸入的值與字不同魏身,則清空換一串隨機(jī)數(shù) HT...
- 說明 前端開發(fā)在做登錄功能時(shí)惊橱,倒計(jì)時(shí)獲取驗(yàn)證碼的情況很是普遍 實(shí)現(xiàn)效果 父組件代碼 [ Login.vue ] C...
- 生活中有很多的不容易税朴,感恩有這么多的難得。昨天下午家制,陪媽媽去商場(chǎng)買衣服正林,因?yàn)檫@周弟弟要訂婚了,爸爸媽媽都要去參加颤殴,...