前面的话

  前面分别介绍了canvas的基础用法和进阶用法,本文将使用canvas的各种语法进行图形绘制

绘制线条

【绘制线条】

  下面来尝试绘制一段线条

<canvas id="drawing" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');//开始绘制
    context.beginPath();//将光标移动到(10,10)位置
    context.moveTo(10,10);//从(10,10)点开始绘制一条直线,到(100,100)为止
    context.lineTo(100,100);//线条宽度为5
    context.lineWidth = 5;//线条颜色为浅绿
    context.strokeStyle = "lightgreen";//绘制线条
    context.stroke();
}
</script>

【绘制折线】

  下面,更进一步,绘制多条折线

<canvas id="drawing" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');context.beginPath();context.moveTo(10,10);context.lineTo(50,50);context.lineTo(10,100);context.lineWidth = 5;context.strokeStyle = "lightgreen";context.stroke();context.beginPath();context.moveTo(60,10);context.lineTo(100,50);context.lineTo(60,100);context.lineWidth = 5;context.strokeStyle = "lightblue";context.stroke();context.beginPath();context.moveTo(110,10);context.lineTo(150,50);context.lineTo(110,100);context.lineWidth = 5;context.strokeStyle = "pink";context.stroke();
}
</script>

【绘制闭合图形】

  下面绘制四条线条,组合成一个闭合图形

<canvas id="drawing" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');context.beginPath();context.moveTo(10,10);context.lineTo(110,10);context.lineTo(110,110);context.lineTo(10,110);context.lineTo(10,10);context.lineWidth = 10;context.strokeStyle = "lightgreen";context.stroke();
}
</script>

  结果如下所示,最后一笔闭合的时候有问题,导致左上角有一个缺口。这种情况是设置了lineWidth导致的。如果默认1笔触的话,是没有问题的。但是笔触越大,线条越宽

  这时,需要使用clothPath()来闭合图形,而最后一笔可以不画出来

<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');context.beginPath();context.moveTo(10,10);context.lineTo(110,10);context.lineTo(110,110);context.lineTo(10,110);context.closePath();context.lineWidth = 10;context.strokeStyle = "lightgreen";context.stroke();
}
</script>

  当然,如果只是画矩形,使用rect()或fillRect()方法更简单

绘制矩形

  下面来绘制一个背景颜色为红色,尺寸为100*100,位置为(0,0)点的矩形

<canvas id="drawing" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');context.fillStyle = 'red';context.fillRect(0,0,100,100);
}
</script>

  下面来绘制一个半透明的蓝色描边矩形,尺寸为100*100,位置在(0,0)点

<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');context.strokeStyle = 'rgba(0,0,255,0.5)';context.strokeRect(0,0,100,100);}
</script>

  接下来,在(0,0)点绘制尺寸为100*100背景为半透明红色的矩形, 1s后在(50,50)点绘制尺寸为100*100,描边为半透明蓝色的矩形,1s后使用clearRect()清除矩形

<canvas id="drawing" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');context.fillStyle = 'rgba(255,0,0,0.5)';context.fillRect(0,0,100,100);setTimeout(function(){context.strokeStyle = 'rgba(0,0,255,0.5)';context.strokeRect(50,50,100,100);  },1000);setTimeout(function(){context.clearRect(0,0,300,150);},2000);
}
</script>

绘制弧形

【绘制圆】

<canvas id="canvas"><p>The canvas element is not supported!</p>
</canvas>
<script>
var canvas = document.getElementById('canvas');
if(canvas.getContext){canvas.width = 1000;canvas.height = 200;canvas.style.width = '400px';    var context = canvas.getContext('2d');context.lineWidth = 5;context.strokeStyle = '#058';for(var i = 0; i < 10; i++){context.beginPath();context.arc(50+i*100,60,40,0,2*Math.PI*(i+1)/10);
        context.closePath();context.stroke();}
}
</script>    

【绘制圆角矩形】

  圆角矩形的示意图如下所示

<canvas id="canvas" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
var canvas = document.getElementById('canvas');
if(canvas.getContext){var cxt = canvas.getContext('2d');var W = 300,H = 150;drawRoundRect(cxt,0,0,W,H,50); cxt.lineWidth = 10;cxt.stroke();       function drawRoundRect(cxt, x, y, w, h, r){  cxt.beginPath();//左上角
        cxt.arc(x+r,y+r,r,Math.PI,Math.PI*3/2);//上侧
        cxt.lineTo(x+w-r,y);//右上角
        cxt.arc(x+w-r,y+r,r,Math.PI*3/2,Math.PI*2);//右侧
        cxt.lineTo(x+w,y+h-r);//右下角
        cxt.arc(x+w-r,y+h-r,r,0,Math.PI/2);//下侧
        cxt.lineTo(x+r,y+h);//左下角
        cxt.arc(x+r,y+h-r,r,Math.PI/2,Math.PI);
        cxt.closePath();}
}
</script>   

