<feComposite>
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2018年10月以降、すべてのブラウザーで利用可能です。
<feComposite> は SVG のフィルタープリミティブで、Porter-Duff 合成演算(over、in、atop、out、xor、lighter、arithmetic)のいずれかを使用して、画像空間において 2 つの入力画像をピクセル単位で合成します。
他のフィルタープリミティブと同様に、デフォルトで linearRGB 色空間の色成分を処理します。color-interpolation-filters を使用することで、代わりに sRGB を使用することもできます。
下記の表では、MDN のロゴに赤い円をつけて合成した画像を用いて、それぞれの演算を示しています。
| 演算 | 説明 |
|---|---|
|
over |
in 属性で定義された元のグラフィック(MDN のロゴ)が、in2 属性で定義された宛先グラフィック(円)の上に配置されます。
これは デフォルトの演算であり、演算が指定されていない場合や、対応していない演算が指定された場合に使用されます。 |
|
in |
in 属性で定義された元のグラフィックの部分のうち、in2属性で定義された宛先グラフィックと重なる部分は、その宛先グラフィックを置き換えます。
|
|
out |
in 属性で定義された元のグラフィックの部分のうち、in2 属性で定義された宛先グラフィックからはみ出している部分を表示させます。
|
|
atop |
in 属性で定義された元のグラフィックのうち、in2 属性で定義された宛先グラフィックと重なる部分は、宛先グラフィックに置き換えます。元のグラフィックと重ならない宛先グラフィックの部分は、変更されません。
|
|
xor |
in 属性で定義された元のグラフィックと、in2 属性で定義された宛先グラフィックとの間で、重なり合わない領域が結合されます。
|
|
lighter |
in 属性で定義されたソースグラフィックと、in2 属性で定義された出力先グラフィックの合計が表示されます。
|
|
arithmetic
|
result = k1*i1*i2 + k2*i1 + k3*i2 + k4 ここで、 |
使用コンテキスト
属性
DOM インターフェイス
この要素は SVGFECompositeElement インターフェイスを実装しています。
例
この例では、対応する演算(over、atop、lighter など)ごとにフィルターを定義し、入力となる SourceGraphic と MDN ロゴの画像を合成しています。それぞれのフィルターは円要素に適用され、その円要素が SourceGraphic として使用されます。
メモ:
現行ブラウザーでは BackgroundImage を合成ソースとして使用できないため、フィルターの直下にたまたまあるピクセルをソースの一つとして合成を行うフィルターを定義することはできません。ここで採用されている手法は、BackgroundImage を使用できないことに対する回避策です。
SVG
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter id="imageOver">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite in2="SourceGraphic" operator="over" />
</filter>
<filter id="imageIn">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite in2="SourceGraphic" operator="in" />
</filter>
<filter id="imageOut">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite in2="SourceGraphic" operator="out" />
</filter>
<filter id="imageAtop">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite in2="SourceGraphic" operator="atop" />
</filter>
<filter id="imageXor">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite in2="SourceGraphic" operator="xor" />
</filter>
<filter id="imageArithmetic">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite
in2="SourceGraphic"
operator="arithmetic"
k1="0.1"
k2="0.2"
k3="0.3"
k4="0.4" />
</filter>
<filter id="imageLighter">
<feImage href="mdn_logo_only_color.png" x="10px" y="10px" width="160px" />
<feComposite in2="SourceGraphic" operator="lighter" />
</filter>
</defs>
<g transform="translate(0,25)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageOver)" />
<text x="80" y="-5">over</text>
</g>
<g transform="translate(200,25)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageIn)" />
<text x="80" y="-5">in</text>
</g>
<g transform="translate(400,25)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageOut)" />
<text x="80" y="-5">out</text>
</g>
<g transform="translate(600,25)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageAtop)" />
<text x="80" y="-5">atop</text>
</g>
<g transform="translate(0,240)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageXor)" />
<text x="80" y="-5">xor</text>
</g>
<g transform="translate(200,240)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageArithmetic)" />
<text x="70" y="-5">arithmetic</text>
</g>
<g transform="translate(400,240)">
<circle
cx="90px"
cy="80px"
r="70px"
fill="#cc0000"
filter="url(#imageLighter)" />
<text x="80" y="-5">lighter</text>
</g>
</svg>
結果
仕様書
| 仕様書 |
|---|
| Filter Effects Module Level 1> # feCompositeElement> |




