{"id":15706638,"url":"https://github.com/transitive-bullshit/rect-contain","last_synced_at":"2025-05-12T19:07:51.180Z","repository":{"id":57349355,"uuid":"125582744","full_name":"transitive-bullshit/rect-contain","owner":"transitive-bullshit","description":"Computes a transform between two rects just like background-size: contain.","archived":false,"fork":false,"pushed_at":"2020-07-11T23:13:06.000Z","size":564,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-12T19:07:30.315Z","etag":null,"topics":["background-size","contain","rect","rectangle"],"latest_commit_sha":null,"homepage":"https://transitive-bullshit.github.io/rect-contain/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/transitive-bullshit.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-17T00:14:00.000Z","updated_at":"2024-06-17T03:23:29.000Z","dependencies_parsed_at":"2022-08-29T21:00:21.125Z","dependency_job_id":null,"html_url":"https://github.com/transitive-bullshit/rect-contain","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/transitive-bullshit%2Frect-contain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Frect-contain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Frect-contain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Frect-contain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/transitive-bullshit","download_url":"https://codeload.github.com/transitive-bullshit/rect-contain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253805861,"owners_count":21967053,"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":["background-size","contain","rect","rectangle"],"created_at":"2024-10-03T20:25:38.581Z","updated_at":"2025-05-12T19:07:51.129Z","avatar_url":"https://github.com/transitive-bullshit.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rect-contain ([demo](https://transitive-bullshit.github.io/rect-contain/))\n\n\u003e Computes a transform between two rectangles the same way as `background-size: contain`.\n\n[![NPM](https://img.shields.io/npm/v/rect-contain.svg)](https://www.npmjs.com/package/rect-contain) [![Build Status](https://travis-ci.com/transitive-bullshit/rect-contain.svg?branch=master)](https://travis-ci.com/transitive-bullshit/rect-contain) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## Intro\n\nSimulating `background-size: contain` is very useful for canvas animations, drawing sprites in games, and rendering videos. This module is used as part of our video rendering pipeline at [Automagical](https://automagical.ai/).\n\n## Install\n\n```bash\nnpm install --save rect-contain\n```\n\n## Usage\n\nCheck out the [demo](https://transitive-bullshit.github.io/rect-contain/) to understand what this module does.\n\n```js\nconst rectContain = require('rect-contain')\n\nconst {\n  scale,\n  translate\n} = rectContain({ width: 500, height: 500 }, { width: 1280, height: 720 })\n\n// CSS transforms apply the rightmost operation first.\n// Note that this assumes a transform-origin of `top left`.\nconst transform = `translate(${translate.x}px, ${translate.y}px) scale(${scale})`\n```\n\n## API\n\n### rectContain(viewport, image)\n\nReturns a 2D affine transform defined by `{ scale: Number, translate: { x: Number, y: Number } }` such that the given `image` will be fully contained within the `viewport` and also maintain its original aspect ratio.\n\nNote that you'll want to apply the `scale` transform first followed by the `translate`.\n\n#### viewport\n\nType: `{ width: Number, height: Number }`\n\nThe viewport you want to draw the image into.\n\n#### image\n\nType: `{ width: Number, height: Number }`\n\nThe dimensions of the image to be drawn.\n\n## Related\n\n- [rect-cover](https://github.com/transitive-bullshit/rect-cover) is this module's sister, implementing `background-size: cover`.\n- [rect-crop](https://github.com/gre/rect-crop) if you need to mimic rect-cover with a `zoom` ratio and `center` point (eg, for [kenburns effects](https://github.com/gre/kenburns))\n- [css background-size](https://developer.mozilla.org/en-US/docs/Web/CSS/background-size)\n\n## License\n\nMIT © [Travis Fischer](https://github.com/transitive-bullshit)\n\nSupport my OSS work by \u003ca href=\"https://twitter.com/transitive_bs\"\u003efollowing me on twitter \u003cimg src=\"https://storage.googleapis.com/saasify-assets/twitter-logo.svg\" alt=\"twitter\" height=\"24px\" align=\"center\"\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftransitive-bullshit%2Frect-contain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftransitive-bullshit%2Frect-contain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftransitive-bullshit%2Frect-contain/lists"}