HTML5 Canvas 起步(3)-颜色与渐变

秋辰小哥_1615

秋辰小哥_1615

2016-05-17

1849人浏览

原创

Canvas 中的基本颜色系统

在 Canvas 中,颜色主要用途就是在绘制路径时,用来指定填充颜色和边框颜色。
Canvas 中的颜色参数值有两种格式:
1. 如果透明度为 1.0,也就是不透明,颜色值的格式就与一般使用一样,为:#AABBCC,其中 AA、BB、CC 分别为 Red、Green、Blue 分量。
2. 如果透明度不为 1.0,也就是带透明,颜色值格式可以使用 rgba(r, g, b, a),其中 r、g、b、a 分别为 Red、Green、Blue 分量和透明度。透明度的值为 0 至 1.0 之间的一个数值。
3. 颜色值还可以为已知的颜色名称,例如 red、blue、green 等。
总的说来,Canvas 中颜色值的格式与 CSS 中一致,因此颜色值没有特别需要注意的地方。


<script type="text/javascript"><br>&nbsp; &nbsp; var canvas = document.getElementById("canvas");<br>&nbsp; &nbsp; var ctx = canvas.getContext("2d");<br>&nbsp; &nbsp; // fill a rectangle whit sepecified fill style<br>&nbsp; &nbsp; <span class="t_tag" href="tag.php?name=function">function fillRect(color) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillStyle = color;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillRect(0, 0, 150, 100);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.font = "10pt Arial";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillStyle = "white";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillText(color, 6, 20);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.translate(160, 0);<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; ctx.translate(0, 50);<br>&nbsp; &nbsp; // fill rect with color name<br>&nbsp; &nbsp; fillRect("green");<br>&nbsp; &nbsp; // fill rect with 0xAABBCC<br>&nbsp; &nbsp; fillRect("#AABBCC");<br>&nbsp; &nbsp; // fill rect with rgba(50, 100, 150, 0.5)<br>&nbsp; &nbsp; ctx.translate(0, -25);<br>&nbsp; &nbsp; fillRect("rgba(50,100,150,0.5)");<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; // fill another rect with rgba(150, 100, 50, 0.5)<br>&nbsp; &nbsp; ctx.translate(-110, 50);<br>&nbsp; &nbsp; fillRect("rgba(150, 100, 50, 0.5)");<br></script>

注意,以上代码需要使用 Firefox 3.5 来查看,在 Firefox 3.0.x 中,Canvas 的 Context 对象不支持 fillText 方法,而我安装的 Chrome 2.0.174.0 对 translate 方法的实现有误。
2. Canvas 中的渐变
WHATWG 的 Canvas 规范中规划了两种渐变模式,一种是线性渐变,另一种是径向渐变。如果需要在 Canvas 中使用渐变,首先要根据你所要创建的渐变模式来调用 Context 的相应方法来创建一个渐变对象,这个对象就是用来控制渐变的效果。
2.1 线性渐变
线性渐变使用 Canvas Context 的 createLinearGradient 方法创建,它的定义如下:
CanvasGradient createLinearGradient(in float x0, in float y0, in float x1, in float y1);
通过 (x0, y0) 与 (x1, y1) 指定渐变的开始位置与截止位置。当然,创建完线性渐变对象并不能完全开始使用渐变对象,你需要给它指定渐变的起始色以及终止色。CanvasGradient 对象有一个 addColorStop 方法用来添加颜色。
gradient.addColorStop(offset, color)
在线性渐变中,偏移量 offset 只能取 0 或 1,分别代表线性渐变的起始色和终止色。color 参数的值可以参考上面说明的颜色格式。
例如,我要创建一个从 (20, 20) 到 (150, 150) 的一个渐变,从绿色渐变到白色透明,就可以按照下面的代码来创建 CanvasGradient 对象:

  • var gradient = ctx.createLinearGradient(20, 20, 150, 150);
  • gradient.addColorStop(0, 'green');
  • gradient.addColorStop(1, 'rgba(255,255,255,0)');

创建完渐变对象后,可以直接将它赋值给 CanvasContext 的 fillStyle 属性或 strokeStyle 属性,用来指定填充的渐变色或画边框的渐变色。也就是说,CanvasGradient 对象的用法等同于颜色参数值,通过赋值给 Context 对象来起作用。


