• Stars
    star
    619
  • Rank 72,496 (Top 2 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 14 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Photoshop-style blend modes for HTML Canvas Contexts

About

Adobe® Photoshop® has a variety of helpful blend modes for compositing images from multiple RGBA layers. This small library provides the same functionality for HTML Canvas Contexts, with the goal of producing the same results as Photoshop.

Syntax

overContext.blendOnto( underContext, blendMode, offsetOptions );
  - overContext   : A CanvasRenderingContext2D
  - underContext  : A CanvasRenderingContext2D
  - blendMode     : A string with the blend mode to use, e.g. 'screen'
  - offsetOptions : [optional] JS Object with some/all of the following keys:
      destX, destY
      The X/Y location in the 'underContext' to blend onto; both default to 0.

      sourceX, sourceY
      The X/Y location in the 'overContext' to blend from; both default to 0.

      width,height
      The size of the box to blend; both default to 'auto', blending up to the
      right and bottom edges of the 'over' context.

      Width and height may less than specified if there is not enough space
      on the over or under contexts to fit the blend.

Use

In Node.js

  1. Install the node module

     npm install context-blender
    

    This will also install node-canvas, which requires a working Cairo install. See https://github.com/Automattic/node-canvas#installation for more details.

  2. Use the library like so in the scripts:

    // Requires the canvas library and augments it for you
    var Canvas = require('context-blender');
    
    var over  = new Canvas(100,100).getContext('2d');
    var under = new Canvas(100,100).getContext('2d');
    
    // …drawing something to both canvas contexts, and then:
    
    // Blend all of 'over' onto 'under', starting at the upper left corner
    over.blendOnto(under,'screen');
    
    // Blend all of 'over' onto 'under' (again), starting at 17,42 in 'under'
    over.blendOnto(under,'multiply',{destX:17,destY:42});
    
    // Blend a 16x16 tile from 'over' onto 'under' (again), starting at 17,42 in 'under'
    over.blendOnto(under,'add',{destX:17,destY:42,sourceX:32,sourceY:128,width:16,height:16});

In a Web Browser

// Likely an 'offscreen' (not in the DOM) canvas
var over = someCanvas.getContext('2d');

// Usually a canvas that is shown on the page
var under = anotherCanvas.getContext('2d');

// Blend all of 'over' onto 'under', starting at the upper left corner
over.blendOnto(under,'screen');

// Blend all of 'over' onto 'under' (again), starting at 17,42 in 'under'
over.blendOnto(under,'multiply',{destX:17,destY:42});

// Blend a 16x16 tile from 'over' onto 'under' (again), starting at 17,42 in 'under'
over.blendOnto(under,'add',{destX:17,destY:42,sourceX:32,sourceY:128,width:16,height:16});

Supported Blend Modes

The following blend modes work perfectly (or as nearly as the vagaries of the HTML Canvas allow):

  • normal (or src-over)
  • src-in
  • screen
  • multiply
  • difference
  • exclusion

The following additional blend modes mostly work as intended, but have issues when it comes to dealing with low-opacity colors.

Test images are the result of blending

over top of

(where the "lighter" repetitions are the result of lowered opacity).

  • add (or plus) - Photoshop's "Linear Dodge (add)" blend mode does not perform addition on the opacities of the two layers. I have not yet figured out what it does instead. For now, this mode performs simple numeric addition, the same as the SVG 1.2 "plus" mode.

    Photoshop context-blender
  • lighten (or lighter) - the result is slightly too dark when the opacity falls and incorrectly 'favors' a higher-opacity source.

    Photoshop context-blender
  • darken (or darker) - the result is too dark when combining low-opacity regions, and does not properly 'favor' the higher-opacity source.

    Photoshop context-blender
  • overlay - this is correct where both the over and under images are 100% opaque; the lower the alpha of either/both images, the more the colors become too desaturated.

    Photoshop context-blender
  • hardlight - this is the opposite of "overlay" and experiences similar problems where either image is not fully opaque.

    Photoshop context-blender
  • colordodge (or dodge) - works correctly only under 100% opacity

    Photoshop context-blender
  • colorburn (or burn) - works correctly only under 100% opacity

    Photoshop context-blender
  • softlight

    Photoshop context-blender
  • luminosity

    Photoshop context-blender
  • color

    Photoshop context-blender
  • hue

    Photoshop context-blender
  • saturation

    Photoshop context-blender
  • lightercolor

    Photoshop context-blender
  • darkercolor

    Photoshop context-blender

Requirements/Browser Support

Should work on any user agent that supplies a CanvasRenderingContext2D along with getImageData and putImageData.

This includes using the node-canvas library under Node.js.

About

This library was created around the need solely for a one-off 'screen' blend mode to match the company-mandated style for bar graphs used internally, previously only available via a Microsoft® Excel® template. Clearly this functionality is useful in more contexts than just my one-off, so I decided to make a framework around it and encourage others to help figure out the formulae. Please, fork this project, add blend modes and/or fix math, and send me pull requests! I feel certain that the resources must exist out there on the equations Photoshop uses in the presence of alpha, but so far I have not found them.

History

v1.3.3 - 2014-Nov-13

  • Fix alpha on all blend modes to exactly match Photoshop.

v1.3.0 - 2014-Nov-12

  • Release as a Node.js module.
  • Add blend modes: softlight, luminosity, color, hue, saturation, lightercolor, darkercolor.
  • Greatly improve the accuracy of many blend modes.

Great thanks to Pixelero for amazing contributions!

v1.2.1 - 2011-Feb-9

  • Improve perf of lighten and darken blend modes.

v1.2.0 - 2010-Dec-14

  • Add blend modes: hardlight, colordodge, colorburn, darken, lighten, exclusion. Thanks gingerbeardman!

v1.1.1 - 2010-Dec-12

  • Improve result of overlay blend mode.

v1.1.0 - 2010-Dec-6

  • Added array blendOnto.supportedBlendModes for enumerating modes.
  • Added object blendOnto.supports for testing if a mode is supported.
  • Test for getImageData() to be present (prevent loading on excanvas).

v1.0 - 2010-Nov-30

  • Initial working release.
  • Supported blend modes: normal, screen, multiply, difference, src-in, add
  • Known broken: overlay, dodge

Reference Material

Contact

To report bugs or request additional features, please use the Context-Blender Issues page for this project.

License

This library is released under an MIT-style license. That generally means that you are free to do almost anything you want with it as long as you give a bit of credit where credit is due. See the LICENSE file included for the actual legal limitations.s and/or fix math, and send me pull requests! I feel certain that the resources must exist out there on the equations Photoshop uses in the presence of alpha, but so far I have not found them.

More Repositories

1

riblits

Shell for new Sinatra Applications
Ruby
160
star
2

SLAXML

SAX-like streaming XML parser for Lua
Lua
148
star
3

NeatJSON

Pretty-print your JSON in Ruby, JS, or Lua with more power than JSON.stringify or JSON.pretty_generate
Lua
102
star
4

svg2geojson

Converts an SVG file with added geo-referencing tags into one or more GeoJSON files.
JavaScript
54
star
5

svg-path-to-polygons

Converts path commands for an SVG path to polygon points.
JavaScript
53
star
6

LXSC

Lua XML StateChart interpreter - parses and executes SCXML state machines with a Lua data model.
Lua
36
star
7

docubot

Create CHM documentation from simple hierarchy of plain text files.
Ruby
24
star
8

Lunity

Simple-but-rich unit testing for Lua
Lua
22
star
9

PEGSH

Realtime PEG Syntax Highlighting in a web browser
JavaScript
16
star
10

visual-scxml-editor

VS Code extension for visualizing and graphically editing SCXML statecharts
JavaScript
8
star
11

rb3jay

Democratic crowd-controlled office music player.
JavaScript
6
star
12

omnifense

2-player, turn-based, offense/defense pseudo-tower-defense-style game. Sort of.
JavaScript
4
star
13

RXSC

Ruby XML StateChart machine for running W3C SCXML files with a Ruby data model in a non-threaded manner.
Ruby
4
star
14

notifitable

Lua tables that notify you when specific values have changed.
Lua
4
star
15

liqrrdmetal

Calculate scoring of autocomplete-style substring matches, identifying which portions of the string were matched.
Ruby
3
star
16

DrumSaber

RGB LED lighting effects generated by live drumming.
C++
2
star
17

d3hierarchy

Structured Documentation for the D3.js objects
2
star
18

Flooph

A simple, safe, sandbox for evaluating simple conditionals, expressions, and templates.
Ruby
1
star
19

ZSS

A pure Lua library that parses (limited) CSS rules and computes the declarations to apply to elements.
Lua
1
star
20

Liqr

Substring filtering for Lua
Lua
1
star
21

SCXMLEditor

Simple web-based editor for SCXML
JavaScript
1
star
22

RUIC

Ruby API for NVIDIA's UI Composer, with a mini DSL for easy analysis and manipulation.
Ruby
1
star
23

Docdo

Generic Ruby document management with lightweight undo/redo stacks.
Ruby
1
star
24

BubbleDiagram

Qt/QML-based interactive architecture space layout
QML
1
star
25

hatshuffler

Creating seasons of hat tournaments that are diverse and fair.
JavaScript
1
star
26

laink

Game server handling remote AI clients playing turn-based games over TCP Sockets and JSON.
Ruby
1
star
27

Khronic

YAML-based mod tracker music for Ruby.
Ruby
1
star
28

SmoothedOdometry

Simply fusing together robot odometry and external measurements in a quick, "good enough" fashion.
JavaScript
1
star