Context blender

HTML 画布上下文的 Photoshop 风格混合模式。(Photoshop-style blend modes for HTML Canvas Contexts)

Github星跟蹤圖

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.

主要指標

概覽
名稱與所有者Phrogz/context-blender
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台Linux, Mac, Web browsers, Windows
許可證Other
所有者活动
創建於2010-11-29 02:58:55
推送於2018-07-26 17:37:03
最后一次提交2018-07-26 11:37:03
發布數7
最新版本名稱v1.3.3 (發布於 2014-11-13 09:32:07)
第一版名稱v1.0.0 (發布於 2010-11-30 23:35:13)
用户参与
星數623
關注者數29
派生數65
提交數59
已啟用問題?
問題數9
打開的問題數7
拉請求數4
打開的拉請求數3
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?