首页前端开发HTML使用HTML5 Canvas绘制圆角矩形及相关的一些应用举例

使用HTML5 Canvas绘制圆角矩形及相关的一些应用举例

时间2024-01-24 17:51:25发布访客分类HTML浏览720
导读:收集整理的这篇文章主要介绍了使用HTML5 Canvas绘制圆角矩形及相关的一些应用举例,觉得挺不错的,现在分享给大家,也给大家做个参考。 圆角矩形是由四段线条和四个1/4圆弧组成,拆解如下。因为我们要写的是函数而不是一个固定的圆...
收集整理的这篇文章主要介绍了使用HTML5 Canvas绘制圆角矩形及相关的一些应用举例,觉得挺不错的,现在分享给大家,也给大家做个参考。

圆角矩形是由四段线条和四个1/4圆弧组成,拆解如下。

因为我们要写的是函数而不是一个固定的圆角矩形,所以这里列出的是函数需要的参数。分析好之后,直接敲出代码。

JavaScript Code复制内容到剪贴板
  1. !DOCTYPE htML>   
  2. html lang="zh">   
  3. head>   
  4.     meta charset="UTF-8">   
  5.     tITle> 圆角矩形/title>   
  6.     style>   
  7.         body {  background: url("./images/bg3.jpg") repeat;  }  
  8.         #canvas {  border: 1px solid #aaaaaa;  display: block;  margin: 50px auto;  }   
  9.     /style>   
  10. /head>   
  11. body>   
  12. div id="canvas-warp">   
  13.     canvas id="canvas">   
  14.         你的浏览器居然不支持Canvas?!赶快换一个吧!!   
  15.     /canvas>   
  16. /div>   
  17.   
  18. script>   
  19.     window.onload = function(){   
  20.         VAR canvas = document.getElementById("canvas");   
  21.         canvas.width = 800;   
  22.         canvas.height = 600;   
  23.         var context = canvas.getContext("2d");   
  24.         context.fillStyle = "#FFF";   
  25.         context.fillRect(0,0,800,600);   
  26.   
  27.         drawRoundRect(context, 200, 100, 400, 400, 50);   
  28.         context.strokeStyle = "#0078AA";   
  29.         context.stroke();   
  30.     }   
  31.   
  32.     function drawRoundRect(cxt, x, y, width, height, radius){   
  33.         cxt.beginPath();   
  34.         cxt.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2);   
  35.         cxt.lineto(width - radius + x, y);   
  36.         cxt.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2);   
  37.         cxt.lineTo(width + x, height + y - radius);   
  38.         cxt.arc(width - radius + x, height - radius + y, radius, 0, Math.PI * 1 / 2);   
  39.         cxt.lineTo(radius + x, height +y);   
  40.         cxt.arc(radius + x, height - radius + y, radius, Math.PI * 1 / 2, Math.PI);   
  41.         cxt.closePath();   
  42.     }   
  43. /script>   
  44. /body>   
  45. /html>   

运行结果:

建议大家自己动手绘制一个圆角矩形,这样有助于对路径的掌握。

下面我们用这个函数来做点其他的事情。

绘制2048游戏界面
对代码不做过多讲解,大家自己研究研究,建议自己动手先尝试写一下。因为我这里采用的是硬编码,所以不是很好,大家也可尝试优化一下。

