http://bbs.csdn.net/topics/391493648  canvas实例分享  2016-3-16

http://bbs.csdn.net/topics/390582151  html5 canvas 实现液体效果  2016-5-11

 CANVAS学习笔记,小函数整理:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
  7. <style type="text/css">
  8. </style>
  9. </head>
  10. <body>
  11. <h3>canvas学习笔记、小函数整理</h3>
  12. <div style="position:relative;width:500px;height:400px;margin:0px;padding:0px;border:1px solid #999;">
  13. <div style="position:absolute;padding:10px;background:rgba(128,128,128,0.5);bottom:0px;z-index:1">这里写字啦。。。。。。。</div>
  14. <canvas id="canvas" width="500" height="400" style="position:absolute;margin:5px;z-index:0"></canvas>
  15. </div>
  16.  

  17. <canvas id="canvas2" width="500" height="400" style="border:1px solid #999;padding:5px;"></canvas>
  18.  
  19. <script type="text/javascript">
  20. $(function(){
  21.  
  22. //画方形
  23. function fang_fill(id,color,x,y,width,height) {
  24. var canvas = document.getElementById(id)
  25. if (canvas == null){return false;}
  26. var context = canvas.getContext("2d");
  27. context.fillStyle = color; //"rgba(255,0,0,0.2)";
  28. context.fillRect(x,y,width,height);
  29. }
  30. //画方形框
  31. function fang_border(id,color,x,y,width,height,borderWidth) {
  32. var canvas = document.getElementById(id)
  33. if (canvas == null){return false;}
  34. var context = canvas.getContext("2d");
  35. context.lineWidth = 1;
  36. context.strokeStyle = color;
  37. context.strokeRect(x,y,width,height);
  38. }
  39.  
  40. //画圆形
  41. function circle_fill(id,color,x,y,r) {
  42. var canvas = document.getElementById(id)
  43. if (canvas == null){return false;}
  44. var context = canvas.getContext('2d');
  45. context.beginPath();
  46. context.arc(x,y,r,0,Math.PI * 2, true);
  47. context.closePath();
  48. context.fillStyle = color;
  49. context.fill();
  50.  
  51. }
  52. //画扇形(未写好)
  53. function fan_fill(id,color,x,y,r,jiao) {
  54. var canvas = document.getElementById(id)
  55. if (canvas == null){return false;}
  56. var context = canvas.getContext('2d');
  57. context.beginPath();
  58. context.lineTo(x,y);
  59. context.arc(x,y,r,0,Math.PI * jiao, true);
  60. context.lineTo(x,y);
  61. context.closePath();
  62. context.fillStyle = color;
  63. context.fill();
  64. }
  65.  
  66. //画五星
  67. function drawStar(id,color,x,y,r) {
  68. var canvas = document.getElementById(id)
  69. if (canvas == null){return false;}
  70. var context = canvas.getContext('2d');
  71. context.lineWidth = 5;
  72. context.beginPath();
  73. var dit = Math.PI * 4 / 5;
  74. var sin = Math.sin(0) * r + y;
  75. var cos = Math.cos(0) * r + x;
  76. console.log(0+":"+0);
  77. context.moveTo(cos, sin);
  78. for (var i = 0; i < 5; i++) {
  79. var tempDit = dit * i;
  80. sin = Math.sin(tempDit) * r + y;
  81. cos = Math.cos(tempDit) * r + x;
  82. context.lineTo(cos, sin);
  83. console.log(sin+":"+sin+":"+tempDit);
  84. }
  85. context.closePath();
  86. context.strokeStyle = "red";
  87. context.fillStyle = color;
  88. context.fill();
  89. }
  90.  
  91. //画渐变(y方向)
  92. function gradient_y(id,color1,color2,x,y,width,height){
  93. var canvas = document.getElementById(id)
  94. if (canvas == null){return false;}
  95. var context = canvas.getContext('2d');
  96. var g1 = context.createLinearGradient(x,y,x,(y+height));
  97. g1.addColorStop(0,color1);
  98. g1.addColorStop(1,color2);
  99. context.fillStyle = g1;
  100. context.fillRect(x,y,width,height);
  101. }
  102. //画渐变(x方向)
  103. function gradient_x(id,color1,color2,x,y,width,height){
  104. var canvas = document.getElementById(id)
  105. if (canvas == null){return false;}
  106. var context = canvas.getContext('2d');
  107. var g1 = context.createLinearGradient(x,y,(x+width),y);
  108. g1.addColorStop(0,color1);
  109. g1.addColorStop(1,color2);
  110. context.fillStyle = g1;
  111. context.scale(0.5, 0.5);
  112. context.rotate(Math.PI / 4);
  113. context.translate(100, 100);
  114. context.fillRect(x,y,width,height);
  115. }
  116.  
  117. //填充文字
  118. function font_fill(id,txt,x,y){
  119. var canvas = document.getElementById(id)
  120. if (canvas == null){return false;}
  121. var context = canvas.getContext("2d");
  122. context.fillStyle = "#00f";
  123. context.font = "40px 微软雅黑";
  124. //context.textBaseline = 'top';
  125. context.fillText(txt,x,y);
  126.  
  127. }
  128. //填充文字边框
  129. function font_border(id,txt,x,y){
  130. var canvas = document.getElementById(id)
  131. if (canvas == null){return false;}
  132. var context = canvas.getContext("2d");
  133. context.font = "100px 微软雅黑";
  134. length = context.measureText(txt);
  135. context.strokeText(txt,x,y);
  136.  
  137. }
  138.  
  139. //保存和恢复状态 (抄的)
  140. function draw18(id) {
  141. var canvas = document.getElementById(id)
  142. if (canvas == null){return false;}
  143. var context = canvas.getContext("2d");
  144. context.fillStyle = "red";
  145. context.save(); //保存了当前context的状态
  146. context.fillStyle = "black";
  147. context.fillRect(0, 0, 100, 100);
  148. context.restore();//恢复到刚刚保存的状态
  149. context.fillRect(0,120,100,100);
  150. }
  151. //保存文件 canvas.toDataURL(MIME) (抄的)
  152. function draw19(id) {
  153. var canvas = document.getElementById(id)
  154. if (canvas == null){return false;}
  155. var context = canvas.getContext("2d");
  156. context.fillStyle = "rgb(0,0,225)";
  157. context.fillRect(0, 0, canvas.width, canvas.height);
  158. context.fillStyle = "rgb(255,255,0)";
  159. context.fillRect(10, 20, 50, 50);
  160. //把图像保存到新的窗口
  161. var w=window.open(canvas.toDataURL("image/jpeg"),"smallwin","width=400,height=350");
  162. }
  163.  
  164. //function example(id,color1,color2,x,y,width,height){//
  165. //context.scale(0.5, 0.5); //缩放
  166. //context.rotate(Math.PI / 4); //旋转
  167. //context.translate(100, 100); //平移
  168. //context.fillRect(x,y,width,height);
  169. //}
  170.  
  171. //例子代码:
  172. //fang_fill("canvas","#f90",10,10,100,100);
  173. //fang_border("canvas","#f90",8,8,104,104);
  174. //circle_fill("canvas","#a9f",150,150,100);
  175. //fan_fill("canvas","#a9f",150,150,100,1/2);
  176. //drawStar("canvas","#f00",100,100,50);
  177. //gradient_y("canvas","#600","#f00",100,50,200,40);
  178. //gradient_y("canvas","#999","#eee",100,100,200,40);
  179. //gradient_x("canvas","#ff0","#f00",100,150,400,40);
  180. font_fill("canvas","啊啊啊啊啊啊啊啊",0,100);
  181. font_border("canvas","呃呃呃呃",120,130);
  182.  
  183. });
  184. </script>
  185. </body>
  186. </html>

 划线:

  1. //画线
  2. function line_stroke(id,color,x,y,end_x,end_y) {
  3. var canvas = document.getElementById(id)
  4. if (canvas == null){return false;}
  5. var context = canvas.getContext("2d");
  6. context.beginPath();//画一条新的线段
  7. context.strokeStyle = color;
  8. context.moveTo(x,y);
  9. context.lineTo(end_x,end_y);
  10. context.stroke();
  11. }
  12. line_stroke("canvas","#f90",10,10,100,100);

canvas学习笔记、小函数整理的更多相关文章

  1. Matlab学习笔记 figure函数

    Matlab学习笔记 figure函数 matlab中的 figure 命令,能够创建一个用来显示图形输出的一个窗口对象.每一个这样的窗口都有一些属性,例如窗口的尺寸.位置,等等.下面一一介绍它们. ...

  2. matlab学习笔记 bsxfun函数

    matlab学习笔记 bsxfun函数 最近总是遇到 bsxfun这个函数,前几次因为无关紧要只是大概看了一下函数体去对比结果,今天再一次遇见了这个函数,想想还是有必要掌握的,遂查了些资料总结如下. ...

  3. matlab学习笔记13_1 函数返回值

    一起来学matlab-matlab学习笔记13函数 13_1 函数返回值 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://blog.csdn.net/qq_36556 ...

  4. 大数据 -- kafka学习笔记:知识点整理(部分转载)

    一 为什么需要消息系统 1.解耦 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险.许多 ...

  5. canvas学习笔记(下篇) -- canvas入门教程--保存状态/变形/旋转/缩放/矩阵变换/综合案例(星空/时钟/小球)

    [下篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  6. canvas学习笔记(上篇)-- canvas入门教程 -- canvas标签/方块/描边/路径/圆形/曲线

    [上篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  7. swift学习笔记2——函数、闭包

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  8. ASP.NET MVC5 及 EF6 学习笔记 - (目录整理)

    个人从传统的CS应用开发(WPF)开始转向BS架构应用开发: 先是采用了最容易上手也是最容易搞不清楚状况的WebForm方式入手:到后面就直接抛弃了服务器控件的开发方式,转而采用 普通页面+Ajax+ ...

  9. canvas学习笔记(中篇) -- canvas入门教程-- 颜色/透明度/渐变色/线宽/线条样式/虚线/文本/阴影/图片/像素处理

    [中篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

随机推荐

  1. PHPEmailer使用简介(以qq邮箱为例)

    1.从网上下载PHPEmailer: 2.确保PHP环境支持sockets扩展,还要开启openssl,如下图: 3.配置QQ邮箱 1.开启SMTP服务 2.验证密保 3.获取授权码(这个就是IMAP ...

  2. .NET Core Generic Host Windows服务部署使用Topshelf

    此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只能关注一下官方issue # ...

  3. 前端学习——jquery操作例子

    一.jquery和DOM函数的转换 . jquery转换成dom $(] . dom转换成jquery var i1=documen.getElementById('#i1')---------> ...

  4. Spring Data JPA Hibernate @QueryHints

    另一个实例: http://leobluewing.iteye.com/blog/2032396 : 本文内容来源:https://blog.csdn.net/gavinchen1985/articl ...

  5. Anaconda+Tensorflow环境安装与配置

    转载请注明出处:http://www.cnblogs.com/willnote/p/6746499.html Anaconda安装 在清华大学 TUNA 镜像源选择对应的操作系统与所需的Python版 ...

  6. JUC集合之 ConcurrentLinkedQueue

    ConcurrentLinkedQueue介绍 ConcurrentLinkedQueue是线程安全的队列,它适用于"高并发"的场景. 它是一个基于链接节点的无界线程安全队列,按照 ...

  7. java 的关键字 native

    native native 关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中.Java语言本身不能对操作系统底层进行访问和操作,但是可 ...

  8. jquery select radio

    Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...});   //为S ...

  9. Flask视图函数与普通函数的区别,响应对象Response

    视图函数与普通函数看似没什么区别,其实他们的返回值上有着很大的区别. from flask import Flask app = Flask(__name__) @app.route('/hello' ...

  10. Extjs 分页传参方法

    第一种(常用): var proxy = new Ext.data.HttpProxy({url : url}) var store = new Ext.data.Store({ pruneModif ...