canvas快速绘制圆形、三角形、矩形、多边形方法
想看前面整理的canvas常用API的同学可以点狼蚁网站SEO优化
本系列文章涉及的所有代码都将上传至
从本篇文章开始,我会分享给大家canvas绘制的各种基础图形和酷炫的图形,注意是一系列!欢迎关注!
后续每篇文章我会着重分享给大家一些使用Canvas开发的实例和这些实例的实现思路。
本文看点使用canvas来绘制常见的各种图形实例,并且会简单封装一下绘制各图形的方法,会分享给大家一个封装好的快速绘制多边形的方法。
开始之前
//获取canvas容器 var can = document.getElementById('canvas'); //创建一个画布 var ctx = can.getContext('2d');
绘制圆形
var draw = function(x, y, r, start, end, color, type) { var unit = Math.PI / 180; ctx.beginPath(); ctx.arc(x, y, r, start unit, end unit); ctx[type + 'Style'] = color; ctx.closePath(); ctx[type](); }
参数解释x,y-圆心;start-起始角度;end-结束角度;color-绘制颜色;type-绘制类型('fill'和'stroke')。
实例如下图所示
绘制三角形
var draw = function(x1, y1, x2, y2, x3, y3, color, type) { ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.lineTo(x3, y3); ctx[type + 'Style'] = color; ctx.closePath(); ctx[type](); }
参数解释x1(2、3),y1(2、3)-三角形的三个点的坐标;color-绘制颜色;type-绘制类型('fill'和'stroke')。
实例如下图所示
绘制(圆角)矩形
var draw = function(x, y, width, height, radius, color, type){ ctx.beginPath(); ctx.moveTo(x, y+radius); ctx.lineTo(x, y+height-radius); ctx.quadraticCurveTo(x, y+height, x+radius, y+height); ctx.lineTo(x+width-radius, y+height); ctx.quadraticCurveTo(x+width, y+height, x+width, y+height-radius); ctx.lineTo(x+width, y+radius); ctx.quadraticCurveTo(x+width, y, x+width-radius, y); ctx.lineTo(x+radius, y); ctx.quadraticCurveTo(x, y, x, y+radius); ctx[type + 'Style'] = color || params.color; ctx.closePath(); ctx[type](); }
参数解释x,y-左上角点的坐标;width、height-宽高;radius-圆角;color-绘制颜色;type-绘制类型('fill'和'stroke')。
实例如下图所示
绘制多边形
var drawPolygon = function(ctx, conf){ var x = conf && conf.x || 0; //中心点x坐标 var y = conf && conf.y || 0; //中心点y坐标 var num = conf && conf.num || 3; //图形边的个数 var r = conf && conf.r || 100; //图形的半径 var width = conf && conf.width || 5; var strokeStyle = conf && conf.strokeStyle; var fillStyle = conf && conf.fillStyle; //开始路径 ctx.beginPath(); var startX = x + r Math.cos(2Math.PI0/num); var startY = y + r Math.sin(2Math.PI0/num); ctx.moveTo(startX, startY); for(var i = 1; i <= num; i++) { var newX = x + r Math.cos(2Math.PIi/num); var newY = y + r Math.sin(2Math.PIi/num); ctx.lineTo(newX, newY); } ctx.closePath(); //路径闭合 if(strokeStyle) { ctx.strokeStyle = strokeStyle; ctx.lineWidth = width; ctx.lineJoin = 'round'; ctx.stroke(); } if(fillStyle) { ctx.fillStyle = fillStyle; ctx.fill(); } }
参数说明
ctx: canvas画布
conf: 配置项,提供以下一些配置
- x 中心点横坐标
- y 中心点纵坐标
- num: 多边形的边数
- r多边形的半径长度
- width多边形线的宽度
- strokeStyle边线的颜色
- fillStyle填充的颜色
上图效果的代码如下
上图1的代码
drawPolygon(ctx, { num: 6, r: 100, strokeStyle: 'blue', fillStyle: '#9da' })
上图2的代码
drawPolygon(ctx, { num: 4, r: 150, strokeStyle: 'red', width: 4 })
上图3的代码
drawPolygon(ctx, { x: 800, y: 250, num: 10, fillStyle: '#000' })
我们一下,使用canvas绘制图形就是那几个函数beginPath、arc、moveTo、lineTo、closePath、fill、stroke。当我们能够熟练掌握并运用自如的时候,就能够独当一面了。加油吧,骚年们!
本文涉及的代码我已经上传至github,,喜欢的同学点个Star,多谢多谢~
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望多多支持狼蚁SEO!
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指