1.0.0 • Published 9 years ago

resize-canvas v1.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
9 years ago

resize-canvas

Build Status npm version

Change dimensions of an html5 canvas element, resizing from a relative position on canvas (center by default) and preserving image data

Details

Similar to how you can change resolution of an image in photoshop, but with granular control of the from position. Adjust with an absolute new size or a relative change. This new version resizes the canvas in place instead of returning you a new canvas with the different size. To get a new canvas, supply a copy of the original.

Installation

Download node at nodejs.org and install it, if you haven't already.

npm install resize-canvas --save

Usage

import resizeCanvas from 'resize-canvas'

let canvas = document.createElement('canvas')

// set height and width
canvas.width = 300
canvas.height = 200

// add to dom
document.body.appendChild(canvas)

// draw a circle
let ctx = canvas.getContext('2d')
ctx.beginPath()
ctx.arc(150, 100, 50, 0, 2 * Math.PI)
ctx.stroke()

// change size from [300, 200] to [300, 250] from center
resizeCanvas({
  canvas: canvas,
  size: [100, 50] // absolute size adjustment
})

// or by size difference adding to the top and left sides
resizeCanvas({
  canvas: canvas,
  diff: [-50, 150], // relative size adjustment
  from: [canvas.width, canvas.height] // from bottom right
})

canvas.width // 50
canvas.height // 200

API

resizeCanvas( { canvas, diff, size, from } )

Arguments

License

ISC