diff --git a/LICENCE b/LICENSE similarity index 100% rename from LICENCE rename to LICENSE diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..4307ae4 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,31 @@ +# svg-gen Examples + +Some code examples, to see how this library works. + +## TypeScript & parcel-bundler + +All examples are written in TypeScript. +In order to use the svg-gen library directly (realive path: ..) instead of npm installing and to avoid much configuration, we use parcel. +Parcel is bundler like Webpack, but with less configuration. + +## Current issue: Folder structure + +I tried to have all examples in subfolders (html + css files). +And to have one index.html in examples root. +But parcel resolves the relative paths wrong, when html files are in subfolders [Parcel issue 2786](https://github.com/parcel-bundler/parcel/issues/2786) + +## Build and Test examples + +```sh +npm run build +``` + +the built files will be in in examples/dist/ + +## Develop + +```sh +npm start +``` + +The browser will open autommatically the local webserver and refreshes when changes are made. \ No newline at end of file diff --git a/examples/package-lock.json b/examples/package-lock.json index 59d892f..6fab447 100644 --- a/examples/package-lock.json +++ b/examples/package-lock.json @@ -1354,6 +1354,12 @@ "@types/d3-selection": "*" } }, + "@types/delaunator": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@types/delaunator/-/delaunator-2.0.0.tgz", + "integrity": "sha512-5MwdeoRF5Q3zfGljBt4uOVt2Xppf+kAoZk2fRQV4TOUa7HsZ9R6J4Bu2pfNx9RoIJrbtQwMGHPSdGiQsmJ9jEQ==", + "dev": true + }, "@types/geojson": { "version": "7946.0.7", "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.7.tgz", @@ -3067,6 +3073,11 @@ } } }, + "delaunator": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/delaunator/-/delaunator-3.0.2.tgz", + "integrity": "sha512-GNSex8jhF1mcqtNAMYvdZ6Ng7YieYNlbOq2xshyZhLc98P8y5O7Vm6buw4A60wGOd9qvK9RcIMm5qoe4PncAPw==" + }, "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", diff --git a/examples/package.json b/examples/package.json index 012b143..e63af31 100644 --- a/examples/package.json +++ b/examples/package.json @@ -4,18 +4,18 @@ "description": "", "main": "index.html", "scripts": { - "start": "rm ./dist/ -rf && parcel serve ./src/**/*", - "build": "rm ./dist/ -rf && parcel build ./src/**/* --public-url ../ && parcel build ./src/index.html --public-url ." + "start": "rm ./dist/ -rf && parcel serve ./src/**/* --public-url ./", + "build": "rm ./dist/ -rf && parcel build ./src/index.html --public-url ./ --no-minify" }, "author": "Tobias Kraus (www.tobias-kraus.com)", "license": "ISC", "devDependencies": { "@types/d3": "^5.7.2", - "@types/d3-voronoi": "^1.1.9", + "@types/delaunator": "^2.0.0", "parcel-bundler": "^1.12.3" }, "dependencies": { "d3": "^5.9.2", - "d3-voronoi": "^1.1.4" + "delaunator": "^3.0.2" } } diff --git a/examples/src/index.html b/examples/src/index.html index b84092d..1184c10 100644 --- a/examples/src/index.html +++ b/examples/src/index.html @@ -21,8 +21,8 @@

svg-gen Examples

diff --git a/examples/src/points-grid/index.html b/examples/src/points-grid.html similarity index 87% rename from examples/src/points-grid/index.html rename to examples/src/points-grid.html index bda48c4..4569e16 100644 --- a/examples/src/points-grid/index.html +++ b/examples/src/points-grid.html @@ -13,12 +13,12 @@ } - +

- svg-gen Examples  + svg-gen Examples  / PointsGrid

- svg-gen Examples / Triangles Web with d3 + svg-gen Examples / Triangles Web with d3