【绘制弯月】

  下面是一轮弯月的计算示意图

  下面将上面的视图变成更通用的函数封装,代码如下

<canvas id="drawing" style="border:1px solid black"><p>The canvas element is not supported!</p>
</canvas>
<script>
if(drawing.getContext){var W = drawing.width = 200;var H = drawing.height = 200;var cxt = drawing.getContext('2d');function dis(x1,y1,x2,y2){return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));}function fillMoon(cxt,d,x,y,r,rot){cxt.save();cxt.translate(x,y);cxt.rotate(rot*Math.PI/180);
    cxt.scale(r,r);cxt.beginPath();cxt.arc(0,0,1,0.5*Math.PI,1.5*Math.PI,true);cxt.moveTo(0,-1);cxt.arcTo(d,0,0,1,dis(0,-1,d,0)/d);
    cxt.closePath();cxt.restore();}fillMoon(cxt,2,100,100,100,0)cxt.fillStyle = '#fb5';cxt.fill();
}
</script>

复杂图形

  下面基于线条、矩形和弧形,来绘制复杂图形

【绘制魔性图案】

  设置为正方形的魔性图案,当坐标位置x或y变化1px时,宽度或高度需要变化2px

  于是,得到下面代码

<canvas id="canvas" width=300 height=300 style="border: 1px solid #aaaaaa;"><p>The canvas element is not supported!</p>
</canvas>
<script>
var canvas = document.getElementById("canvas");
if(canvas.getContext){var context = canvas.getContext("2d");  for(var i=0; i<=20; i++){drawRect(context, 0 + 15 * i, 0 + 15 * i, 300 - 30 * i, 300 - 30 * i);}function drawRect(cxt,x,y,width,height){cxt.beginPath();cxt.rect(x, y, width, height);cxt.lineWidth = 5;cxt.strokeStyle = "blue";cxt.stroke();    }
}
</script>

【绘制五角星】

  五角星可分为大圆和小圆两部分。大圆控制外侧5个点的坐标位置,小圆控制内侧5个点的坐标位置。下面是详细的角度分析

<canvas id="canvas"><p>The canvas element is not supported!</p>
</canvas>
<script>
var canvas = document.getElementById('canvas');
if(canvas.getContext){var cxt = canvas.getContext('2d');var H = 100,W = 200;canvas.height = H;canvas.width = W;    function drawStar(cxt,r,R,x,y,rotate){if(rotate == undefined){rotate = 0;}cxt.beginPath();for(var i = 0; i < 5; i++){cxt.lineTo(Math.cos((18 + i*72 - rotate)/180*Math.PI)*R + x,-Math.sin((18+i*72 - rotate)/180 * Math.PI) * R + y);cxt.lineTo(Math.cos((54 + i*72 - rotate)/180*Math.PI)*r + x,-Math.sin((54+i*72 - rotate)/180 * Math.PI) * r + y);}cxt.closePath();cxt.stroke();}  drawStar(cxt,30,50,50,50);
}
</script>    

【绘制螺旋线】

  下面是制作螺旋线的示意图

  从圆心点开始,按照圆的公式向外移动,每次移动时,圆心角逐渐增大,半径逐渐增大

<canvas id="drawing" width="100" height="100"></canvas>
<script>
var drawing = document.getElementById('drawing');
if(drawing.getContext){var context = drawing.getContext('2d');var x = drawing.width/2;var y = drawing.height/2;var deg = 0;var r = 1;context.strokeStyle = 'red';context.lineWidth = 2;context.moveTo(x,y);for(var i = 0; i < 4800; i++){deg++;r+=0.01;context.lineTo(x+Math.cos(deg * Math.PI/180)*r,y+Math.sin(deg * Math.PI/180)*r);}context.stroke();
}
</script>

