js实现填涂画板

文章目录

凑个数,存粹是好玩儿,哈哈...

1实现效果

最上方一栏:

左侧是颜色按钮,点击选中颜色,

中间是功能按钮,重置颜色、清空画板、回退、涂改液(填涂色置为白色)

右侧是显示当前所选颜色
下方填涂板是表格实现的。

2 实现代码

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画图板</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        #bt {
            display: flex;
            margin-top: 5px;
            height: 30px;
            box-sizing: border-box;
        }

        .bt1 {
            padding-left: 20px;
            flex: 1;
            box-sizing: border-box;
        }

        .bt2 {
            flex: 1;
            text-align: center;
        }

        .bt3 {
            flex: 1;
            font-size: 20px;
        }

        #content {
            margin-top: 10px;
        }

        table {
            border-collapse: collapse;
        }

        td {
            width: 25px;
            height: 25px;
            border: 0.5px solid gray;
        }

        td:hover {
            background-color: gray;
        }

        .b1 {
            margin-left: 10px;
            width: 15px;
            height: 15px;
        }

        .b2 {
            margin-right: 10px;
            width: 100px;
            line-height: 27px;
            background-color: lightgray;
            border: none;
            border-radius: 10px;
        }

        .b1:hover {
            width: 20px;
            height: 20px;
        }

        .b2:hover {
            background-color: lightblue;
        }

        #colorName {
            display: inline-block;
            color: red;
            width: 200px;
            height: 30px;
            text-align: center;
            background-color: gray;
        }
    </style>
</head>

<body>
    <div id="bt">
        <div class="bt1">
        </div>
        <div class="bt2">
            <button class="b2">重置颜色</button>
            <button class="b2">清空画板</button>
            <button class="b2">后退一步</button>
            <button class="b2">涂改液</button>
        </div>
        <div class="bt3">
            当前所选颜色为:<span id="colorName">rgb(255,0,0)</span>
        </div>
    </div>

    <div id="content">

    </div>
    <script>
        // 存放改变颜色的td索引
        let bakeTable = [];
        let div = document.getElementById('content');
        let width = 60;
        let height = 25;
        // 初始化颜色按钮
        createColorTable();
        // 打印画板
        let str = "<table>"
        for (let i = 0; i < height; i++) {
            str += "<tr>"
            for (let j = 0; j < width; j++) {
                str += "<td></td>";
            }
            str += '</tr>';
        }
        str += "</table>"
        div.innerHTML = str;
        // 给td加事件
        let tds = document.querySelectorAll("td");
        tds.forEach((td,i) => {
            td.onclick = setColor.bind(td,null,i);
        });
        // 获取功能按钮
        let buts_2 = document.querySelectorAll('.b2');
        // 获取颜色名称
        let tdColorName = document.querySelector("#colorName");
        // 初始化颜色
        let butColor = 'rgb(255,0,0)';

        // 按钮功能
        buts_2[0].onclick = createColorTable;
        buts_2[1].onclick = clearCanvas;
        buts_2[2].onclick = backStep;
        buts_2[3].onclick = function(){
            butColor = 'rgb(255, 255, 255)';
            tdColorName.innerHTML = butColor;
            tdColorName.style.color = butColor;
        };

        function getColor(but) {
            but = this;
            butColor = but.style.backgroundColor;
            tdColorName.innerHTML = butColor;
            tdColorName.style.color = butColor;
        }
        

        function setColor(td,i) {
            td = this;
            td.style.backgroundColor = butColor;
            bakeTable.push(i);
        }

        function randomColor() {
            let color = "#";
            let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
            for (let i = 0; i < 6; i++) {
                let index = parseInt(Math.random() * arr.length);
                color += arr[index];
            }
            return color;
        }

        function createColorTable() {
            let btn_str = '';
            for (let i = 0; i < 15; i++) {
                btn_str += '<button class="b1" style="background-color: ' + randomColor() + '"></button>';
            }
            document.querySelector('.bt1').innerHTML = btn_str;
            let buts = document.querySelectorAll('.b1');
            buts.forEach(but => {
                but.onclick = getColor;
            })
        }
        function clearCanvas(){
            document.querySelectorAll("td").forEach(td=>{
                td.style.backgroundColor = "";
            })
        }
        function backStep(){
            if(bakeTable.length>0){
                let i = bakeTable.pop();
                document.querySelectorAll("td")[i].style.backgroundColor = "";
            }
        }
    </script>
</body>

</html>
相关推荐
flash俊杰5 分钟前
时间线交互引擎:从像素到帧的逆向映射与磁吸网格
前端
光影少年6 分钟前
setImmediate 和 setTimeout(0) 的区别
android·前端·react.js·ios·前端框架
flash俊杰8 分钟前
LLM 结构化输出的契约化工程:JSON Mode、Schema 约束与重试降级
前端·ai编程
༄久梦༒长醉༻9 分钟前
AI面试助手:本地运行,一键备战
前端·ai编程
Web4Browser12 分钟前
浏览器指纹逆向到底在分析什么:从 JavaScript 采集、Canvas 与 WebGL 到环境一致性校验
java·服务器·前端
2601_9583529013 分钟前
不改PCB也能调拾音方向?真相在这。
前端·嵌入式硬件·回声消除·语音模块·降噪处理
zhanghaha131427 分钟前
HTML系列教程:17_HTML 框架(frameset、frame、iframe)零基础详解
前端·html
IT_陈寒38 分钟前
React的useEffect依赖项居然骗了我三年
前端·人工智能·后端
一个金牛座的前端44 分钟前
AI 写前端,优化的是演示,不是交付
前端·ai·cursor
球球不吃虾1 小时前
Nuxt在使用过程中的一些小总结(二)
前端·vue·nuxt