js实现扫描线填色算法使用canvas展示

算法原理

扫描线填色算法的基本思想是:用水平扫描线从上到下扫描由点线段构成的多段构成的多边形。每根扫描线与多边形各边产生一系列交点。将这些交点按照x坐标进行分类,将分类后的交点成对取出,作为两个端点,以所填的色彩画水平直线。多边形被扫描完毕后,填色也就完成。

效果

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scanline Seed Fill Algorithm</title>
<style>
    canvas {
        border: 1px solid black;
    }
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<br/>
<canvas id="canvasFill" width="400" height="400"></canvas>
<script>
    // Define the path
    const path = [
        { x: 100, y: 100 },
        { x: 300, y: 100 },
        { x: 200, y: 200 },
        {x:200,y:300}
    ];

    // Function to sort points by x-coordinate
    function sortByX(a, b) {
        return a.x - b.x;
    }

    // Function to find minimum and maximum y-coordinate in a list of points
    function findMinMaxY(points) {
        let minY = points[0].y;
        let maxY = points[0].y;
        for (let i = 1; i < points.length; i++) {
            minY = Math.min(minY, points[i].y);
            maxY = Math.max(maxY, points[i].y);
        }
        return { minY, maxY };
    }

    // Scanline seed fill algorithm
    function seedFill(canvas, path, fillColor) {
        const ctx = canvas.getContext('2d');
        const { minY, maxY } = findMinMaxY(path);
        const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        const imageDataArray = imageData.data;

        for (let y = minY; y <= maxY; y++) {
            const intersections = [];
            for (let i = 0; i < path.length; i++) {
                const p1 = path[i];
                const p2 = path[(i + 1) % path.length];

                if ((p1.y <= y && p2.y >= y) || (p2.y <= y && p1.y >= y)) {
                    const x = Math.round(p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y));
                    intersections.push(x);
                }
            }

            intersections.sort((a, b) => a - b);

            for (let i = 0; i < intersections.length; i += 2) {
                const x1 = intersections[i];
                const x2 = intersections[i + 1];
                for (let x = x1; x <= x2; x++) {
                    const index = (y * canvas.width + x) * 4;
                    imageDataArray[index] = fillColor.r;
                    imageDataArray[index + 1] = fillColor.g;
                    imageDataArray[index + 2] = fillColor.b;
                    imageDataArray[index + 3] = fillColor.a;
                }
            }
        }

        const canvasFill = document.getElementById('canvasFill');
        const ctxFill = canvasFill.getContext('2d');
        ctxFill.putImageData(imageData, 0, 0);
    }

    // Draw the path
    function drawPath(canvas, path) {
        const ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(path[0].x, path[0].y);
        for (let i = 1; i < path.length; i++) {
            ctx.lineTo(path[i].x, path[i].y);
        }
        ctx.closePath();
        ctx.stroke();
    }

    // Fill the path
    function fillPath(canvas, path) {
        const ctx = canvas.getContext('2d');
        const fillColor = { r: 255, g: 0, b: 0, a: 255 }; // Red color

        // Draw the path
        drawPath(canvas, path);

        // Seed fill the path
        seedFill(canvas, path, fillColor);
    }

    // Fill the path on canvas load
    const canvas = document.getElementById('canvas');
    fillPath(canvas, path);
</script>
</body>
</html>

相关推荐

  1. 【2024最新】C++扫描线算法介绍+实战例题

    2024-03-16 10:06:04       27 阅读
  2. js使用canvas实现图片鼠标滚轮放大缩小拖拽预览

    2024-03-16 10:06:04       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 10:06:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 10:06:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 10:06:04       82 阅读
  4. Python语言-面向对象

    2024-03-16 10:06:04       91 阅读

热门阅读

  1. 要导出 PostgreSQL 数据库中的数据使用 pg_dump 命令

    2024-03-16 10:06:04       47 阅读
  2. android 快速实现 Switch 样式

    2024-03-16 10:06:04       42 阅读
  3. 完美十进制数——去年天梯校赛

    2024-03-16 10:06:04       41 阅读
  4. python面试题

    2024-03-16 10:06:04       42 阅读
  5. 独立服务器的优势

    2024-03-16 10:06:04       44 阅读
  6. 每天学习一个Linux命令之ssh

    2024-03-16 10:06:04       40 阅读
  7. 泰勒级数、海森矩阵、雅可比矩阵

    2024-03-16 10:06:04       47 阅读
  8. Docker详解:如何创建运行Memcached的Docker容器

    2024-03-16 10:06:04       34 阅读
  9. ChatGPT有身体了?

    2024-03-16 10:06:04       39 阅读
  10. C++:[NWRRC2015] Concatenation(洛谷)P7050

    2024-03-16 10:06:04       43 阅读
  11. Linux下新增有root权限的用户

    2024-03-16 10:06:04       37 阅读