canvas图形绘制相关推荐

  1. Android Canvas 图形绘制

    在View对象上绘图 如果应用程序不需要大量的图形处理或很高的帧速率(如一个棋类游戏.Snake游戏或另外的慢动画类应用程序),那么就应该考虑创建一个定制的View组件,并且用该组件的View.onD ...

  2. 自定义控件之Canvas图形绘制基础练习-青春痘笑脸^_^

    对于自定义控件的意义不言而喻,所以对它的深入研究是很有必要的,前些年写过几篇关于UI效果的学习过程,但是中途比较懒一直就停滞了,而对于实际工作还是面试来说系统深入的了解自定义控件那是很有必要的,所以接 ...

  3. 小强的HTML5移动开发之路(6)——Canvas图形绘制基础

    在前面提到Canvas是HTML5中一个重要特点,canvas功能非常强大,用photoshop可以实现的效果canvas也同样可以实现,下面我们用canvas实现基本图形的绘制. 一.Canvas标 ...

  4. html绘制动态小人,JavaScript和html5 canvas如何绘制一个小人的代码

    这篇文章主要介绍了JavaScript+html5 canvas绘制的小人效果,涉及JavaScript结合html5 canvas图形绘制及颜色随机填充的技巧,需要的朋友可以参考下 本文实例讲述了J ...

  5. WebGL(三)——学习Canvas及简单图形绘制

    WebGL(三)--学习Canvas及简单图形绘制 Canvas简介 canvas是HTML5新增的一个可以使用javascript脚本在其中绘制图像的HTML元素(容器),它可以用来制作图像.动画, ...

  6. html5中Canvas、绘制线条模糊、常见绘制工具、绘制基本图形、绘制图片、面向对象的方式绘制图形图片、绘制文本、帧动画绘制

    Canvas容器: canvas标签用来定义图像的容器,必须配合脚本来绘制图像,canvas也运用于游戏开发.注意:canvas绘制图时会出现线条模糊情况,这是因为显示屏像素和canvas中定义的一个 ...

  7. 【HTML5】在canvas上绘制简单的2D图形 (+太极图)

    canvas(画布) canvas 元素是H5中新增的一个重要元素,专门用来绘制图形.在页面中放置一个canvas元素,就相当于在页面上放置一块画布,可以利用 canvas api 在其中进行图形的描 ...

  8. Android画布和图形绘制---Canvas and Drawables(一)

    注:本文译自:http://developer.android.com/guide/topics/graphics/2d-graphics.html Android框架API提供了一组2D描画API, ...

  9. [Web Chart系列之一(续)]Web端图形绘制SVG,VML, HTML5 Canvas 简单实例

    前言 本篇是继 [Web Chart系列之一]Web端图形绘制SVG,VML, HTML5 Canvas 技术比较 的补充和实例说明各种技术的使用方式. VML 的用法和实例 引入命名空间之后,就可以 ...

最新文章

  1. 2020年, SGG论文汇总
  2. WSCRIPT与CScript区别解释
  3. Caffe: Caffe的Python接口
  4. intellij中java文件都是灰色
  5. 【BZOJ2054】疯狂的馒头(并查集)
  6. 如果我使用Docker,是否需要OpenStack?
  7. matlab工具箱参数修改,使用matlab工具箱标定摄像头(内参数)
  8. 日志易——中国版的splunk
  9. 拼多多和酷家乐面试经历总结(已拿offer)
  10. 左神数据结构与算法(基础提升)——01
  11. ALFA机器视觉深度学习外观缺陷检测系统软件机器视觉
  12. 湖南出台不动产登记新规 “小产权房”不予办理
  13. 联想一体机用u盘装linux教程,联想一体机如何用u盘装系统
  14. php 合成图片,合成圆形图片
  15. 《电感元器件》的特性分析
  16. Android常用面试题大全
  17. 【pyhon】理想论坛爬虫1.05版,将读取和写DB分离成两个文件
  18. 线稿上色V3(比V2差别在于这个参考图的处理方式),并且更好用哦
  19. 根据PLL相噪测试曲线计算jitter的Matlab程序
  20. 转:电子邮件的工作原理

热门文章

  1. 在VB中如何让线程或进程在指定的CPU上运行
  2. 寻找优秀的AI公司与受人尊敬的资本助推者 | 量子位年度评选进行时
  3. 北京允许无人车上路后,Pony.ai正式广州开跑
  4. Mysql你应该要懂索引知识
  5. 业界领先的最新版本Oracle数据库现可部署在云端 以及Oracle Cloud at Customer和企业本地...
  6. ZREVRANK key member
  7. JavaSE基础:泛型
  8. 智能传感器产业三年行动指南
  9. 在 ML2 中配置 Vlan Network- 每天5分钟玩转 OpenStack(93)
  10. 整理一些质量不错的教程、博客、论坛