English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML リファレンスマニュアル

HTML タグ一覧

HTML canvas globalCompositeOperation 属性

globalCompositeOperation 属性は、新しい形状を描画する際に適用する合成操作の種類を設定します。typeは、使用する合成またはミックスモード操作を識別する文字列です。

HTML canvas 参考マニュアル

在线示例

異なる globalCompositeOperation 値を使用して矩形を描画しています。赤色の矩形は目标图像、青色の矩形は源图像

部分会显示,-上显示
out-上显示
あなたのブラウザは HTML をサポートしていません5 canvasタグ.

JavaScript:

!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML canvas globalCompositeOperation属性使用-基础教程(oldtoolbag.com)</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3>
あなたのブラウザは HTML をサポートしていません5 canvas タグ。
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.fillStyle="blue";    
ctx.globalCompositeOperation="source-over";
ctx.fillRect(50,50,75,50);    
ctx.fillStyle="red";
ctx.fillRect(150,20,75,50);
ctx.fillStyle="blue";    
ctx.globalCompositeOperation="destination-over";
ctx.fillRect(180,50,75,50);    
</script>
</body>
</html>
テストをしてみる ‹/›

ブラウザの互換性

IEFirefoxOperaChromeSafari

Internet Explorer 9Firefox、Opera、Chrome および Safari が globalCompositeOperation 属性をサポートしています。

注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。

定义和用法

globalCompositeOperation 属性设置或返回如何将一个源(新的)图像绘制到目标(已有的)的图像上。

源图像 = 您打算放置到画布上的绘图。

您已经放置在画布上的绘图。 默认值:

JavaScript 语法:部分会显示,-上显示
context.globalCompositeOperation="sourcein";-属性值

描述默认。在
部分会显示,-上显示source目标图像atop源图像
部分会显示,-顶部显示之外显示目标图像位于源图像源图像之外的部分是不可见的。目标图像in
部分会显示,-中显示之外显示目标图像之内的源图像之外的目标图像destination源图像over目标图像lighter
部分会显示,-在之外显示目标图像。只有源图像之外的目标图像部分会被显示,源图像over目标图像lighter
out-上显示之外显示源图像atop目标图像
out-顶部显示之外显示源图像位于目标图像目标图像之外的部分是不可见的。源图像in
out-中显示之外显示源图像之内的目标图像之外的源图像destination目标图像是透明的。源图像lighter
out-在之外显示源图像。只有目标图像之外的源图像部分会被显示,目标图像是透明的。源图像lighter
copy忽略源图像 + 目标图像
显示忽略源图像目标图像
xor使用异或操作对源图像目标图像进行组合。

在线示例

所有 globalCompositeOperation 属性值:

!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML canvas globalCompositeOperation属性使用-基础教程(oldtoolbag.com)</title>
<style>
canvas
{
    border:1px solid #d3d3d3;
    margin-right:10px;
    margin-bottom:20px;    
}
</style>
</head>
<body>
<script>
var gco=new Array();
gco.push("source-atop");
gco.push("source-in");
gco.push("source-out");
gco.push("source-over");
gco.push("destination-atop");
gco.push("destination-in");
gco.push("destination-out");
gco.push("destination-over");
gco.push("lighter");
gco.push("copy");
gco.push("xor");
for (n=0;n<gco.length;n++)
{
    document.write("<div id='p_" + n + "' style='float:left;'>" + gco[n] + ":<br>");
    var c=document.createElement("canvas");
    c.width=120;
    c.height=100;
    document.getElementById("p_" + n).appendChild(c);
    var ctx=c.getContext("2d");    
    ctx.fillStyle="blue";
    ctx.fillRect(10,10,50,50);
    ctx.globalCompositeOperation=gco[n];
    ctx.beginPath();
    ctx.fillStyle="red";
    ctx.arc(50,50,30,0,2*Math.PI);
    ctx.fill();
    document.write("</div>    
}
</script>
</body>
</html>
テストをしてみる ‹/›

HTML canvas 参考マニュアル