- +
+ \ No newline at end of file diff --git a/examples/src/triangles-web-with-d3/triangles-web-with-d3.ts b/examples/src/triangles-web-with-d3.ts similarity index 53% rename from examples/src/triangles-web-with-d3/triangles-web-with-d3.ts rename to examples/src/triangles-web-with-d3.ts index a9d5047..d13a306 100644 --- a/examples/src/triangles-web-with-d3/triangles-web-with-d3.ts +++ b/examples/src/triangles-web-with-d3.ts @@ -1,9 +1,8 @@ import * as d3 from 'd3'; -import * as voronoi from 'd3-voronoi'; - -import { PointsGrid } from '../../../src'; -import { Point } from '../../../src/model/types'; +import Delaunator from 'delaunator'; +import { PointsGrid } from '../../src'; +import { Point } from '../../src/model/types'; interface TrianglesWebOptions { svgSelector: string; @@ -12,13 +11,13 @@ interface TrianglesWebOptions { class TrianglesWeb { - constructor ( + constructor( private options: TrianglesWebOptions, ) { let svg = document.querySelector(this.options.svgSelector), width = svg!.getBoundingClientRect().width, height = svg!.getBoundingClientRect().height, - {rows, cols} = this.getWebDimension(this.options.rowsAndCols, width, height); + { rows, cols } = this.getWebDimension(this.options.rowsAndCols, width, height); let pointsGrid = new PointsGrid({ maxX: Math.round(width), @@ -30,8 +29,8 @@ class TrianglesWeb { pointsOnLeftEdge: true, pointsOnRightEdge: true, pointsOnTopEdge: true, - randomDistortionX: (width / (cols -1)) * 0.5, - randomDistortionY: (height / (rows -1)) * 0.5, + randomDistortionX: (width / (cols - 1)) * 0.3, + randomDistortionY: (height / (rows - 1)) * 0.3, }); // flatten array @@ -52,20 +51,34 @@ class TrianglesWeb { .attr('cx', d => d[0]) .attr('cy', d => d[1]); - let graph = voronoi.voronoi(); - let triangles = graph.triangles(points); + // let graph = voronoi.voronoi(); + // let triangles = graph.triangles(points); + // console.log('triangles', triangles); + + let graph = Delaunator.from(points); + console.log('graph', graph); + + // console.log(graph); + + // retrieve triangles from graph.triangles (Uint32Array to [number, number, number][]) + let triangles = this.getTriangles(graph, points); console.log('triangles', triangles); + // let triangles = Array.prototype.slice.call(graph.triangles); + + // draw triangles d3.select(this.options.svgSelector) .selectAll('polygon') + // .data(triangles) .data(triangles) .enter() .append('polygon') - .attr('points', d => `${d[0][0]},${d[0][1]} ${d[1][0]},${d[1][1]} ${d[2][0]},${d[2][1]}`); + // .attr('points', d => `${d[0][0]},${d[0][1]} ${d[1][0]},${d[1][1]} ${d[2][0]},${d[2][1]}`); + .attr('points', (d, i) => `${d[0][0]},${d[0][1]} ${d[1][0]},${d[1][1]} ${d[2][0]},${d[2][1]}`); } - private getWebDimension (rowsAndCols: number, svgWidth: number, svgHeight: number) { + private getWebDimension(rowsAndCols: number, svgWidth: number, svgHeight: number) { // rows + cols = rowsAndCols // svgWidth / svgHeight = cols / rows let ratioWidth = svgWidth / (svgWidth + svgHeight), @@ -76,6 +89,18 @@ class TrianglesWeb { rows: Math.floor(rows), } } + + private getTriangles(graph: Delaunator>, points: Point[]): Point[][] { + let triangles: Point[][] = []; + for (let i = 0; i < graph.triangles.length; i += 3) { + triangles.push([ + points[graph.triangles[i]], + points[graph.triangles[i + 1]], + points[graph.triangles[i + 2]], + ]); + } + return triangles; + } } new TrianglesWeb({ diff --git a/examples/tsconfig.json b/examples/tsconfig.json index e95a4dc..4a75ab2 100644 --- a/examples/tsconfig.json +++ b/examples/tsconfig.json @@ -1,7 +1,8 @@ { "compilerOptions": { - "target": "es5", + "target": "es2015", "module": "commonjs", - "baseUrl": "./" + "baseUrl": "./", + "esModuleInterop": true } } \ No newline at end of file