{"id":27440555,"url":"https://github.com/gucheen/rect-restrict","last_synced_at":"2025-04-14T22:57:03.992Z","repository":{"id":57114484,"uuid":"316433190","full_name":"gucheen/rect-restrict","owner":"gucheen","description":"to restrict your target to some areas(rectangle).","archived":false,"fork":false,"pushed_at":"2020-12-02T06:18:24.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T22:56:57.680Z","etag":null,"topics":["position","rectangle","restrict"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/gucheen.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}},"created_at":"2020-11-27T07:42:45.000Z","updated_at":"2021-01-13T06:35:09.000Z","dependencies_parsed_at":"2022-08-22T00:20:11.191Z","dependency_job_id":null,"html_url":"https://github.com/gucheen/rect-restrict","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gucheen%2Frect-restrict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gucheen%2Frect-restrict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gucheen%2Frect-restrict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gucheen%2Frect-restrict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gucheen","download_url":"https://codeload.github.com/gucheen/rect-restrict/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975324,"owners_count":21192208,"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":["position","rectangle","restrict"],"created_at":"2025-04-14T22:57:03.386Z","updated_at":"2025-04-14T22:57:03.972Z","avatar_url":"https://github.com/gucheen.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rect Restrict\n\n[![rect-restrict](https://badgen.net/npm/v/@gucheen/rect-restrict)](https://www.npmjs.com/package/@gucheen/rect-restrict)\n[![Coverage Status](https://coveralls.io/repos/github/gucheen/rect-restrict/badge.svg?branch=main)](https://coveralls.io/github/gucheen/rect-restrict?branch=main)\n\nRectRestrict is simple util to help you restrict your target to some areas(rectangle).\n\n## Motivation\n\nRecently in a project of mine, I need to restrict a user-draggable item to several areas.\n\nUsers can drag the item to anywhere on the web page, but the item finally needs to be in several areas when it was dropped.\n\nSo, I wrote this simple util.\n\n## Usage\n\n```\nnpm i @gucheen/rect-restrict\n```\n\n```ts\nimport { RectRestrict } from '@gucheen/rect-restrict';\n\nconst rectRestrict = new RectRestrict();\n\nrectRestrict.setAreas([\n  {\n    id: 1,\n    top: 50,\n    left: 50,\n    width: 200,\n    heigth: 200,\n  },\n]);\n\nconst result = rectRestrict.check({\n  top: 10,\n  left: 10,\n  width: 20,\n  height: 20,\n});\n\nconsole.log(result);\n\n/**\n{x: 50, y: 50, left: 50, top: 50, id: 1}\n* /\n```\n\n## Api\n\n### Area\n\n```ts\ninterface Area {\n  id?: any;\n  x?: number;\n  top?: number;\n  y?: number;\n  left?: number;\n  right?: number;\n  bottom?: number;\n  width: number;\n  height: number;\n  center?: [number, number];\n}\n// one of top and y should be provided\n// one of left and x should be provided\n// id will be returned in check() if it was provided\n```\n\n### class RectRestrict({areas}: {areas?: Area[]} = {})\n\n### static RectRestrict.optimizeRect(area: Partial\\\u003cArea\\\u003e): OptimizeArea\n\n```ts\ninterface OptimizeArea {\n  id?: number;\n  top: number;\n  left: number;\n  right: number;\n  bottom: number;\n  width: number;\n  height: number;\n  center: [number, number];\n}\n```\n\nThis static method is used internally to deal with the areas' data.\n\nYou may also use it to get formatted data.\n\n### RectRestrict.check(target: Area): RestrictPosition\n\n```ts\ninterface RestrictPosition {\n  top: number;\n  left: number;\n  x: number;\n  y: number;\n  id: any; // the id of area which this postion belongs to\n}\n```\n\nCheck if the target was in one of the areas.\n\nReturn the current position of the target if it is already in one of the areas.\n\nReturn the closest position of the closest area otherwise.\n\nThe closest position means that the restricted position will be one side or corner of the area.\n\n### RectRestrict.getPosInArea(pos: Partial\\\u003cArea\\\u003e): Area\n\nReturn the area if the given position is in it.\n\n## FAQ\n\n1. Is it too simple to cover some more complex demands?\n\n    This lib(util, well) is focused on the specific proposal - restrict a rectangle in others.\n\n2. What if I need to deal with a polygon or circle?\n\n    Refer to a physical engine or game engine to deal with complex problems.\n\n## MIT License\n\nCopyright (c) 2020 Cheng Gu\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgucheen%2Frect-restrict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgucheen%2Frect-restrict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgucheen%2Frect-restrict/lists"}