关键词搜索

源码搜索 ×
×

canvas笔记-globalAlpha和globaleCompositeOperation的使用

发布2020-06-03浏览1185次

详情内容

如下代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. for(let i = 0; i < 100; i++){
  18. let R = Math.floor(Math.random() * 255);
  19. let G = Math.floor(Math.random() * 255);
  20. let B = Math.floor(Math.random() * 255);
  21. context.fillStyle = "rgb(" + R + "," + G + "," + B + ")";
  22. context.beginPath();
  23. context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, 2 * Math.PI);
  24. context.fill();
  25. }
  26. }
  27. </script>
  28. </body>
  29. </html>

程序运行截图如下:

修改脚本添加globalAlpha属性后,代码如下:

  1. <script>
  2. window.onload = function(){
  3. let canvas = document.getElementById("canvas");
  4. canvas.width = 800;
  5. canvas.height = 800;
  6. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  7. context.globalAlpha = 0.5;
  8. for(let i = 0; i < 100; i++){
  9. let R = Math.floor(Math.random() * 255);
  10. let G = Math.floor(Math.random() * 255);
  11. let B = Math.floor(Math.random() * 255);
  12. context.fillStyle = "rgb(" + R + "," + G + "," + B + ")";
  13. context.beginPath();
  14. context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, 2 * Math.PI);
  15. context.fill();
  16. }
  17. }
  18. </script>

程序运行截图如下:

 

globalAlpha会将所有元素的透明的进行更改。

 

 

下一个是globalCompositeOperation顾名思义就是整体图形复合时的操作,保证哪个在上面,哪个在下面,哪个需要裁剪,哪个要隐藏等等。

其属性值如下:

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

所有属性值对应的效果图是这样的。

下面来一个简单的小栗子

程序运行截图如下:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.globalCompositeOperation = "source-over";
  18. context.fillStyle = "red";
  19. context.fillRect(20, 20, 75, 50);
  20. context.fillStyle = "blue";
  21. context.fillRect(50, 50, 75, 50);
  22. context.globalCompositeOperation = "destination-over";
  23. context.fillStyle = "red";
  24. context.fillRect(150, 20, 75, 50);
  25. context.fillStyle = "blue";
  26. context.fillRect(180, 50, 75, 50);
  27. }
  28. </script>
  29. </body>
  30. </html>

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载