phrogz / context-blender Goto Github PK
View Code? Open in Web Editor NEWPhotoshop-style blend modes for HTML Canvas Contexts
License: Other
Photoshop-style blend modes for HTML Canvas Contexts
License: Other
Hi,
I just stumbled upon this demo, which is related to what you're doing.
They have equations for all the blend modes; see this page (pasted below). You may find this useful, since this is what you want help with.
/**
* JavaScript implementation of common blending modes, based on
* http://stackoverflow.com/questions/5919663/how-does-photoshop-blend-two-images-together
*
* Note that I'm not using <code>this</code> to reference current object
* for faster execution
*/
var blendingModes = {
normal: function(a, b) {
return a;
},
lighten: function(a, b) {
return (b > a) ? b : a;
},
darken: function(a, b) {
return (b > a) ? a : b;
},
multiply: function(a, b) {
return (a * b) / 255;
},
average: function(a, b) {
return (a + b) / 2;
},
add: function(a, b) {
return Math.min(255, a + b);
},
substract: function(a, b) {
return (a + b < 255) ? 0 : a + b - 255;
},
difference: function(a, b) {
return Math.abs(a - b);
},
negation: function(a, b) {
return 255 - Math.abs(255 - a - b);
},
screen: function(a, b) {
return 255 - (((255 - a) * (255 - b)) >> 8);
},
exclusion: function(a, b) {
return a + b - 2 * a * b / 255;
},
overlay: function(a, b) {
return b < 128
? (2 * a * b / 255)
: (255 - 2 * (255 - a) * (255 - b) / 255);
},
softLight: function(a, b) {
return b < 128
? (2 * ((a >> 1) + 64)) * (b / 255)
: 255 - (2 * (255 - (( a >> 1) + 64)) * (255 - b) / 255);
},
hardLight: function(a, b) {
return blendingModes.overlay(b, a);
},
colorDodge: function(a, b) {
return b == 255 ? b : Math.min(255, ((a << 8 ) / (255 - b)));
},
colorBurn: function(a, b) {
return b == 0 ? b : Math.max(0, (255 - ((255 - a) << 8 ) / b));
},
linearDodge: function(a, b) {
return blendingModes.add(a, b);
},
linearBurn: function(a, b) {
return blendingModes.substract(a, b);
},
linearLight: function(a, b) {
return b < 128
? blendingModes.linearBurn(a, 2 * b)
: blendingModes.linearDodge(a, (2 * (b - 128)));
},
vividLight: function(a, b) {
return b < 128
? blendingModes.colorBurn(a, 2 * b)
: blendingModes.colorDodge(a, (2 * (b - 128)));
},
pinLight: function(a, b) {
return b < 128
? blendingModes.darken(a, 2 * b)
: blendingModes.lighten(a, (2 * (b - 128)));
},
hardMix: function(a, b) {
return blendingModes.vividLight(a, b) < 128 ? 0 : 255;
},
reflect: function(a, b) {
return b == 255 ? b : Math.min(255, (a * a / (255 - b)));
},
glow: function(a, b) {
return blendingModes.reflect(b, a);
},
phoenix: function(a, b) {
return Math.min(a, b) - Math.max(a, b) + 255;
}
};
This JSFiddle http://jsfiddle.net/4K6aJ/3/ shows the tearing and artifacts in action. Perhaps either forcibly round those numbers if they must be integers or alternatively include mention in the docs that non-integers will cause tearing?
Hello, does this library offer tint?
This library seriously lack ability to define "how much" do I want to blend the layer.
Let's say addition: now it's doing like:
result = destination + source
What it should do:
result = destination + source * factor
where factor is a float between 0.0 and 1.0
Couldn't find any online information on the blend mode called "src-in", couldn't find a matching blend mode in CS6
@Phrogz I'm having trouble implementing this in the browser because I am new to canvas
. I was wondering if you could add an example of the usage of this plugin. Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.