本文内容与路径([js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解)是canvas中比较重要的概念。掌握理解他们是做出复杂canvas动画必要的基础之一.

再谈clip函数,这个函数在这篇文章[js高手之路] html5 canvas系列教程 - 图片操作(drawImage,clip,createPattern)已经有讲到过他的基本用法,我们来两个简单的例子复习一下:

 <meta charset='utf-8' />
<style>
#canvas,#canvas2{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' );
var oCanvas2 = document.querySelector( "#canvas2" ),
oGc2 = oCanvas2.getContext( '2d' ); oGc.beginPath();
oGc.strokeStyle = '#09f';
oGc.arc( 100, 100, 100, 0, 360 * Math.PI / 180, false );
oGc.stroke();
oGc.closePath(); oGc.clip(); oGc.beginPath();
oGc.fillStyle = 'red';
oGc.fillRect( 100, 100, 200, 100 );
oGc.closePath(); oGc2.beginPath();
oGc2.strokeStyle = '#09f';
oGc2.rect( 0, 0, 100, 100 );
oGc2.stroke();
oGc2.closePath(); oGc2.clip(); oGc2.beginPath();
oGc2.fillStyle = 'red';
oGc2.arc( 100, 100, 100, 0, 360 * Math.PI / 180, false );
oGc2.fill();
oGc2.closePath();
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
<canvas id="canvas2" width="500" height="400"></canvas>
</body>

请注意,如果用矩形作为裁剪区域,用使用rect,不能使用strokeRect和fillRect,即下面这段代码不能改成strokeRect或者fillRect

oGc2.beginPath();
oGc2.strokeStyle = '#09f';
oGc2.rect( 0, 0, 100, 100 );
oGc2.stroke();
oGc2.closePath();
如果想在已经裁剪的区域中再加载一张新的图片,怎么做?
 <meta charset='utf-8' />
<style>
#canvas{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
oBtn = document.querySelector( "input" ); oGc.beginPath();
oGc.strokeStyle = '#09f';
oGc.arc( 100, 100, 100, 0, 360 * Math.PI / 180, false );
oGc.stroke();
oGc.closePath(); oGc.clip();
function loadImg( imgPath ){
var oImg = new Image();
oImg.src = imgPath;
oImg.onload = function(){
oGc.drawImage( oImg, 0, 0 );
}
}
loadImg( './img/mv.jpg' );
oBtn.onclick = function(){
loadImg( './img/mv2.jpg' );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
<br/><input type="button" value="加载另一张图片">
</body>

当点击按钮的时候,加载一张新的图片,但是加载后的图片,也产生了裁剪效果.

如果,不需要保留裁剪效果怎么做呢?利用save方法保存最初的状态,再加载图片的使用,用restore来恢复

 <meta charset='utf-8' />
<style>
#canvas{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
oBtn = document.querySelector( "input" ); oGc.save(); //保存画布最初的状态,即没有产生裁剪效果的
oGc.beginPath();
oGc.strokeStyle = '#09f';
oGc.arc( 100, 100, 100, 0, 360 * Math.PI / 180, false );
oGc.stroke();
oGc.closePath(); oGc.clip();
function loadImg( imgPath ){
var oImg = new Image();
oImg.src = imgPath;
oImg.onload = function(){
oGc.drawImage( oImg, 0, 0 );
}
}
loadImg( './img/mv.jpg' );
oBtn.onclick = function(){
oGc.restore(); //恢复画布最初始的状态
loadImg( './img/mv2.jpg' );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
<br/><input type="button" value="加载另一张图片">
</body>

再次点击之后,就没有产生裁剪效果了

保存与恢复变形状态,如果一个形状产生多次平移效果,如果没有保存和恢复状态,那么平移相对的是他上一次变化后的状态

 <meta charset='utf-8' />
<style>
#canvas{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
oBtn = document.querySelector( "input" ); // oGc.save();
oGc.beginPath();
oGc.fillStyle = '#09f';
oGc.fillRect( 50, 50, 100, 100 );
oGc.translate( 100, 100 );
oGc.fillRect( 50, 50, 100, 100 );
oGc.closePath(); oGc.beginPath();
// oGc.restore();
oGc.fillStyle = 'red';
oGc.translate( 150, 150 );
oGc.fillRect( 50, 50, 100, 100 );
oGc.closePath();
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>

把save()和restore打开,红色的方块将是针对第一次绘制的蓝色方块平移,而不是针对平移后的状态平移【关于平移,后面会有文章,如果你有css3的基础。这个跟css3是一样的,就是相对原来的位置进行平移, 不过这里要注意一点,平移这个动作是写在渲染(fillRect)之前】

保存与恢复字体相关样式

 <meta charset='utf-8' />
<style>
#canvas{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
oBtn = document.querySelector( "input" ),
text = '跟着ghostwu学习html5 canvas'; oGc.font = 'bold 30px 微软雅黑';
oGc.fillStyle = '#09f';
// oGc.save();
oGc.fillText( text, 12, 60 ); oGc.fillStyle = 'red';
oGc.fillText( text, 12, 160 ); // oGc.restore();
oGc.fillText( text, 12, 260 );
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>

打开注释的save和restore状态之后,第三行文字就会应用到保存之前的状态(天蓝色:oGc.fillStyle = '#09f';)

[js高手之路] html5 canvas系列教程 - 状态详解(save与restore)的更多相关文章

  1. [js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

    接着上文[js高手之路] html5 canvas系列教程 - 状态详解(save与restore),相信大家都应该玩过美颜功能,而我们今天要讲的就是canvas强大的像素处理能力,通过像素处理,实现 ...

  2. [js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

    我们接着上文[js高手之路] html5 canvase系列教程 - 认识canvas以及基本使用方法继续. 一.直线的绘制 cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点 ...

  3. [js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)

    之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, ...

  4. [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)

    绘制曲线,经常会用到路径的知识,如果你对路径有疑问,可以参考我的这篇文章[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解. arc:画 ...

  5. [js高手之路] html5 canvas系列教程 - 图片操作(drawImage,clip,createPattern)

    接着上文[js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)继续,本文介绍的 ...

  6. [js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

    接着上文线条样式[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)继续. canvas提供两种输出文本的方 ...

  7. [js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

    上文,写完弧度与贝塞尔曲线[js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具),本文主要是关于线条的样式设置 lineWidth: 设置线条的宽 ...

  8. [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置

    接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...

  9. [js高手之路] html5 canvas动画教程 - 实时获取鼠标的当前坐标

    有了前面的canvas基础之后,现在开始就精彩了,后面写的canvas教程都是属于综合应用,前面已经写了常用的canvas基础知识,参考链接如下: [js高手之路] html5 canvas系列教程 ...

随机推荐

  1. Servlet中Response对象应用2(输出随机验证码图片)

    预期结果如图: 可用于登陆界面的验证 需要使用random类和绘画相关的几个类.以及imageio的内容. import java.awt.*; import java.awt.image.Buffe ...

  2. The C++ Programming Language 学习笔记 第5章 指针、数组和结构

    1.关于输出指向字符的指针的值. 现在定义,char c='a',char* pc=&c.在C中,输出该值只需要printf("%p\n",pc);而在C++中,如果cou ...

  3. codeforces 466d Increase Sequence

    D. Increase Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Redis[三] @Hash 哈希

    Redis的哈希值是字符串字段和字符串值之间的映射,所以他们是表示对象的完美数据类型 在Redis中的哈希值,可存储超过400十亿键值对. redis 提供了2套操纵 一种是批量 一种是非批量 假设需 ...

  5. C++中static关键字作用总结

    1.先来介绍它的第一条也是最重要的一条:隐藏.(static函数,static变量均可) 当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.举例来说明.同时编译两个源文件 ...

  6. js学习--变量作用域和作用域链

    作为一名菜鸟的我,每天学点的感觉还是不错的.今天学习闭包的过程中看到作用域与作用域链这两个概念,我觉得作为一名有追求的小白,有必要详细了解下. 变量的作用域 就js变量而言,有全局变量和局部变量.这里 ...

  7. CSS3弹性盒模型 display:box

    刚开始做网页时就有一个困惑,为什么display:block只能垂直排列,如果要水平排列就要使用float:left等方式.这种方法最难受的当然是当子元素的数量改变时,需要去修改子元素的宽度使重新适应 ...

  8. Vue项目搭建基础之Vue-cli模版测试

    第一步安装node,nodejs.org下载node稳定版安装包.node -v   (查看node版本)npm install -g vue-cli(安装Vue脚手架环境)vuevue listvu ...

  9. HTML5 贝塞尔绘画 桃心

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 201521123044 《Java程序设计》第8周学习总结

    1. 本章学习总结 2. 书面作业 本题作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结(见注释) public static List<String> conv ...