免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1357 | 回复: 0
打印 上一主题 下一主题

canvas教程 (六) 颜色渐变 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:54 |只看该作者 |倒序浏览
除了 CSS 颜色, fillStyle和 strokeStyle 属性可以设置为 CanvasGradient 对象。——通过 CanvasGradient可以为线条和填充使用颜色渐变。欲创建 CanvasGradient对象,可以使用两个方法:createLinearGradient 和 createRadialGradient。前者创建线性颜色渐变,后者创建圆形颜色渐变。创建颜色渐变对象后,可以使用对象的 addColorStop方法添加颜色中间值。下面的代码演示了颜色渐变使用方法:
复制内容到剪贴板

  • // You need to provide the source 和 destination (x,y) coordinates     
  • // for the gradient (from where it starts 和 where it ends).   
  • var gradient1 = context.createLinearGradient(sx, sy, dx, dy);   
  • // Now you can add colors in your gradient.   
  • // The first argument tells the position for the color in your gradient. The     
  • // accepted value range is from 0 (gradient start) to 1 (gradient end).   
  • // The second argument tells the color you want, using the CSS color format.   
  • gradient1.addColorStop(0,   ’#f00′); // red   
  • gradient1.addColorStop(0.5, ‘#ff0′); // yellow   
  • gradient1.addColorStop(1,   ’#00f’); // blue   
  • // For the radial gradient you also need to provide source   
  • // 和 destination circle radius.   
  • // The (x,y) coordinates define the circle center points (start 和     
  • // destination).   
  • var gradient2 = context.createRadialGradient(sx, sy, sr, dx, dy, dr);   
  • // Adding colors to a radial gradient is the same as adding colors to linear     
  • // gradients.   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP