npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

under-canvas

v0.3.1

Published

Plain sailing image previews, powered by canvas.

Downloads

5

Readme

under-canvas.js

Plain sailing image previews, powered by canvas.

npm CircleCI GitHub issues GitHub license


Have you ever wanted to preview an image before it gets uploaded? Or generate a thumbnail of an image on the fly?

Canvas can be extremely powerful when it comes to manipulating images, but there's a lot of complexity just to perform seemingly easy tasks.

Under Canvas makes it incredibly effortless to generate image previews from a number of different sources.

The function returns a Promise, which resolves to base64 data URI.


Installation

npm install --save under-canvas

ES6 import

import underCanvas from 'under-canvas';

Alternatively, you can use it directly in the browser:

<script type='http://unpkg.com/under-canvas'></script>

underCanvas is directly attached to the window:

window.underCanvas()

Usage

External URL

Note that manipulating images from a remote source requires Access-Control-Allow-Origin to be set.

underCanvas('http://localhost:8080/image.jpg').then((b64) => {
  const img = document.createElement('img');
  img.src = b64;
  document.getElementById('root').appendChild(img);
})

Base-64 encoded image

underCanvas('data:image/png;base64,...').then((b64) => {
  const img = document.createElement('img');
  img.src = b64;
  document.getElementById('root').appendChild(img);
})

API Usage

underCanvas(image, [options])

Returns a Promise which resolves a base64 string of the preview.

background

Type: string

Background colour to use if not transparent. Useful when scaling to fit images.

Defaults to transparent.

format

Type: string Default: image/png

Format of the image to export.

Supports a number of different image formats (dependant on browser). eg. image/jpeg and image/png are supported by all browsers, but Chrome also supports image/webp.

quality

Type: number Default: 0.9

width

Type: number

Width of the exported image.

Defaults to original size.

height

Type: number

Height of the exported image.

Defaults to original size.

crop

Type: boolean Default: false

Resizes the image to fill the size and crops excess edges.

By default, the original image will be resized to fit within the specified size, ensuring the image is fully visible and no cropping occurs. If crop is set to true, the image will be resized to ensure that the width and height are greater than or equal to the expected size.

Planned features

  • Generating previews from input[type=file]
  • Detect browser support for Canvas
  • nodeJS support using node-canvas

Contributing

Contributions are certainly welcome. Please take a look at any existing issues.