How to use earcut - 10 common examples

To help you get started, we’ve selected a few earcut examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github UDST / vizicities / src / util / GeoJSON.js View on Github external
var _triangulate = function(contour, holes, dim) {
    // console.time('earcut');

    var faces = earcut(contour, holes, dim);
    var result = [];

    for (i = 0, il = faces.length; i < il; i += 3) {
      result.push(faces.slice(i, i + 3));
    }

    // console.timeEnd('earcut');

    return result;
  };
github UDST / vizicities / src / layer / geometry / PolygonLayer.js View on Github external
_triangulate(contour, holes, dim) {
    // console.time('earcut');

    var faces = earcut(contour, holes, dim);
    var result = [];

    for (i = 0, il = faces.length; i < il; i += 3) {
      result.push(faces.slice(i, i + 3));
    }

    // console.timeEnd('earcut');

    return result;
  }
github maptalks / maptalks.biglayer / src / painter / ExtrudePainter.js View on Github external
}
            if (this.options['project']) {
                c = this.map.coordinateToPoint(new maptalks.Coordinate(data.vertices[i], data.vertices[i + 1]), targetZ);
                bottom.push(c.x, c.y, 0);
                top.push(c.x, c.y, height);
            } else {
                bottom.push(data.vertices[i], data.vertices[i + 1], 0);
                top.push(data.vertices[i], data.vertices[i + 1], height);
            }
        }
        data.vertices = bottom;
        let triangles = earcut(data.vertices, data.holes, dimension);
        if (triangles.length <= 2) {
            return;
        }
        const deviation = earcut.deviation(data.vertices, data.holes, dimension, triangles);
        if (Math.round(deviation * 1E3) / 1E3 !== 0) {
            if (console) {
                console.warn('Failed triangluation.');
            }
            return;
        }

        const count = bottom.length / dimension;

        const preCount = this.vertexArray.length / dimension;
        if (preCount > 0) {
            triangles = triangles.map(e => e + preCount);
        }
        // push bottom vertice
        maptalks.Util.pushIn(this.vertexArray, bottom);
        // push bottom elements
github maptalks / maptalks.biglayer / src / painter / PolygonPainter.js View on Github external
const data = earcut.flatten(vertice);

        if (this.options['project']) {
            const v = [];
            let c;
            for (let i = 0, l = data.vertices.length; i < l; i += 2) {
                c = this.map.coordinateToPoint(new maptalks.Coordinate(data.vertices[i], data.vertices[i + 1]), targetZ);
                v.push(c.x, c.y);
            }
            data.vertices = v;
        }
        let triangles = earcut(data.vertices, data.holes, 2);
        if (triangles.length <= 2) {
            return this;
        }
        const deviation = earcut.deviation(data.vertices, data.holes, 2, triangles);
        if (Math.round(deviation * 1E3) / 1E3 !== 0) {
            if (console) {
                console.warn('Failed triangluation.');
            }
            return this;
        }
        const count = this.vertexArray.length / 2;
        if (count > 0) {
            triangles = triangles.map(e => e + count);
        }
        maptalks.Util.pushIn(this.vertexArray, data.vertices);
        maptalks.Util.pushIn(this.elementArray, triangles);

        // 添加样式数据
        this._addTexCoords(data.vertices.length / 2, style);
        return this;
