Chinaunix首页 | 论坛 | 博客
  • 博客访问: 378716
  • 博文数量: 466
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-16 13:59
文章分类

全部博文(466)

文章存档

2015年(466)

我的朋友

分类: 系统运维

2015-03-16 14:16:07

除了 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.   
阅读(287) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~