Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/transitive-bullshit/rect-contain
Computes a transform between two rects just like background-size: contain.
https://github.com/transitive-bullshit/rect-contain
background-size contain rect rectangle
Last synced: 23 days ago
JSON representation
Computes a transform between two rects just like background-size: contain.
- Host: GitHub
- URL: https://github.com/transitive-bullshit/rect-contain
- Owner: transitive-bullshit
- Created: 2018-03-17T00:14:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-11T23:13:06.000Z (over 4 years ago)
- Last Synced: 2024-10-20T01:09:07.268Z (27 days ago)
- Topics: background-size, contain, rect, rectangle
- Language: JavaScript
- Homepage: https://transitive-bullshit.github.io/rect-contain/
- Size: 551 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# rect-contain ([demo](https://transitive-bullshit.github.io/rect-contain/))
> Computes a transform between two rectangles the same way as `background-size: contain`.
[![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)
## Intro
Simulating `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/).
## Install
```bash
npm install --save rect-contain
```## Usage
Check out the [demo](https://transitive-bullshit.github.io/rect-contain/) to understand what this module does.
```js
const rectContain = require('rect-contain')const {
scale,
translate
} = rectContain({ width: 500, height: 500 }, { width: 1280, height: 720 })// CSS transforms apply the rightmost operation first.
// Note that this assumes a transform-origin of `top left`.
const transform = `translate(${translate.x}px, ${translate.y}px) scale(${scale})`
```## API
### rectContain(viewport, image)
Returns 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.
Note that you'll want to apply the `scale` transform first followed by the `translate`.
#### viewport
Type: `{ width: Number, height: Number }`
The viewport you want to draw the image into.
#### image
Type: `{ width: Number, height: Number }`
The dimensions of the image to be drawn.
## Related
- [rect-cover](https://github.com/transitive-bullshit/rect-cover) is this module's sister, implementing `background-size: cover`.
- [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))
- [css background-size](https://developer.mozilla.org/en-US/docs/Web/CSS/background-size)## License
MIT © [Travis Fischer](https://github.com/transitive-bullshit)
Support my OSS work by following me on twitter