github pixelpicosean / voltar / src / engine / scene / graphics / renderer / utils / build_rounded_rectangle.js View on Github external
// TODO - fix this properly, this is not very elegant.. but it works for now.

    if (graphics_data.fill) {
        const color = hex2rgb(graphics_data.fill_color);
        const alpha = graphics_data.fill_alpha;

        const r = color[0] * alpha;
        const g = color[1] * alpha;
        const b = color[2] * alpha;

        const verts = webgl_data.points;
        const indices = webgl_data.indices;

        const vec_pos = verts.length / 6;

        const triangles = earcut(rect_points, null, 2);

        for (let i = 0, j = triangles.length; i < j; i += 3) {
            indices.push(triangles[i] + vec_pos);
            indices.push(triangles[i] + vec_pos);
            indices.push(triangles[i + 1] + vec_pos);
            indices.push(triangles[i + 2] + vec_pos);
            indices.push(triangles[i + 2] + vec_pos);
        }

        for (let i = 0, j = rect_points.length; i < j; i++) {
            verts.push(rect_points[i], rect_points[++i], r, g, b, alpha);
        }
    }

    if (graphics_data.line_width) {
        const temp_points = graphics_data.points;
github uber / deck.gl / src / core-layers / solid-polygon-layer / polygon-tesselator-extruded.js View on Github external
function calculateSurfaceIndices(vertices, offset) {
  const stride = Polygon.getVertexCount(vertices);

  let holes = null;
  const holeCount = vertices.length - 1;

  if (holeCount) {
    holes = [];
    let vertexIndex = 0;
    for (let i = 0; i < holeCount; i++) {
      vertexIndex += vertices[i].length;
      holes[i] = vertexIndex;
    }
  }

  const indices = earcut(flatten(vertices, 3), holes, 3).map(index => index + offset);

  vertices.forEach(polygon => {
    const numVertices = polygon.length;

    // polygon sides
    for (let i = 0; i < numVertices - 1; i++) {
      drawSurfaceRectangle(indices, offset + i, stride);
    }

    offset += numVertices;
  });

  return indices;
}
github uber / deck.gl / src / experimental-layers / src / solid-polygon-layer / polygon.js View on Github external
export function getSurfaceIndices(complexPolygon) {
  // Prepare an array of hole indices as expected by earcut
  const holeIndices = getHoleIndices(complexPolygon);
  // Flatten the polygon as expected by earcut
  const verts = flattenVertices(complexPolygon, {dimensions: 2, result: []});
  // Let earcut triangulate the polygon
  return earcut(verts, holeIndices, 2);
}
github vasturiano / three-globe / src / layers / polygons / ConicPolygonGeometry.js View on Github external
function generateCap(top = true) {
    // !! using the 3d coords generates shapes with the wrong winding, connecting the outsides of the contour
    // so we derive the indexes from the lat,lng coordinates directly
    // let capIndices = earcut(top ? topVerts : bottomVerts, holes, 3);
    let capIndices = earcut(earcut.flatten(polygonGeoJson).vertices, holes, 2);

    !top && (capIndices = capIndices.map(v => v + numPoints)); // translate bottom indices

    return capIndices;
  }
}
github maptalks / maptalks.biglayer / src / painter / ExtrudePainter.js View on Github external
if (i === l - 1) {
                if (this._equalCoord(data.vertices[i], data.vertices[0])) {
                    continue;
                }
            }
            if (this.options['project']) {
                c = this.map.coordinateToPoint(new maptalks.Coordinate(data.vertices[i], data.vertices[i + 1]), targetZ);
                bottom.push(c.x, c.y, 0);
                top.push(c.x, c.y, height);
            } else {
                bottom.push(data.vertices[i], data.vertices[i + 1], 0);
                top.push(data.vertices[i], data.vertices[i + 1], height);
            }
        }
        data.vertices = bottom;
        let triangles = earcut(data.vertices, data.holes, dimension);
        if (triangles.length <= 2) {
            return;
        }
        const deviation = earcut.deviation(data.vertices, data.holes, dimension, triangles);
        if (Math.round(deviation * 1E3) / 1E3 !== 0) {
            if (console) {
                console.warn('Failed triangluation.');
            }
            return;
        }

        const count = bottom.length / dimension;

        const preCount = this.vertexArray.length / dimension;
        if (preCount > 0) {
            triangles = triangles.map(e => e + preCount);
github Turfjs / turf / packages / turf-tesselate / index.js View on Github external
function processPolygon(coordinates) {
    var data = flattenCoords(coordinates);
    var dim = 2;
    var result = earcut(data.vertices, data.holes, dim);

    var features = [];
    var vertices = [];

    result.forEach(function (vert, i) {
        var index = result[i];
        vertices.push([data.vertices[index * dim], data.vertices[index * dim + 1]]);
    });

    for (var i = 0; i < vertices.length; i += 3) {
        var coords = vertices.slice(i, i + 3);
        coords.push(vertices[i]);
        features.push(polygon([coords]));
    }

    return features;

earcut

The fastest and smallest JavaScript polygon triangulation library for your WebGL apps

ISC
Latest version published 5 months ago

Package Health Score

86 / 100
Full package analysis

Popular earcut functions

Similar packages