CanvasRenderingContext2D: свойство globalCompositeOperation
Базовая линия Широко доступно
Эта функция хорошо зарекомендовала себя и работает на многих устройствах и версиях браузеров. Она доступна во всех браузерах с июля 2015 года.
Свойство CanvasRenderingContext2D.globalCompositeOperation API Canvas 2D задаёт тип операции композиции, применяемой при рисовании новых фигур.
См. также Композицию и обрезку в Учебнике по Canvas.
Значение
Строка, определяющая, какую из операций композиции или смешивания использовать. Это может быть любое из следующих значений:
"source-over"-
Это значение по умолчанию, и новые фигуры рисуются поверх существующего содержимого холста.
"source-in"-
Новая фигура рисуется только там, где она перекрывает существующий холст. Всё остальное делается прозрачным.
"source-out"-
Новая фигура рисуется там, где она не перекрывает существующее содержимое холста.
"source-atop"-
Новая фигура рисуется только там, где она перекрывает существующее содержимое холста.
"destination-over"-
Новые фигуры рисуются под существующим содержимым холста.
"destination-in"-
Существующее содержимое холста сохраняется там, где оно перекрывается новой фигурой. Всё остальное делается прозрачным.
"destination-out"-
Существующее содержимое сохраняется там, где оно не перекрывается новой фигурой.
"destination-atop"-
Существующий холст сохраняется только там, где он перекрывается новой фигурой. Новая фигура рисуется под содержимым холста.
"lighter"-
Там, где обе фигуры перекрываются, цвет определяется путем сложения значений цвета.
"copy"-
Показывается только новая фигура.
"xor"-
Фигуры делают прозрачными там, где они перекрываются, и рисуют нормально в остальных местах.
"multiply"-
Пиксели верхнего слоя умножаются на соответствующие пиксели нижнего слоя. Результатом является более тёмное изображение.
"screen"-
Пиксели инвертируются, умножаются и снова инвертируются. Результатом является более светлое изображение (противоположность
multiply) "overlay"-
Комбинация
multiplyиscreen. Тёмные части основного слоя становятся темнее, а светлые — светлее. "darken"-
Сохраняет самые тёмные пиксели обоих слоёв.
"lighten"-
Сохраняет самые светлые пиксели обоих слоёв.
"color-dodge"-
Делит нижний слой на инвертированный верхний слой.
"color-burn"-
Делит инвертированный нижний слой на верхний слой, а затем инвертирует результат.
"hard-light"-
Как
overlay, комбинацияmultiplyиscreen, но вместо этого с переставленными верхним и нижним слоями. "soft-light"-
Более мягкая версия
hard-light. Чистый черный или белый не приводит к чистому черному или белому. "difference"-
Вычитает нижний слой из верхнего слоя — или наоборот — чтобы всегда получить положительное значение.
"exclusion"-
Как
difference, но с более низким контрастом. "hue"-
Сохраняет яркость и цветность нижнего слоя, а оттенок принимает от верхнего слоя.
"saturation"-
Сохраняет яркость и оттенок нижнего слоя, а цветность принимает от верхнего слоя.
"color"-
Сохраняет яркость нижнего слоя, а оттенок и цветность принимает от верхнего слоя.
"luminosity"-
Сохраняет оттенок и цветность нижнего слоя, а яркость принимает от верхнего слоя.
Примеры
Изменение операции композиции
В этом примере используется свойство globalCompositeOperation для рисования двух прямоугольников, исключающих друг друга, где они перекрываются.
HTML
<canvas id="canvas"></canvas>
JavaScript
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.globalCompositeOperation = "xor";
ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 100, 100);
ctx.fillStyle = "red";
ctx.fillRect(50, 50, 100, 100);
Результат
Демонстрация всех значений
Глобальные значения
Этот код настраивает глобальные значения, используемые остальной частью программы.
const canvas1 = document.createElement("canvas");
const canvas2 = document.createElement("canvas");
const gco = [
"source-over",
"source-in",
"source-out",
"source-atop",
"destination-over",
"destination-in",
"destination-out",
"destination-atop",
"lighter",
"copy",
"xor",
"multiply",
"screen",
"overlay",
"darken",
"lighten",
"color-dodge",
"color-burn",
"hard-light",
"soft-light",
"difference",
"exclusion",
"hue",
"saturation",
"color",
"luminosity",
].reverse();
const gcoText = [
"This is the default setting and draws new shapes on top of the existing canvas content.",
"The new shape is drawn only where both the new shape and the destination canvas overlap. Everything else is made transparent.",
"The new shape is drawn where it doesn't overlap the existing canvas content.",
"The new shape is only drawn where it overlaps the existing canvas content.",
"New shapes are drawn behind the existing canvas content.",
"The existing canvas content is kept where both the new shape and existing canvas content overlap. Everything else is made transparent.",
"The existing content is kept where it doesn't overlap the new shape.",
"The existing canvas is only kept where it overlaps the new shape. The new shape is drawn behind the canvas content.",
"Where both shapes overlap the color is determined by adding color values.",
"Only the new shape is shown.",
"Shapes are made transparent where both overlap and drawn normal everywhere else.",
"The pixels of the top layer are multiplied with the corresponding pixel of the bottom layer. A darker picture is the result.",
"The pixels are inverted, multiplied, and inverted again. A lighter picture is the result (opposite of multiply)",
"A combination of multiply and screen. Dark parts on the base layer become darker, and light parts become lighter.",
"Retains the darkest pixels of both layers.",
"Retains the lightest pixels of both layers.",
"Divides the bottom layer by the inverted top layer.",
"Divides the inverted bottom layer by the top layer, and then inverts the result.",
"A combination of multiply and screen like overlay, but with top and bottom layer swapped.",
"A softer version of hard-light. Pure black or white does not result in pure black or white.",
"Subtracts the bottom layer from the top layer or the other way round to always get a positive value.",
"Like difference, but with lower contrast.",
"Preserves the luma and chroma of the bottom layer, while adopting the hue of the top layer.",
"Preserves the luma and hue of the bottom layer, while adopting the chroma of the top layer.",
"Preserves the luma of the bottom layer, while adopting the hue and chroma of the top layer.",
"Preserves the hue and chroma of the bottom layer, while adopting the luma of the top layer.",
].reverse();
const width = 320;
const height = 340;
Основная программа
При загрузке страницы этот код выполняется для настройки и запуска примера:
window.onload = () => {
// lum in sRGB
const lum = {
r: 0.33,
g: 0.33,
b: 0.33,
};
// resize canvas
canvas1.width = width;
canvas1.height = height;
canvas2.width = width;
canvas2.height = height;
lightMix();
colorSphere();
runComposite();
return;
};
А этот код, runComposite(), обрабатывает основную часть работы, опираясь на ряд вспомогательных функций для выполнения сложной части.
function createCanvas() {
const canvas = document.createElement("canvas");
canvas.style.background = `url(${op_8x8.data})`;
canvas.style.border = "1px solid #000";
canvas.style.margin = "5px";
canvas.width = width / 2;
canvas.height = height / 2;
return canvas;
}
function runComposite() {
const dl = document.createElement("dl");
document.body.appendChild(dl);
while (gco.length) {
const pop = gco.pop();
const dt = document.createElement("dt");
dt.textContent = pop;
dl.appendChild(dt);
const dd = document.createElement("dd");
const p = document.createElement("p");
p.textContent = gcoText.pop();
dd.appendChild(p);
const canvasToDrawOn = createCanvas();
const canvasToDrawFrom = createCanvas();
const canvasToDrawResult = createCanvas();
let ctx = canvasToDrawResult.getContext("2d");
ctx.clearRect(0, 0, width, height);
ctx.save();
ctx.drawImage(canvas1, 0, 0, width / 2, height / 2);
ctx.globalCompositeOperation = pop;
ctx.drawImage(canvas2, 0, 0, width / 2, height / 2);
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "rgb(0 0 0 / 80%)";
ctx.fillRect(0, height / 2 - 20, width / 2, 20);
ctx.fillStyle = "#FFF";
ctx.font = "14px arial";
ctx.fillText(pop, 5, height / 2 - 5);
ctx.restore();
ctx = canvasToDrawOn.getContext("2d");
ctx.clearRect(0, 0, width, height);
ctx.save();
ctx.drawImage(canvas1, 0, 0, width / 2, height / 2);
ctx.fillStyle = "rgb(0 0 0 / 80%)";
ctx.fillRect(0, height / 2 - 20, width / 2, 20);
ctx.fillStyle = "#FFF";
ctx.font = "14px arial";
ctx.fillText("existing content", 5, height / 2 - 5);
ctx.restore();
ctx = canvasToDrawFrom.getContext("2d");
ctx.clearRect(0, 0, width, height);
ctx.save();
ctx.drawImage(canvas2, 0, 0, width / 2, height / 2);
ctx.fillStyle = "rgb(0 0 0 / 80%)";
ctx.fillRect(0, height / 2 - 20, width / 2, 20);
ctx.fillStyle = "#FFF";
ctx.font = "14px arial";
ctx.fillText("new content", 5, height / 2 - 5);
ctx.restore();
dd.appendChild(canvasToDrawOn);
dd.appendChild(canvasToDrawFrom);
dd.appendChild(canvasToDrawResult);
dl.appendChild(dd);
}
}
Вспомогательные функции
Программа опирается на ряд вспомогательных функций.
const lightMix = () => {
const ctx = canvas2.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "lighter";
ctx.beginPath();
ctx.fillStyle = "rgb(255 0 0 / 100%)";
ctx.arc(100, 200, 100, Math.PI * 2, 0, false);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "rgb(0 0 255 / 100%)";
ctx.arc(220, 200, 100, Math.PI * 2, 0, false);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "rgb(0 255 0 / 100%)";
ctx.arc(160, 100, 100, Math.PI * 2, 0, false);
ctx.fill();
ctx.restore();
ctx.beginPath();
ctx.fillStyle = "#f00";
ctx.fillRect(0, 0, 30, 30);
ctx.fill();
};
const colorSphere = (element) => {
const ctx = canvas1.getContext("2d");
const width = 360;
const halfWidth = width / 2;
const rotate = (1 / 360) * Math.PI * 2; // per degree
const offset = 0; // scrollbar offset
const oLeft = -20;
const oTop = -20;
for (let n = 0; n <= 359; n++) {
const gradient = ctx.createLinearGradient(
oLeft + halfWidth,
oTop,
oLeft + halfWidth,
oTop + halfWidth,
);
const color = Color.HSV_RGB({ H: (n + 300) % 360, S: 100, V: 100 });
gradient.addColorStop(0, "rgb(0 0 0 / 0%)");
gradient.addColorStop(0.7, `rgb(${color.R} ${color.G} ${color.B} / 100%)`);
gradient.addColorStop(1, "rgb(255 255 255 / 100%)");
ctx.beginPath();
ctx.moveTo(oLeft + halfWidth, oTop);
ctx.lineTo(oLeft + halfWidth, oTop + halfWidth);
ctx.lineTo(oLeft + halfWidth + 6, oTop);
ctx.fillStyle = gradient;
ctx.fill();
ctx.translate(oLeft + halfWidth, oTop + halfWidth);
ctx.rotate(rotate);
ctx.translate(-(oLeft + halfWidth), -(oTop + halfWidth));
}
ctx.beginPath();
ctx.fillStyle = "#00f";
ctx.fillRect(15, 15, 30, 30);
ctx.fill();
return ctx.canvas;
};
// HSV (1978) = H: Hue / S: Saturation / V: Value
Color = {};
Color.HSV_RGB = (o) => {
const S = o.S / 100;
let H = o.H / 360,
V = o.V / 100;
let R, G;
let A, B, C, D;
if (S === 0) {
R = G = B = Math.round(V * 255);
} else {
if (H >= 1) H = 0;
H *= 6;
D = H - Math.floor(H);
A = Math.round(255 * V * (1 - S));
B = Math.round(255 * V * (1 - S * D));
C = Math.round(255 * V * (1 - S * (1 - D)));
V = Math.round(255 * V);
switch (Math.floor(H)) {
case 0:
R = V;
G = C;
B = A;
break;
case 1:
R = B;
G = V;
B = A;
break;
case 2:
R = A;
G = V;
B = C;
break;
case 3:
R = A;
G = B;
B = V;
break;
case 4:
R = C;
G = A;
B = V;
break;
case 5:
R = V;
G = A;
B = B;
break;
}
}
return { R, G, B };
};
const createInterlace = (size, color1, color2) => {
const proto = document.createElement("canvas").getContext("2d");
proto.canvas.width = size * 2;
proto.canvas.height = size * 2;
proto.fillStyle = color1; // top-left
proto.fillRect(0, 0, size, size);
proto.fillStyle = color2; // top-right
proto.fillRect(size, 0, size, size);
proto.fillStyle = color2; // bottom-left
proto.fillRect(0, size, size, size);
proto.fillStyle = color1; // bottom-right
proto.fillRect(size, size, size, size);
const pattern = proto.createPattern(proto.canvas, "repeat");
pattern.data = proto.canvas.toDataURL();
return pattern;
};
const op_8x8 = createInterlace(8, "#FFF", "#eee");
Результат
Спецификации
| Спецификация |
|---|
| HTML # dom-context-2d-globalcompositeoperation-dev |
Совместимость с браузерами
| Рабочий стол | Мобильные устройства | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox для Android | Opera Android | Safari в iOS | Samsung Internet | WebView Android | |
globalCompositeOperation |
1 | 12 | 1.5 | 9 | 2 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 |
См. также
- Интерфейс, определяющий это свойство:
CanvasRenderingContext2D CanvasRenderingContext2D.globalAlpha
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation