您现在的位置是:网站首页> 编程资料编程资料

HTML5 canvas基本绘图之图形组合HTML5 canvas基本绘图之绘制阴影效果HTML5 canvas基本绘图之文字渲染HTML5 canvas基本绘图之绘制曲线HTML5 canvas基本绘图之图形变换HTML5 canvas基本绘图之填充样式实现HTML5 canvas基本绘图之绘制线条HTML5 canvas基本绘图之绘制五角星HTML5 canvas基本绘图之绘制矩形HTML5 canvas基本绘图之绘制线段一波HTML5 Canvas基础绘图实例代码集合

2021-08-31 1023人已围观

简介 <canvas></canvas>是HTML5中新增的标签,用于绘制图形,这篇文章主要为大家详细介绍了HTML5 canvas基本绘图之图形组合方法,感兴趣的小伙伴们可以参考一下

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

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

图形组合:

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

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

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

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

XML/HTML Code复制内容到剪贴板
  1. >  
  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-overa>  
  31.         <a href="#">source-atopa>  
  32.         <a href="#">source-ina>  
  33.         <a href="#">source-outa>  
  34.         <a href="#">destination-overa>  
  35.         <a href="#">destination-atopa>  
  36.         <a href="#">destination-ina>  
  37.         <a href="#">destination-outa>  
  38.         <a href="#">lightera>  
  39.         <a href="#">copya>  
  40.         <a href="#">xora>  
  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.         co

相关内容

-六神源码网