Code Monkey home page Code Monkey logo

ishape-rust / ishape-js Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 2.37 MB

A poly-bool library supports main operations such as union, intersection, difference, xor, and self-intersection. This algorithm is based on Vatti clipping ideas but is an original implementation.

Home Page: https://ishape-rust.github.io/iShape-js/

License: MIT License

Rust 9.97% JavaScript 89.81% Shell 0.22%
poly-bool polygon polygon-clipping vatti-clipper boolean-operations clipping difference intersection self-intersection union

ishape-js's Introduction

iShape-js

2D geometry library for poly-bool operations such as union, intersection, difference and xor.

Try out iShape with an interactive demo. The demo covers operations like union, intersection, difference and exclusion

Features

  • Operations: union, intersection, difference, and exclusion.
  • Polygons: with holes, self-intersections, and multiple paths.
  • Simplification: removes degenerate vertices and merges collinear edges.
  • Fill Rules: even-odd and non-zero.

Getting Started

Direct include

Download Library Files:

  • ishape_wasm.js
  • ishape_bg_wasm.wasm

You can find it at: pkg

Place Files:

Place these files in a directory that your HTML file can access; in this example, the directory is named ./ishape

NPM

Installation

You can install the iShape library from NPM:

npm install ishape_wasm

The NPM package is available here

Import and Usage

After installing the NPM package, you can import it in your JavaScript or TypeScript file as follows:

import init, { Overlay, OverlayGraph, OverlayRule, ShapeType, FillRule } from './ishape/ishape_wasm.js';

// Your code here

Example

Here is a simple HTML example that demonstrates how to use the iShape library for union operation. Full example is available here

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iShape</title>
    <style>
        #result {
            background-color: #f5f5f5;
            border: 1px solid #ccc;
            padding: 10px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            font-family: monospace;
            margin-bottom: 10px;
        }
    </style>
    <script type="module">
        import init, { Overlay, OverlayGraph, OverlayRule, ShapeType, FillRule} from './ishape/ishape_wasm.js';

        init();

        document.getElementById('union').addEventListener('click', () => {
            const subjInput = document.getElementById('subjInput').value;
            const clipInput = document.getElementById('clipInput').value;

            const subj = JSON.parse(subjInput);
            const clip = JSON.parse(clipInput);

            const overlay = new Overlay();
            overlay.add_paths(subj, ShapeType.Subject);
            overlay.add_paths(clip, ShapeType.Clip);

            // build segments geometry
            const graph = overlay.build_graph(FillRule.EvenOdd);

            // apply union operation
            const union = graph.extract_shapes(OverlayRule.Union);

            // add more operations if required
            // ...

            const resultText = JSON.stringify(union, null, 2);
            document.getElementById('result').innerText = `Result:\n${resultText}`;
        });
    </script>
</head>
<body>
    <textarea id="subjInput" placeholder='Enter "subj" polygon here...'>[[[200, 300], [200, 100], [400, 100], [400, 300]]]</textarea>
    <textarea id="clipInput" placeholder='Enter "clip" polygon here...'>[[[300, 400], [300, 200], [500, 200], [500, 400]]]</textarea>
    <button id="union">Union</button>
    <pre id="result"></pre>
</body>
</html>

Explanation:

Import classes and initialize the WebAssembly module using init(). Use the imported classes to perform geometric operations.

Overlay Rules

Union, A or B

Union

Intersection, A and B

Intersection

Difference, B - A

Difference

Exclusion, A xor B

Exclusion

ishape-js's People

Contributors

nailxsharipov avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.