<script type="text/javascript"><br>&nbsp; &nbsp; var canvas = document.getElementById("canvas");<br>&nbsp; &nbsp; var ctx = canvas.getContext("2d");<br>&nbsp; &nbsp; // fill a rectangle whit sepecified fill style<br>&nbsp; &nbsp; function fillRect(color, x, y, h, w) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillStyle = color;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillRect(x || 0, y || 0, h || 150, w || 100);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.font = "10pt Arial";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillStyle = "white";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillText(color, 6, 20);<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; ctx.translate(0, 50);<br>&nbsp; &nbsp; // fill rect with color name<br>&nbsp; &nbsp; fillRect("green");<br>&nbsp; &nbsp; // &#20174;&#32511;&#33394;&#28176;&#21464;&#21040;&#30333;&#33394;&#23436;&#20840;&#36879;&#26126;&#65292;&#24182;&#25351;&#23450;&#32473;&#22635;&#20805;&#33394;<br>&nbsp; &nbsp; var gradient = ctx.createLinearGradient(20, 20, 150, 150);<br>&nbsp; &nbsp; gradient.addColorStop(0, 'green');<br>&nbsp; &nbsp; gradient.addColorStop(1, 'rgba(255,255,255,0)');<br>&nbsp; &nbsp; ctx.translate(250, 0);<br>&nbsp; &nbsp; fillRect(gradient, -50, -50, 250, 200);<br>&nbsp; &nbsp; fillRect(gradient);<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; // &#20174;&#30333;&#33394;&#21040;&#40657;&#33394;&#30340;&#28176;&#21464;&#65292;&#24182;&#25351;&#23450;&#32473;&#36793;&#26694;&#33394;<br>&nbsp; &nbsp; gradient = ctx.createLinearGradient(0, 0, 150, 100);<br>&nbsp; &nbsp; gradient.addColorStop(0, 'white');<br>&nbsp; &nbsp; gradient.addColorStop(1, 'black');<br>&nbsp; &nbsp; ctx.strokeStyle = gradient;<br>&nbsp; &nbsp; ctx.strokeRect(0, 0, 150, 100);<br></script>
2.2 径向渐变
与线性的用法类似,它的函数签名如下:
CanvasGradient createRadialGradient(in float x0, in float y0, in float r0, in float x1, in float y1, in float r1);
与线性渐变不同的是,径向渐变除了要指定起始位置和终止位置外,还需要指定起始半径和终止半径。使用不同中心点的径向渐变可以实现类似光照的效果,不过目前 Chrome 对不同中心点的径向渐变支持不好,在 Chrome 中只会使用第二个中心点进行径向渐变,测试发现在最新开发版 Chrome 3.0.184.0 (17842) 中仍是如此。
在下图中可以看到,主流支持 Canvas 的浏览器都能正确渲染中心点不同的径向渐变,而 Chrome 则只能使用第二个中心点进行渲染。

<script type="text/javascript"><br>&nbsp; &nbsp; var canvas = document.getElementById("canvas");<br>&nbsp; &nbsp; var ctx = fixContext(canvas.getContext("2d"));<br>&nbsp; &nbsp; // fill a rectangle whit sepecified fill style<br>&nbsp; &nbsp; function fillRect(color, x, y, w, h) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillStyle = color;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillRect(x || 0, y || 0, w || 150, h || 100);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.font = "10pt Arial";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillStyle = "white";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ctx.fillText(color, 6, 20);<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; ctx.translate(50, 50);<br>&nbsp; &nbsp; // &#21516;&#20013;&#24515;&#65292;&#20174;&#32511;&#33394;&#28176;&#21464;&#21040;&#30333;&#33394;&#23436;&#20840;&#36879;&#26126;&#65292;&#24182;&#25351;&#23450;&#32473;&#22635;&#20805;&#33394;<br>&nbsp; &nbsp; var gradient = ctx.createRadialGradient(50, 50, 0, 50, 50, 100);<br>&nbsp; &nbsp; gradient.addColorStop(0, 'white');<br>&nbsp; &nbsp; gradient.addColorStop(1, 'green');<br>&nbsp; &nbsp; fillRect(gradient, -50, -50, 300, 200);<br>&nbsp; &nbsp; // &#31532;&#20108;&#27425;&#22635;&#20805;&#65292;&#30697;&#24418;&#21306;&#22495;&#39068;&#33394;&#21152;&#28145;<br>&nbsp; &nbsp; fillRect(gradient);<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; ctx.translate(250, 0);<br>&nbsp; &nbsp; // &#19981;&#21516;&#20013;&#24515;&#65292;&#20174;&#32511;&#33394;&#28176;&#21464;&#21040;&#30333;&#33394;&#23436;&#20840;&#36879;&#26126;&#65292;&#24182;&#25351;&#23450;&#32473;&#22635;&#20805;&#33394;<br>&nbsp; &nbsp; var gradient = ctx.createRadialGradient(0, 0, 10, 50, 50, 100);<br>&nbsp; &nbsp; gradient.addColorStop(0, 'white');<br>&nbsp; &nbsp; gradient.addColorStop(1, 'green');<br>&nbsp; &nbsp; fillRect(gradient, -50, -50, 200, 200);<br>&nbsp; &nbsp; // &#31532;&#20108;&#27425;&#22635;&#20805;&#65292;&#30697;&#24418;&#21306;&#22495;&#39068;&#33394;&#21152;&#28145;<br>&nbsp; &nbsp; fillRect(gradient);<br></script>

小结
总的来说,Canvas 中的颜色使用与使用 CSS 进行颜色定义没有什么区别,但比较强大的是 Canvas 支持渐变,这样就可以通过 Canvas 来做一些比较炫的效果。
这篇拖的太久了,已经有些忘了,先结束掉这部分再继续写了……

前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

html5

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

80

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

60

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

40

15

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

2026.11.06

140

12

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

2026.11.06

120

13

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

2026.11.06

120

10

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

100

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

80

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

100

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程