{"id":16184042,"url":"https://github.com/fralonra/ds-heightmap","last_synced_at":"2025-03-19T02:31:01.554Z","repository":{"id":57216638,"uuid":"127545452","full_name":"fralonra/ds-heightmap","owner":"fralonra","description":"Use diamond-square algorithm to generate heightmaps, with JavaScript/Rust/WebAssembly.","archived":false,"fork":false,"pushed_at":"2023-06-07T06:50:55.000Z","size":203,"stargazers_count":12,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-11T03:52:17.338Z","etag":null,"topics":["diamond-square-algorithm","ds-heightmap","heightmap","map","procedural-generation"],"latest_commit_sha":null,"homepage":"https://fralonra.github.io/ds-heightmap/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fralonra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-31T15:34:30.000Z","updated_at":"2024-10-29T16:28:25.000Z","dependencies_parsed_at":"2024-10-27T19:38:06.769Z","dependency_job_id":null,"html_url":"https://github.com/fralonra/ds-heightmap","commit_stats":{"total_commits":60,"total_committers":3,"mean_commits":20.0,"dds":"0.21666666666666667","last_synced_commit":"58a4bfdd4b268b6a900717a518b66aed63673f76"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fralonra%2Fds-heightmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fralonra%2Fds-heightmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fralonra%2Fds-heightmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fralonra%2Fds-heightmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fralonra","download_url":"https://codeload.github.com/fralonra/ds-heightmap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243960434,"owners_count":20375101,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["diamond-square-algorithm","ds-heightmap","heightmap","map","procedural-generation"],"created_at":"2024-10-10T07:08:48.410Z","updated_at":"2025-03-19T02:31:01.251Z","avatar_url":"https://github.com/fralonra.png","language":"Rust","readme":"# ds-heightmap\n\n[![Build Status](https://travis-ci.com/fralonra/ds-heightmap.svg?branch=master)](https://travis-ci.com/fralonra/ds-heightmap)\n[![npm version](https://img.shields.io/npm/v/ds-heightmap.svg)](https://www.npmjs.com/package/ds-heightmap)\n\nUsing [diamond-square algorithm](https://en.wikipedia.org/wiki/Diamond-square_algorithm) to generate heightmaps which stored in a 2D-array.\n\n## Examples\n\n- [basic](https://fralonra.github.io/ds-heightmap/): Render heightmaps on 2d canvas.\n- [heightmap-visualizer](https://github.com/fralonra/heightmap-visualizer): Heightmap 2d/3d visualization.\n- [zatlas](https://github.com/fralonra/zatlas): Heightmap generator. Use a legacy version of [ds-heightmap](https://github.com/fralonra/ds-heightmap/tree/20ad02e589d6cc40292966b9ecdb6d771d5ba39e).\n\n## Install\n\n```bash\nnpm install --save ds-heightmap\n```\n\n## Usage\n\n```javascript\nimport { ds } from 'ds-heightmap';\nconst data = ds({\n  width: 129,           // the width of the map, must be larger than 1.\n  height: 129,          // the height of the map, must be larger than 1.\n  depth: 2000,          // [optional] the value of each pixel will be within 0~depth, default: 2000.\n  rough: 1,             // [optional] effect the terrain variability (roughness), default: 1.\n  randomizer(base, range) {\n    // [optional] customize the logic of random height generation.\n    // receive two number arguments:\n    // first is the average of the four(or three) vertices of the square/diamomnd step.\n    // second is half of the square/diamond width plus half of its height, you might want to use this value to decide how big the random value plus to the average is.\n    // finally, return the height\n    const random = Math.random() * Math.pow(2, -range / (129 * 2))\n    return base + random\n  }\n});\nconsole.log(data.data); // you would get a 2D-array of numbers\nconsole.log(data.max);  // the maximum number in all pixels\nconsole.log(data.min);  // the minimum number in all pixels\n```\n\nThere is also a WebAssembly version of the module:\n```javascript\nimport { Runner } from 'ds-heightmap/wasm';\nconst runner = new Runner(\n  129,   // width\n  129,   // height\n  2000,  // depth\n  1      // rough\n);\nconst data = runner.ds();\nconsole.log(data.data);\nconsole.log(data.max);\nconsole.log(data.min);\n```\n\n*Note: `randomizer` is not supported in WebAssembly version yet.*\n\n### Render the map\n\nHere is an example of how to render the data on canvas:\n\n```javascript\nimport { ds } from 'ds-heightmap'\n\nconst width = 400\nconst height = 300\nconst { data, max, min } = ds({\n  width,\n  height\n})\n\nconst range = max - min\nconst colorData = []\nfor (let i = 0; i \u003c height; i++) {\n  for (let j = 0; j \u003c width; j++) {\n    const level = (data[j][i] - min) / range\n    if (level \u003e 0.8) {\n      colorData.push(186, 174, 154, 255)\n    } else if (level \u003e 0.6) {\n      colorData.push(222, 214, 163, 255)\n    } else if (level \u003e 0.4) {\n      colorData.push(209, 215, 171, 255)\n    } else if (level \u003e 0.2) {\n      colorData.push(189, 204, 150, 255)\n    } else {\n      colorData.push(148, 191, 139, 255)\n    }\n  }\n}\n\nconst imageData = new ImageData(\n  Uint8ClampedArray.from(colorData),\n  width,\n  height,\n);\n\nconst canvas = document.getElementById('canvas')\ncanvas.width = width\ncanvas.height = height\nconst ctx = canvas.getContext('2d')\nctx.putImageData(imageData, 0, 0)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffralonra%2Fds-heightmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffralonra%2Fds-heightmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffralonra%2Fds-heightmap/lists"}