首页前端开发HTMLHTML5 canvas基本绘图之图形组合

HTML5 canvas基本绘图之图形组合

时间2024-01-24 19:26:26发布访客分类HTML浏览557
导读:收集整理的这篇文章主要介绍了HTML5 canvas基本绘图之图形组合,觉得挺不错的,现在分享给大家,也给大家做个参考。 <canvas></canvas>只是一个绘制图形的容器,除了id、class、st...
收集整理的这篇文章主要介绍了HTML5 canvas基本绘图之图形组合,觉得挺不错的,现在分享给大家,也给大家做个参考。

canvas> /canvas> 只是一个绘制图形的容器,除了id、class、style等属性外,还有height和width属性。在canvas> > 元素上绘图主要有三步:

1.获取canvas> 元素对应的DOM对象,这是一个Canvas对象;
2.调用Canvas对象的getContext()方法,得到一个CanvasRenderingContext2D对象;
3.调用CanvasRenderingContext2D对象进行绘图。

图形组合:

•globalAlpha: 设置或返回绘图的当前 alpha 或透明值

该方法主要是设置图形的透明度,这里就不具体介绍。

•globalComposITeoperation: 设置或返回新图像如何绘制到已有的图像上,该方法有以下属性值:

下面是一个小示例,可以通过点击改变组合效果:

XML/HTML Code复制内容到剪贴板
  1. !DOCTYPE html>   
  2. html lang="en">   
  3. head>   
  4.     meta charset="UTF-8">   
  5.     title> 图形组合/title>   
  6.     style type="text/css">   
  7.         #canvas{   
  8.             border: 1px solid #1C0EFA;   
  9.             display: block;   
  10.             margin: 20px auto;   
  11.         }   
  12.         #buttons{   
  13.             width: 1000px;   
  14.             margin: 5px auto;   
  15.             clear:both;   
  16.         }   
  17.         #buttons a{   
  18.             font-Size: 18px;   
  19.             display: block;   
  20.             float: left;   
  21.             margin-left: 20px;   
  22.         }   
  23.     /style>   
  24. /head>   
  25. body>   
  26.     canvas id="canvas" width="1000" height="800">   
  27.             你的浏览器还不支持canvas   
  28.     /canvas>   
  29.     div id="buttons">   
  30.         a href="#"> source-over/a>   
  31.         a href="#"> source-atop/a>   
  32.         a href="#"> source-in/a>   
  33.         a href="#"> source-out/a>   
  34.         a href="#"> destination-over/a>   
  35.         a href="#"> destination-atop/a>   
  36.         a href="#"> destination-in/a>   
  37.         a href="#"> destination-out/a>   
  38.         a href="#"> lighter/a>   
  39.         a href="#"> copy/a>   
  40.         a href="#"> xor/a>   
  41.     /div>   
  42. /body>   
  43. script type="text/javascript">   
  44.   
  45. window.onload = function(){   
  46.     draw("source-over");   
  47.   
  48.     VAR buttons = document.getElementById("buttons").getelementsbytagname("a");   
  49.     for (var i = 0;  i  buttons.length;  i++) {   
  50.         buttons[i].onclick = function(){   
  51.             draw(this.text);   
  52.             return false;   
  53.         } ;   
  54.     }   
  55. } ;   
  56.   
  57.     function draw(compositestyle){   
  58.         var canvas = document.getElementById("canvas");   
  59.         var context = canvas.getContext("2d");   
  60.   
  61.         context.clearRect(0, 0, canvas.width, canvas.height);   
  62.   
  63.         //draw title   
  64.         context.font = "bold 40px Arial";   
  65.         context.textAlign = "center";   
  66.         context.textBasedline = "middle";   
  67.         context.fillStyle = "#150E0E";   
  68.         context.fillText("globalCompositeOperation = "+compositeStyle, canvas.width/2, 60);   
  69.   
  70.         //draw a rect   
  71.         context.fillStyle = "#F6082A";   
  72.         context.fillRect(300, 150, 500, 500);   
  73.   
  74.         //draw a triangle   
  75.         context.globalCompositeOperation = compositeStyle;   
  76.         context.fillStyle = "#1611F5";   
  77.         context.beginPath();   
  78.         context.moveTo(700, 250);   
  79.         context.lineto(1000,750);   
  80.         context.lineTo(400, 750);   
  81.         context.closePath();   
  82.         context.fill();   
  83.     }   
  84.   
  85. /script>   
  86. /html>   

读者可以点击标签来观察不同的组合效果,效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

canvas

若转载请注明出处: HTML5 canvas基本绘图之图形组合
本文地址: https://pptw.com/jishu/585652.html
HTML5 canvas基本绘图之图形变换 使用HTML5里的classList操作CSS类

游客 回复需填写必要信息