HTML5-Canvas 初认识
1. 理解canvas
canvas其实是HTML5中一个新增加的标签,对于canvas标签本身并没有什么非常强大的属性(width、height、id、class、style),仅仅作为一个画布存在,只能使用js获取canvas绘图上下文环境(也就是获取CanvasRenderingContext2D对象啦)。下面用一段小程序来描述:
<canvas id="myCanvas" width="200" height="200" style="border:1px solid #000000;"></canvas>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d"); // 获取CanvasRenderingContext2D对象
对于上述的CanvasRenderingContext2D对象,其实才是具有绘图本领,它拥有HTML5绘图的API,所以canvas绘图仅仅是操作CanvasRenderingContext2D对象罢了。
2. 使用CanvasRenderingContext2D对象来画一条直线
// 绘制路径(并没有画,只是状态的设置)
ctx.moveTo(100,100); // 移动点到 100 , 100
ctx.lineTo(200,200); // 连接上一个点到 200 , 200 // 用线条根据状态中路径(进行绘图)
ctx.stroke();
因为canvas是基于状态的绘图,所以在绘制所有路径之前,还可以设置一些其他状态参数,比如直线还可以设置 lineWidth, strokeStyle 等属性。
ctx.lineWidth = 10; // 线条宽度
ctx.strokeStyle = 'blue'; // 线条颜色
ctx.moveTo(100,100);
ctx.lineTo(200,200);
ctx.stroke();
3. beginPath() 和 closePath() 的使用
由于canvas是基于状态的绘制,所以如果不指定beginPath(),每次调用绘制函数stroke都会将canvas所有的路径进行重新绘制,而达不到我们想要的效果,下面是一段beginPath()的使用的小程序:
ctx.lineWidth = 10; ctx.beginPath(); // 进行一次全新的绘制
ctx.moveTo(50,50); // == ctx.lineTo(50,50);
ctx.lineTo(100,100);
ctx.strokeStyle = 'red';
ctx.stroke(); ctx.beginPath(); // 进行一次全新的绘制
ctx.moveTo(60,60);
ctx.lineTo(120,120);
ctx.strokeStyle = 'green';
ctx.stroke(); ctx.beginPath(); // 进行一次全新的绘制
ctx.moveTo(70,70);
ctx.lineTo(140,140);
ctx.strokeStyle = 'blue';
ctx.stroke();
其实在canvas绘图中,最好的方式是将绘制路径的函数放在beginPath()和closePath()之间,beginPath表示进行一次全新的绘制,closePath表示当前绘制的图形应该封闭并且结束。它们成对的出现主要是来绘制一些封闭的图形。
ctx.lineWidth = 10; ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(100,100);
ctx.closePath(); // 封闭当前路径,并且结束
ctx.strokeStyle = 'red';
ctx.stroke(); ctx.beginPath();
ctx.moveTo(60,60);
ctx.lineTo(120,120);
ctx.closePath(); // 封闭当前路径,并且结束
ctx.strokeStyle = 'green';
ctx.stroke(); ctx.beginPath();
ctx.moveTo(70,70);
ctx.lineTo(140,140);
ctx.closePath(); // 封闭当前路径,并且结束
ctx.strokeStyle = 'blue';
ctx.stroke();
4. 填充一个封闭图形使用fill()和fillStyle
ctx.lineWidth = 10; ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(100,100);
ctx.closePath();
ctx.strokeStyle = 'red';
ctx.stroke(); ctx.beginPath();
ctx.moveTo(60,60);
ctx.lineTo(120,120);
ctx.closePath();
ctx.strokeStyle = 'green';
ctx.stroke(); ctx.beginPath();
ctx.moveTo(70,70);
ctx.lineTo(140,140);
ctx.closePath();
ctx.strokeStyle = 'blue';
ctx.stroke(); ctx.fillStyle = 'yellow'; // 填充颜色
ctx.fill(); // 填充绘制
上述程序其实是错误的,正确的程序应该是先填充绘制在进行stroke操作。
ctx.lineWidth = 10;
ctx.fillStyle = 'yellow'; ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(100,100);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = 'red';
ctx.stroke(); ctx.beginPath();
ctx.moveTo(60,60);
ctx.lineTo(120,120);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = 'green';
ctx.stroke(); ctx.beginPath();
ctx.moveTo(70,70);
ctx.lineTo(140,140);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = 'blue';
ctx.stroke();
5. 矩形
由于矩形是非常常用的一种图形,所以canvas提供一个api可以快速进行矩形的绘制(rect(x,y,width,height)函数)。
// 画一个矩形
// ctx.moveTo(x,y);
// ctx.lineTo(x+width,y);
// ctx.lineTo(x+width,y+height);
// ctx.lineTo(x,y+height);
ctx.rect(x,y,width,height); // 建立路径
其实还有更加方便的方法:fillRect(x,y,width,height) 和 strokeRect(x,y,width,height),这两个方法不但进行建立矩形路径还进行矩形的绘制
// ctx.beginPath();
// ctx.rect(x,y,width,height);
// ctx.closePath();
// ctx.stroke(); | ctx.fill();
ctx.strokeRect(x,y,width,height); | ctx.fillRect(x,y,width,height);
HTML5-Canvas 初认识的更多相关文章
- HTML5 程序设计 - 使用HTML5 Canvas API
请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...
- 赠书:HTML5 Canvas 2d 编程必读的两本经典
赠书:HTML5 Canvas 2d 编程必读的两本经典 这两年多一直在和HTML5 Canvas 打交道,也带领团队开发了世界首款基于HTML5 Canvas 的演示文档工具---AxeSlide( ...
- 如何开发一个简单的HTML5 Canvas 小游戏
原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...
- html5 canvas常用api总结(一)
1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...
- HTML5 Canvas绘制转盘抽奖
新项目:完整的Canvas转盘抽奖代码 https://github.com/givebest/GB-canvas-turntable 演示 http://blog.givebest.cn/GB-ca ...
- html5 canvas首屏自适应背景动画循环效果代码
模板描述:html5 canvas首屏自适应背景动画循环效果代码 由于动态图太大,怕以后服务器受不了,所以现在都改为静态图了,大家点击演示地址一样的,希望大家喜欢,你们的支持就是小海的动力!! 欢迎大 ...
- 自己写的HTML5 Canvas + Javascript五子棋
看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本.样式,帮助大众,成为受欢迎的人,感觉满羡慕的.我也想学会前端技术,变得受欢迎呀.于是心血来潮,开始学习前端知识,并写下了这 ...
- HTML5 Canvas彩色小球碰撞运动特效
脚本简介 HTML5 Canvas彩色小球碰撞运动特效是一款基于canvas加面向对象制作的运动小球动画特效. 效果展示 http://hovertree.com/texiao/html5/39/ ...
- 学习笔记:HTML5 Canvas绘制简单图形
HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...
- 基于HTML5 Canvas实现的图片马赛克模糊特效
效果请点击下面网址: http://hovertree.com/texiao/html5/1.htm 一.开门见山受美国肖像画家Chuck Close的启发,此脚本通过使用HTML5 canvas元素 ...
随机推荐
- linux制做RPM包
制作rpm包 1.制作流程 1.1 前期工作 1)创建打包用的目录rpmbuild/{BUILD,SPECS,RPMS, SOURCES,SRPMS} 建议使用普通用户,在用户家目录中创建 2)确定好 ...
- 【大数据系列】基于MapReduce的数据处理 SequenceFile序列化文件
为键值对提供持久的数据结构 1.txt纯文本格式,若干行记录 2.SequenceFile key-value格式,若干行记录,类似于map 3.编写写入和读取的文件 package com.slp; ...
- 【linux系列】centos安装vsftp
一.检查vsftpd软件 如果发现上不了网可以修改配置文件中的ONBOOT=no改为yes,然后重启服务试试
- smali-2.2.4.jar & baksmali-2.2.4.jar
https://bitbucket.org/JesusFreke/smali/downloads/
- 题目1040:Prime Number(第k个素数)
题目链接:http://ac.jobdu.com/problem.php?pid=1040 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- Capistrano 部署rails 应用
1 安装 gem install capistrano // For mutiple stages gem install capistrano-ext 2 准备 capify . 这个命令会创建Ca ...
- java代码中实现android背景选择的selector-StateListDrawable的应用
首先定义一个获得StateListDrawable对象的方法: private StateListDrawable addStateDrawable(Context context, int idNo ...
- SVG学习笔录(一)
SVG可缩放矢量图形(Scalable Vector Graphics)这项技术,现在越来越让大家熟知,在h5的移动端应用使用也越来越广泛了, 下面让我分享给大家svg学习的经验. HTML体系中,最 ...
- python开发环境搭建(python2.7.5+pyCharm2.7.3)【原创】
1.下载python 官网下载最新版python http://www.wingide.com/downloads 2.下载PyCharm 官网可下载最新版pyCharm-professional h ...
- OpenCV Create Circular Mask 圆形遮罩
在OpenCV中,比较常见的是矩形遮罩CvRect,没有专门提供圆形的mask,那么我们只能自己写一个来模拟圆形mask的函数,需要提供的参数为原图的大小,以及圆形mask的圆心位置和半径即可,返回一 ...