Wondering what’s next for npm?Check out our public roadmap! »

    canvas-object-fit

    0.8.1 • Public • Published

    canvas-object-fit

    project status license build status dependencies status devDependencies status coverage status climate status

    Easily draw images in your canvas with object-type contraints.

    • Works with node-canvas on the server.
    • Supports EXIF orientation values to handle mobile pictures.

    Usage

    Quickstart

    1. Draw an image with a cover object-fit:

      import Canvas from 'canvas';
      import {drawImage} from 'canvas-object-fit';
       
      const [width, height] = [200, 200];
      const canvas = new Canvas(width, height);
      const context = canvas.getContext('2d');
      const image = new Canvas.Image();
      image.src = await fs.readFileAsync(`${fixturesPath}/image.jpg`);
      drawImage(context, image, 0, 0, canvas.width, canvas.height, {objectFit: 'cover'});
      const buffer = canvas.toBuffer('png');
    2. Draw an image with a contain object-fit and 90-CW exif orientation:

      import Canvas from 'canvas';
      import {drawImage} from 'canvas-object-fit';
       
      const [width, height] = [200, 200];
      const canvas = new Canvas(width, height);
      const context = canvas.getContext('2d');
      const image = new Canvas.Image();
      image.src = await fs.readFileAsync(`${fixturesPath}/image-rotated.jpg`);
      drawImage(context, image, 0, 0, canvas.width, canvas.height, {objectFit: 'contain', orientation: 6});
      const buffer = canvas.toBuffer('png');

    Exif orientations

    //   1        2       3      4         5            6           7          8
    //  888888  888888      88  88      8888888888  88                  88  8888888888
    //  88          88      88  88      88  88      88  88          88  88      88  88
    //  8888      8888    8888  8888    88          8888888888  8888888888          88
    //  88          88      88  88
    //  88          88  888888  888888
    export const EXIF_ORIENTATIONS = [
      {op: 'none', radians: 0},
      {op: 'none', radians: 0},
      {op: 'flip-x', radians: 0},
      {op: 'none', radians: Math.PI},
      {op: 'flip-y', radians: 0},
      {op: 'flip-x', radians: Math.PI / 2},
      {op: 'none', radians: Math.PI / 2},
      {op: 'flip-x', radians: -Math.PI / 2},
      {op: 'none', radians: -Math.PI / 2}
    ];

    Available scripts

    Script Description
    test Run mocha unit tests
    lint Run eslint static tests
    test:watch Run and watch mocha unit tests
    compile Compile the library

    Authors

    Olivier Louvignes

    License

    The MIT License
    
    Copyright (c) 2016 Olivier Louvignes
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    

    Install

    npm i canvas-object-fit

    DownloadsWeekly Downloads

    219

    Version

    0.8.1

    License

    MIT

    Unpacked Size

    1.05 MB

    Total Files

    26

    Last publish

    Collaborators

    • avatar