Skip to content
프로젝트

소개

HTML5 canvas 요소를 사용해 이미지에 레트로/빈티지 효과 추가

디자인 및 미디어

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

vintageJS

Add a retro/vintage effect to images using the HTML5 canvas element.

npm npm Greenkeeper badge

Installation

$ npm install vintagejs

How to use

vintagejs is a function that takes a source (URL, ImageElement or CanvasElement) and an effect (object with all the options) and returns a Promise that resolves to a result object.

vintagejs('./path/to/picture.jpg', { brightness: 0.2 })
  .then(res => res.genImage())
  .then(img => document.body.appendChild(img));

The result object provides the following methods to access the modified image data:

// returns the data url of the updated image. Use it to update the source of an existing image
getDataURL(mimeType?: string, quality?: number): string;
// returns the canvas with the updated image. Use it to draw your changes onto another canvas
getCanvas(): HTMLCanvasElement;
// returns a promise that resolves to an HTMLImageElement of the updated image
genImage(mimeType?: string, quality?: number): Promise<HTMLImageElement>;

If not provided, mimeType defaults to image/jpeg and quality defaults to 1.

In a browser window, getCanvas() returns an HTMLCanvasElement. Use getBlob(mimeType?, quality?): Promise<Blob> to encode the result without building a data URL; it works everywhere, including Web Workers.

Using vintagejs in a Web Worker

Effects are applied pixel by pixel, which can block the main thread on large photos. vintagejs also runs in a Web Worker, where there is no DOM: pass an ImageBitmap or OffscreenCanvas as the source and the result lives on an OffscreenCanvas. Viewfinder images are loaded with fetch and createImageBitmap there.

// worker.js
import vintagejs from 'vintagejs';

self.onmessage = async ({ data: { bitmap, effect } }) => {
  const res = await vintagejs(bitmap, effect); // TS: vintagejs<OffscreenCanvas>(...)
  const result = res.getCanvas().transferToImageBitmap();
  self.postMessage({ result }, [result]);
};

// main.js
const bitmap = await createImageBitmap(file);
worker.postMessage({ bitmap, effect: { brightness: 0.2 } }, [bitmap]);

getDataURL() and genImage() need the DOM, so in a worker they throw an error pointing to getBlob().

More Examples

// use an image as source and update image with data url
const srcEl = document.querySelector('img.myImage');
vintagejs(srcEl, { brightness: 0.2 })
  .then(res => {
    srcEl.src = res.getDataURL();
  });

// use a canvas as source and draw result to canvas
const srcEl = document.querySelector('canvas.myCanvas');
const ctx = srcEl.getContext('2d');
vintagejs(srcEl, { brightness: 0.2 })
  .then(res => {
    ctx.drawImage(res.getCanvas(), 0, 0, srcEl.width, srcEl.height);
  });

Effect options

type TEffect = {
  curves: false | TCurve,     // default: false
  screen: false | TRGBAColor, // default: false
  saturation: number,         // float between 0 and 1, default: 1
  vignette: number,           // float between 0 and 1, default: 0
  lighten: number,            // float between 0 and 1, default: 0
  viewfinder: false | string, // string must be URL, default: false
  sepia: boolean,             // default: false
  gray: boolean,              // default: false
  brightness: number,         // float between -1 and 1, default: 0
  contrast: number,           // float between -1 and 1, default: 0
  grain: number,              // float between 0 and 1, default: 0
  lightLeak: false | TLightLeak, // default: false
};

// A warm glow bleeding in from one edge of the photo
type TLightLeak = {
  intensity: number,                            // float between 0 and 1
  edge?: 'left' | 'right' | 'top' | 'bottom',  // default: 'right'
  color?: { r: Uint8, g: Uint8, b: Uint8 },    // default: warm orange (255, 112, 36)
};

// every channel, r=red, g=green, b=blue serves as a look up table for color mappings
type TCurve = {
  r: Array<Uint8> | Uint8ClampedArray, // array of int between 0 and 255, length of array === 256
  g: Array<Uint8> | Uint8ClampedArray, // array of int between 0 and 255, length of array === 256
  b: Array<Uint8> | Uint8ClampedArray, // array of int between 0 and 255, length of array === 256
};

type TRGBAColor = {
  r: Uint8,  // int between 0 and 255
  g: Uint8,  // int between 0 and 255
  b: Uint8,  // int between 0 and 255
  a: number, // float between 0 and 1
};

Examples

const noEffect = {};

const effect_1 = {
  brightness: -0.2,
  contrast: 0.15,
};

const effect_2 = {
  brightness: 0.1,
  vignette: 0.3,
  viewfinder: './film-1.jpg',
  screen: {
    r: 227,
    g: 12,
    b: 169,
    a: 0.15,
  },
};

const effect_3 = {
  grain: 0.35,
  lightLeak: { intensity: 0.5, edge: 'left' },
  vignette: 0.3,
};

Film grain scales with the image, so a small preview and the full-size photo look the same, and it is identical on every run (no flicker between renders).

See examples folder for more examples.

Browser support

Check support for the canvas element canisue.com/canvas.

Higher performance when canvas blend modes are supported caniuse.com/#feat=canvas-blending, but fallbacks are implemented.

License

MIT

Changelog

2.5.0

  • New effect grain: film grain (0–1) that shows most in the midtones. It scales with the image size and is the same on every run, so previews match full-size renders
  • New effect lightLeak: a warm glow bleeding in from one edge ({ intensity, edge?, color? })
  • New exported types TLightLeak, TLightLeakEdge and TRGBColor
  • Tests render real pixels in Node with @napi-rs/canvas

2.4.0

  • Web Worker support: OffscreenCanvas and ImageBitmap are accepted as sources, and without a DOM the effect renders to an OffscreenCanvas and viewfinders load with fetch + createImageBitmap. Behavior in browser windows is unchanged
  • Added getBlob(mimeType?, quality?) to the result object
  • TResult takes an optional canvas type parameter (TResult<OffscreenCanvas>); it defaults to HTMLCanvasElement, so existing typings are unchanged
  • The package entry now re-exports the public types (TEffect, TResult, TSource, TCurve, TRGBAColor, TCanvas, TDrawable)
  • Fixed: the returned promise never settled when a viewfinder image failed to load; it now rejects

2.3.0

  • Migrated the toolchain from Flow + Browserify to TypeScript + Vite; the library is now written in TypeScript and ships .d.ts type declarations
  • Added an ES module build (dist/vintage.mjs) alongside the existing UMD (dist/vintage.js) and minified (dist/vintage.min.js) bundles
  • Fixed all 31 npm audit vulnerabilities (5 critical, 9 high) by removing the legacy build/test dependencies — no runtime dependency was affected
  • No API changes: the UMD global vintagejs and the function signature are unchanged

2.2.0

  • Added true grayscale effect (Thanks @bjornbos for PR #38)

2.1.0

  • Add support for strings (URI or base64 encoded data-uri) as a source

2.0.0

  • Rewrite from ground up
  • Functional API

1.1.5

  • Added "main" field to package.json

1.1.4

  • Added universal module definition (umd) wrapper

1.1.3

  • Added minified versions
  • Fixed same-origin error

1.1.2

  • added AngularJS support thanks to @dpiccone
  • grunt based build script for all versions

1.1.1

  • performance improvements
  • new effect options:
    • brightness
    • contrast

1.1.0

  • Improved core performance

1.0.0

  • Initial release

관련 프로젝트 살펴보기

불러오는 중…