JavaScript Code复制内容到剪贴板
  1. !DOCTYPE html>   
  2. html lang="zh">   
  3. head>   
  4.     meta charset="UTF-8">   
  5.     title> 2048游戏界面/title>   
  6.     style>   
  7.         body {  background: url("./images/bg3.jpg") repeat;  }  
  8.         #canvas {  border: 1px solid #aaaaaa;  display: block;  margin: 50px auto;  }   
  9.     /style>   
  10. /head>   
  11. body>   
  12. div id="canvas-warp">   
  13.     canvas id="canvas">   
  14.         你的浏览器居然不支持Canvas?!赶快换一个吧!!   
  15.     /canvas>   
  16. /div>   
  17.   
  18. script>   
  19.     window.onload = function(){   
  20.         var canvas = document.getElementById("canvas");   
  21.         canvas.width = 800;   
  22.         canvas.height = 600;   
  23.         var context = canvas.getContext("2d");   
  24.         context.fillStyle = "#FFF";   
  25.         context.fillRect(0,0,800,600);   
  26.   
  27.         drawRoundRect(context, 200, 100, 400, 400, 5);   
  28.         context.fillStyle = "#AA7B41";   
  29.         context.strokeStyle = "#0078AA";   
  30.         context.stroke();   
  31.         context.fill();   
  32.   
  33.         for(var i = 1;  i = 4;  i++){   
  34.             for(var j = 1;  j = 4;  j++){   
  35.                 drawRoundRect(context, 200 + 16 * i + 80 * (i - 1), 100 + 16 * j + 80 * (j - 1), 80, 80, 5);   
  36.                 context.fillStyle = "#CCBFB4";   
  37.                 context.strokeStyle = "#0078AA";   
  38.                 context.stroke();   
  39.                 context.fill();   
  40.             }   
  41.         }   
  42.     }   
  43.   
  44.     function drawRoundRect(cxt, x, y, width, height, radius){   
  45.         cxt.beginPath();   
  46.         cxt.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2);   
  47.         cxt.lineTo(width - radius + x, y);   
  48.         cxt.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2);   
  49.         cxt.lineTo(width + x, height + y - radius);   
  50.         cxt.arc(width - radius + x, height - radius + y, radius, 0, Math.PI * 1 / 2);   
  51.         cxt.lineTo(radius + x, height +y);   
  52.         cxt.arc(radius + x, height - radius + y, radius, Math.PI * 1 / 2, Math.PI);   
  53.         cxt.closePath();   
  54.     }   
  55. /script>   
  56. /body>   
  57. /html>   
  58.   

运行结果:

这个圆角矩形的函数写好之后,可以自己封装进JS文件里,以后遇到什么好的函数都可以放进去,这样积累下来,这个文件就是一套属于自己的图形库和游戏引擎了,是不是非常的酷?

其实游戏制作是Canvas的主要用途,但是要知道每一个游戏设计师都是一个艺术家。


绘制微信对话框
大家可以尝试着使用Canvas绘制一下微信聊天界面,作为练习与巩固。

这里使用到了绘制矩形,绘制圆角矩形,绘制多线条图形,填充颜色的一些知识。还有一些 Canvas文本API 我们并没有说到,所以大家只要能绘制出一个大概的界面就算合格了。能够绘制出来,也就基本掌握了Canvas API。

其实上述对话是生成出来的——“微信界面生成器网页版”,可谓是微商神器。是不是非常的酷?

这只是暑假花两天时间写的最初版本,还尚未达到发布的地步,在我写本节的时候,这个网页的界面还正在优化中。大家可以尝试自己动手做做,也可以关注和参考我的这个小项目github:微信界面生成器。本节就不再重复给出界面代码了。

好了,学到这里基本上已经学完了所有基本的Canvas绘图的api,大家拿起自己的画笔,自由的发挥吧!

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

上一篇: 详解通过HTML5 Canvas实现图片的...下一篇:使用HTML5 Canvas API绘制弧线的...猜你在找的html5相关文章 关于移动端h5开发相关内容总结2022-05-17html5教程-学表单的第二天2018-12-10html5教程-HTML5浏览器支持2018-12-10html5教程-HTML5智能表单2018-12-10html5教程-微信H5使用resLoader实现加载页效果2018-12-10html5教程-day01-初级-JS0-热身运动JS入门教程2018-12-10html5教程-html5shiv.js和respond.min.js2018-12-10html5教程-不同浏览器对于html5 audio标签和音频格式的兼容性2018-12-10html5教程-使用Html5实现手风琴案例2018-12-10html5教程-html5笔记2018-12-10 其他相关热搜词更多phpjavapython程序员load

若转载请注明出处: 使用HTML5 Canvas绘制圆角矩形及相关的一些应用举例
本文地址: https://pptw.com/jishu/585564.html
使用HTML5 Canvas API绘制弧线的教程 实例讲解利用HTML5 Canvas API操作图形旋转的方法

游客 回复需填写必要信息