js SVG应用---验证码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        svg {
            width: 600px;
            height: 200px;
            border: 1px dashed #ccc;
            font-size: 100px;
        }
    </style>
</head>

<body>
    <svg><rect x="60" y="10" rx="0" ry="0" width="500" height="200" stroke="black" fill="transparent" stroke-width="1" />
    </svg>
    <input type="button" value="验证码">
    <script src="../util/tools.js"></script>
    <script>
  function Validate() {
            let date = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
            let svg = document.querySelector("svg");
            let Length = date.length;
            let number = 30;
            let text = "";
            for (let i = 0; i < number; i++) {
                text += `<line x1="${Tool.random(100)}" y1="${Tool.random(300)}" x2="${Tool.random(600)}" y2="${Tool.random(150)}" stroke=" rgb(${[Tool.random(0, 255)]},${Tool.random(0, 255)},${Tool.random(0, 255)})" fill="transparent" stroke-width="${Tool.random(1, 3)}" />`
            }
            for (let j = 0; j < 4; j++) {
                text += ` <text x="${100 + j * 100}" y="${100}" style="font-size:100;fill: rgb(${[Tool.random(0, 255)]},${Tool.random(0, 255)},${Tool.random(0, 255)})" rotate="${Tool.random(-50, 50)}">${date[Tool.random(Length - 1)]}
               
            </text>`
            }
            svg.innerHTML = text;

        }
        let input = document.querySelector("input");
        input.addEventListener("click", function () {
            Validate()
        })
        Validate()
    </script>
</body>

</html>

util工具包

let Tool = (function () {
    function random(start, end) {
        if (arguments.length === 0) {
            return 0
        } else if (arguments.length === 1) {
            end = start;
            start = 0;
        } else {
            if (end < start) {
                [end, start] = [start, end]
            }
        }
        return parseInt(Math.random() * (end - start + 1)) + start;
    }
    function color(opcity = 1) {
        return `rgba(${random(ZERO, 255)},${random(ZERO, 255)},${random(ZERO, 255)},${opcity})`
    }
    function curry(func, ...args) { //函数柯里化
        return function (...inArgs) {
            const allArgs = args.concat(inArgs);
            if (allArgs.length >= func.length) {
                return func(...allArgs);
            }
            else {
                return curry(func, ...allArgs);
            }
        }
    }
    function debounce(func, wait) {  //防抖函数
        let timerId = null;
        return function (...rag) {
            if (timerId) {
                clearTimeout(timerId);
            }
            timerId = setTimeout(() => {
                func(rag);
            }, wait);
        }
    }
    return { random, color,curry, debounce }
})()
    
 
    
 
   
 
```javascript
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值