|
1 |
| -HTML `<canvas>` 标签的 globalCompositeOperation 属性 |
2 |
| -=== |
| 1 | +HTML canvas globalCompositeOperation 属性 |
| 2 | +=== |
| 3 | + |
| 4 | +## 示例 |
| 5 | + |
| 6 | +使用两个不同的 `globalCompositeOperation` 值绘制矩形。 红色矩形是目标图像。 蓝色矩形是源图像: |
| 7 | + |
| 8 | +```html idoc:preview:iframe |
| 9 | +<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">您的浏览器不支持 HTML5 canvas 标签。</canvas> |
| 10 | + |
| 11 | +<script> |
| 12 | + var c = document.getElementById("myCanvas"); |
| 13 | + var ctx = c.getContext("2d"); |
| 14 | + ctx.fillStyle = "red"; |
| 15 | + ctx.fillRect(20, 20, 75, 50); |
| 16 | + ctx.fillStyle = "blue"; |
| 17 | + ctx.globalCompositeOperation = "source-over"; |
| 18 | + ctx.fillRect(50, 50, 75, 50); |
| 19 | + ctx.fillStyle = "red"; |
| 20 | + ctx.fillRect(150, 20, 75, 50); |
| 21 | + ctx.fillStyle = "blue"; |
| 22 | + ctx.globalCompositeOperation = "destination-over"; |
| 23 | + ctx.fillRect(180, 50, 75, 50); |
| 24 | +</script> |
| 25 | +``` |
| 26 | + |
| 27 | +JavaScript: |
| 28 | + |
| 29 | +```js |
| 30 | +var c = document.getElementById("myCanvas"); |
| 31 | +var ctx = c.getContext("2d"); |
| 32 | + |
| 33 | +ctx.fillStyle = "red"; |
| 34 | +ctx.fillRect(20, 20, 75, 50); |
| 35 | +ctx.globalCompositeOperation = "source-over"; |
| 36 | +ctx.fillStyle = "blue"; |
| 37 | +ctx.fillRect(50, 50, 75, 50); |
| 38 | + |
| 39 | +ctx.fillStyle = "red"; |
| 40 | +ctx.fillRect(150, 20, 75, 50); |
| 41 | +ctx.globalCompositeOperation = "destination-over"; |
| 42 | +ctx.fillStyle = "blue"; |
| 43 | +ctx.fillRect(180, 50, 75, 50); |
| 44 | +``` |
| 45 | + |
| 46 | +## 浏览器支持 |
| 47 | + |
| 48 | +![chrome][1] ![edge][2] ![firefox][3] ![safari][4] ![opera][5] |
| 49 | + |
| 50 | +Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 globalCompositeOperation 属性。 |
| 51 | + |
| 52 | +## 定义和用法 |
| 53 | + |
| 54 | +`globalCompositeOperation` 属性设置或返回如何将源(新)图像绘制到目标(现有)图像上。 |
| 55 | + |
| 56 | +*源图像 =* 您将要放置到画布上的绘图。 |
| 57 | + |
| 58 | +*目标图像 =* 已经放置在画布上的绘图。 |
| 59 | + |
| 60 | +| 默认值: | `source-over` | |
| 61 | +| ------ | ------ | |
| 62 | +| JavaScript syntax: | *context*.globalCompositeOperation="source-in"; | |
| 63 | + |
| 64 | +## 属性值 |
| 65 | + |
| 66 | +| 值 Value | 描述 Description | |
| 67 | +| ------ | ------ | |
| 68 | +| source-over | 默认。在 **目标图像** 上显示 **源图像** | |
| 69 | +| source-atop | 在 **目标图像** 顶部显示 **源图像**。 **目标图像**之外的**源图像**部分未显示 | |
| 70 | +| source-in | 将 **源图像** 显示到 **目标图像**。仅显示**目标图像**内部的**源图像**部分,并且**目标图像**是透明的 | |
| 71 | +| source-out | 显示**目标图像**之外的**源图像**。仅显示**目标图像**之外的**源图像**部分,并且**目标图像**是透明的 | |
| 72 | +| destination-over | 在**源图像**上显示**目标图像** | |
| 73 | +| destination-atop | 在**源图像**之上显示**目标图像**。未显示 **源图像** 之外的 **目标图像** 部分 | |
| 74 | +| destination-in | 在**源图像**中显示**目标图像**。仅显示**源图像**内部的**目标图像**部分,并且**源图像**是透明的 | |
| 75 | +| destination-out | 显示**源图像**中的**目标图像**。仅显示 **目标图像** 在 **源图像** 之外的部分,并且 **源图像** 是透明的 | |
| 76 | +| lighter | 显示**源图像**+**目标图像** | |
| 77 | +| copy | 显示**源图像**。**目标图像**被忽略 | |
| 78 | +| xor | **源图像**与**目标图像**使用异或组合 | |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +## 更多示例 |
| 83 | + |
| 84 | +所有 globalCompositeOperation 属性值: |
| 85 | + |
| 86 | +```html idoc:preview:iframe |
| 87 | +<style> |
| 88 | +canvas { |
| 89 | + border: 1px solid #d3d3d3; |
| 90 | + margin-right: 10px; |
| 91 | + margin-bottom: 20px; |
| 92 | +} |
| 93 | +</style> |
| 94 | +<script> |
| 95 | +var gco = new Array(); |
| 96 | +gco.push("source-atop"); |
| 97 | +gco.push("source-in"); |
| 98 | +gco.push("source-out"); |
| 99 | +gco.push("source-over"); |
| 100 | +gco.push("destination-atop"); |
| 101 | +gco.push("destination-in"); |
| 102 | +gco.push("destination-out"); |
| 103 | +gco.push("destination-over"); |
| 104 | +gco.push("lighter"); |
| 105 | +gco.push("copy"); |
| 106 | +gco.push("xor"); |
| 107 | +
|
| 108 | +var n; |
| 109 | +for (n = 0; n < gco.length; n++) { |
| 110 | + document.write("<div id='p_" + n + "' style='float:left;'>" + gco[n] + ":<br>"); |
| 111 | + var c = document.createElement("canvas"); |
| 112 | + c.width = 120; |
| 113 | + c.height = 100; |
| 114 | + document.getElementById("p_" + n).appendChild(c); |
| 115 | + var ctx = c.getContext("2d"); |
| 116 | + ctx.fillStyle = "blue"; |
| 117 | + ctx.fillRect(10, 10, 50, 50); |
| 118 | + ctx.globalCompositeOperation = gco[n]; |
| 119 | + ctx.beginPath(); |
| 120 | + ctx.fillStyle = "red"; |
| 121 | + ctx.arc(50, 50, 30, 0, 2 * Math.PI); |
| 122 | + ctx.fill(); |
| 123 | + document.write("</div>"); |
| 124 | +} |
| 125 | +</script> |
| 126 | +``` |
| 127 | + |
| 128 | +[1]: ../assets/chrome.svg |
| 129 | +[2]: ../assets/edge.svg |
| 130 | +[3]: ../assets/firefox.svg |
| 131 | +[4]: ../assets/safari.svg |
| 132 | +[5]: ../assets/opera.svg |
0 commit comments