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 初认识的更多相关文章

  1. HTML5 程序设计 - 使用HTML5 Canvas API

    请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...

  2. 赠书:HTML5 Canvas 2d 编程必读的两本经典

    赠书:HTML5 Canvas 2d 编程必读的两本经典 这两年多一直在和HTML5 Canvas 打交道,也带领团队开发了世界首款基于HTML5 Canvas 的演示文档工具---AxeSlide( ...

  3. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  4. html5 canvas常用api总结(一)

    1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...

  5. HTML5 Canvas绘制转盘抽奖

    新项目:完整的Canvas转盘抽奖代码 https://github.com/givebest/GB-canvas-turntable 演示 http://blog.givebest.cn/GB-ca ...

  6. html5 canvas首屏自适应背景动画循环效果代码

    模板描述:html5 canvas首屏自适应背景动画循环效果代码 由于动态图太大,怕以后服务器受不了,所以现在都改为静态图了,大家点击演示地址一样的,希望大家喜欢,你们的支持就是小海的动力!! 欢迎大 ...

  7. 自己写的HTML5 Canvas + Javascript五子棋

    看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本.样式,帮助大众,成为受欢迎的人,感觉满羡慕的.我也想学会前端技术,变得受欢迎呀.于是心血来潮,开始学习前端知识,并写下了这 ...

  8. HTML5 Canvas彩色小球碰撞运动特效

    脚本简介 HTML5 Canvas彩色小球碰撞运动特效是一款基于canvas加面向对象制作的运动小球动画特效.   效果展示 http://hovertree.com/texiao/html5/39/ ...

  9. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

  10. 基于HTML5 Canvas实现的图片马赛克模糊特效

    效果请点击下面网址: http://hovertree.com/texiao/html5/1.htm 一.开门见山受美国肖像画家Chuck Close的启发,此脚本通过使用HTML5 canvas元素 ...

随机推荐

  1. 手写自己的ThreadLocal(线程局部变量)

    ThreadLocal对象通常用于防止对可变的单实例变量或全局变量进行共享. 精简版: public class MyThreadLocal<T> { private Map<Thr ...

  2. WEB中会话跟踪[转]

    今天晚上去华工参加睿智融科的笔试,问到web会话跟踪,一脸懵比,这个词听都没听过,回来后百度下,发现其实会话跟踪的内容我基本都了解的~_~ 转自:http://www.cnblogs.com/gaop ...

  3. 日记整理---->2016-11-25

    2017-03-02开始,记录的一些知识点.岁月长,三更漏.漫漫回廊,依稀人空瘦.借酒消愁入断肠,倚剑笑我,我独自寻殇. 一.vx中的v-bind和{{}}的区别 <td class=" ...

  4. find中的-print0和xargs中-0的奥妙

    原文地址:find中的-print0和xargs中-0的奥妙作者:改变自己 默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一 ...

  5. 【大数据系列】hadoop上传文件报错_COPYING_ could only be replicated to 0 nodes

    使用hadoop上传文件 hdfs dfs -put  XXX 17/12/08 17:00:39 WARN hdfs.DFSClient: DataStreamer Exception org.ap ...

  6. Linux(Ubuntu)下也能用搜狗输入法了!!!

    Ubuntu原生的中文输入法是不是总有点别扭? 不用再别扭了. 告诉你一个好消息:Linux(Ubuntu)下也能用搜狗输入法了!!! 下载地址:http://pinyin.sogou.com/lin ...

  7. Rails 添加新的运行环境

    Rails自带了development.test和production三个environments 我们可以添加Staging database.yml staging: adapter: mysql ...

  8. 圆形CD绘制 (扇形)

    参考: Egret教程Arc是使用示例:http://edn.egret.com/cn/article/index/id/673 我封装的工具类: /** * 圆形进度 * @author chenk ...

  9. [Log]ASP.NET之HttpModule 事件执行顺序

    ASP.Net下的HttpModule是基于事件的处理模型,这使得我们在选择事件监听和处理的时候有更多选择.下面是对HttpModule有关事件被触发的监测: 有关代码如下 using System; ...

  10. STS没有找到Dynamic Web Project

    解决:安装JavaEE插件 help-> install new software-> 选择sts对应的eclipse版本站点,如eclipse版本4.09选择2018-09.4.10选择 ...