前端:改变鼠标点击物体的颜色

需求:

需要改变图片中某一物体的颜色,该物体是纯色;

鼠标点击哪个物体,哪个物体的颜色变为指定的颜色,利用canvas实现。

演示案例

代码Demo

html 复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Color Replacement</title>
        <style>
            canvas {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        <input type="color" id="newColor" name="newColor" />
        <canvas id="myCanvas" width="375" height="397"></canvas>
        <script>
            const canvas = document.getElementById('myCanvas');
            const ctx = canvas.getContext('2d', { willReadFrequently: true });

            const img = new Image();
            img.src =
                'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXcAAAGNCAIAAABlq6bWAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AAA0SSURBVHic7d2/lStFFsDhnj3PxcFYb82NgARwIQXIgxjIA1IAlwSIABMPYx0C0BoCnUGjlvrf7bq36vsOxjvwZqbUTP3mdkvqebtcLhNAmH+1XgDQOZUBYqkMEEtlgFgqA8RSGSCWygCxVAaIpTJALJUBYqkMEEtlgFgqA8RSGSCWygCxVAaIpTJALJUBYqkMEEtlgFgqA8RSGSCWygCxVAaIpTJALJUBYqkMEEtlgFgqA8RSGSCWygCxVAaI9an1Atr7/u3fbRfw3eWPtguAUG+Xy6X1Gppp3pc7ckOXBq1Mtr7cCA39GfG6TNrETLnXBtsMV5n82zj/CmGV4SpTgtDQk7EqY/fC+caqTCGCSDcGqox9C00MVJlyZJE+qAwQa5TKmAuglVEqA7SiMkAslQFiqQwQS2WAWCoDxFIZIJbKALFUBoilMkAslQFiqQwQS2WAWCoDxFIZIJbfYEt7//vPf69/+Pz339quhAhmGRq7Jebuz3RDZYBYKkNLH4cX40x/VAaIpTI0Mze2GGc6ozJALJWhjecDi3GmJyoDxFIZkjLOdENlaEBBhqIyQCyV4WzLBxkjTx9UBoilMpxq7XhinOmAygCxVIbzbBtMjDPVq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nTHl53V3R+l+B3b/AN9TmcPoy7E63ocdP7SH3F/mGBLDEqP15coscwCJCVJuTz5Z8Lc/fVPu4RzF1d/Hll/9lZhoVa4EDxuRl8wyu0jMCeze6lRmO4k5Tf7Q5F9hQyqzkcScKflJk8Q8pzJbSMyZjkqMFrTi6u9jc1d/9eVk+xNzF5fDxyLxesnrZVaQmAze7+onyZjb/N/+9M2BoZGYJcwyj32cZSTmfB9z8HFXL/k7Sz7zBhKzkOsyi0hMBg939batvj8QErOcM6bHfvn5i9s4IzFdumZiw1CjL2upzCxxGcHa1kjMBipDGT98/eOS6zIb3D7t3GcTlz1c/SWpJRv+4d9RhGxUhow2TygSk5DnmOiHxOSkMqSzbZCRmLRUhgJeFkRiMvMcE7msfS2vvuSnMmSx6kRJXApRGVJ4kpjkN5fhJddlaG9tRwwytagMEEtlKMYgU47rMpShL0V5hwEpPL80oy+lOWMihee/lfHMlXA4lSGLA2+FRyrOmIBYZhkglsoAsVQGiKUyQCyVAWKpDBBLZYBYKgPEUhkglsoAsVQGiKUyQCyVAWKpDBBLZYBYKgPEUhkglsoAsVQGiKUyQCyVAWKpDBDr/zbvDo3bd4LHAAAAAElFTkSuQmCC';
            img.onload = function () {
                ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
            };

            // 替换颜色
            function replaceColor(clickX, clickY, newColor) {
                const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                const data = imageData.data;
                // debugger;
                // 点击的颜色值
                const index = parseInt((clickY * canvas.width + clickX) * 4);
                const oldColor = [data[index], data[index + 1], data[index + 2]];

                // 将颜色转成rgb
                const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(newColor.slice(1));
                const newRGB = result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : null;

                if (!newRGB) return;
                floodFill(imageData, clickX, clickY, oldColor, newRGB);
                ctx.putImageData(imageData, 0, 0);
            }

            // 填充颜色
            function floodFill(imageData, x, y, oldColor, newColor) {
                let stack = [[x, y]];
                const data = imageData.data;
                let includesPoints = [];

                while (stack.length) {
                    let point = stack.pop();
                    let index = parseInt((point[1] * canvas.width + point[0]) * 4);
                    if (data[index] === oldColor[0] && data[index + 1] === oldColor[1] && data[index + 2] === oldColor[2]) {
                        data[index] = newColor[0];
                        data[index + 1] = newColor[1];
                        data[index + 2] = newColor[2];
                        data[index + 3] = 255;
                        [
                            [0, -1],
                            [1, 0],
                            [0, 1],
                            [-1, 0],
                        ].forEach(([dx, dy]) => {
                            let newX = point[0] + dx;
                            let newY = point[1] + dy;
                            const pointToString = newX + ',' + newY;
                            if (newX >= 0 && newX < canvas.width && newY >= 0 && newY < canvas.height) {
                                stack.push([newX, newY]);
                            }
                        });
                    }
                }
            }

            canvas.addEventListener('click', function (event) {
                const rect = canvas.getBoundingClientRect();
                const clickX = event.clientX - rect.left;
                const clickY = event.clientY - rect.top;
                // debugger;
                let newColor = document.getElementById('newColor').value;
                replaceColor(Math.round(clickX), Math.round(clickY), newColor);
            });
        </script>
    </body>
</html>

后续

确定点击点四周相同颜色值的算法有待优化

相关推荐
蟾宫曲3 小时前
在 Vue3 项目中实现计时器组件的使用(Vite+Vue3+Node+npm+Element-plus,附测试代码)
前端·npm·vue3·vite·element-plus·计时器
秋雨凉人心3 小时前
简单发布一个npm包
前端·javascript·webpack·npm·node.js
liuxin334455663 小时前
学籍管理系统:实现教育管理现代化
java·开发语言·前端·数据库·安全
qq13267029403 小时前
运行Zr.Admin项目(前端)
前端·vue2·zradmin前端·zradmin vue·运行zradmin·vue2版本zradmin
魏时烟5 小时前
css文字折行以及双端对齐实现方式
前端·css
2401_882726486 小时前
低代码配置式组态软件-BY组态
前端·物联网·低代码·前端框架·编辑器·web
web130933203986 小时前
ctfshow-web入门-文件包含(web82-web86)条件竞争实现session会话文件包含
前端·github
胡西风_foxww6 小时前
【ES6复习笔记】迭代器(10)
前端·笔记·迭代器·es6·iterator
前端没钱6 小时前
探索 ES6 基础:开启 JavaScript 新篇章
前端·javascript·es6
m0_748255267 小时前
vue3导入excel并解析excel数据渲染到表格中,纯前端实现。
前端·excel