当前位置:网站首页>《canvas》之第11章 canvas状态
《canvas》之第11章 canvas状态
2022-06-29 13:55:00 【yxqq378287007】
《canvas》之第11章 canvas状态
第11章 canvas状态
11.1 什么是状态
canvas基于状态(strokeStyle、fillStyle、lineWidth等)绘制(stroke()、fill())图形。
状态值改变时:
- 使用beginPath()开始新路径,使用新的状态值。
- 未使用beginPath()开始新路径,后面值覆盖前面值。
save()保存当前状态,restore()恢复之前保存的状态,一般成对使用。
11.2 clip()方法
基本图形绘制区域,然后clip()剪切,后面绘制图形超出剪切区域后,不会显示。
cxt.clip();
clip()不支持strokeRect()和fillRect(),使用rect()代替。
- 剪切圆
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); //绘制一个“描边圆”,圆心为(50,50),半径为40 cxt.beginPath(); cxt.arc(50, 50, 40, 0, 360 * Math.PI / 180, true); cxt.closePath(); //cxt.strokeStyle = "HotPink"; //cxt.stroke(); cxt.fillStyle = "HotPink"; cxt.fill(); //使用clip(),使得“描边圆”成为一个剪切区域 cxt.clip(); //绘制一个“填充矩形” cxt.beginPath(); cxt.fillStyle = "#66CCFF"; cxt.fillRect(50, 50, 100, 80); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
- 剪切矩形
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); //绘制一个“填充矩形” cxt.beginPath(); cxt.rect(20, 20, 100, 80); cxt.strokeStyle = " HotPink"; cxt.stroke(); //cxt.strokeRect(20, 20, 100, 80); //strokeRect()有bug cxt.clip(); //绘制一个“描边圆”,圆心为(120,60),半径为40 cxt.beginPath(); cxt.arc(120, 60, 40, 0, 360 * Math.PI / 180, true); cxt.closePath(); cxt.fillStyle = " #66CCFF"; cxt.fill(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
11.3 save()方法和restore()方法
适合场合:
- 图形或图片剪切。
- 图形或图片变形。
- 状态属性改变。
fillStyle、font、global Alpha、globalCompositeOperation、lineCap、lineJoin、lineWidth、miterLimit、shadowBlur、shadowColor、shadowOffsetX、shadowOffsetY、strokeStyle、textAlign、textBaseLine。
11.3.1 图形或图片剪切
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); //save()保存状态 cxt.save(); //使用clip()方法指定一个圆形的剪切区域 cxt.beginPath(); cxt.arc(70, 70, 50, 0, 360 * Math.PI / 180, true); cxt.closePath(); cxt.stroke(); cxt.clip(); //绘制一张图片 var image = new Image(); image.src = "images/princess.png"; image.onload = function () {
cxt.drawImage(image, 10, 20); } $$("btn").onclick = function () {
//restore()恢复状态 cxt.restore(); //清空画布 cxt.clearRect(0, 0, cnv.width, cnv.height); //绘制一张新图片 var image = new Image(); image.src = "images/flower.png"; image.onload = function () {
cxt.drawImage(image, 10, 20); } } } </script>
</head>
<body>
<canvas id="canvas" width="200" height="160" style="border:1px dashed gray;"></canvas><br />
<input id="btn" type="button" value="绘制新图" />
</body>
</html>
11.3.2 图形或图片变形
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); cxt.save(); cxt.translate(30, 30); cxt.fillStyle = "HotPink"; cxt.fillRect(0, 0, 100, 50); cxt.restore(); cxt.translate(60, 60); cxt.fillStyle = "LightSkyBlue"; cxt.fillRect(0, 0, 100, 50); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray"></canvas>
</body>
</html>
11.3.3 状态属性的改变
- 填充属性:fillStyle。
- 描边属性:strokeStyle。
- 线条效果:lineCap、lineJoin、lineWidth、miterLimit。
- 文本效果:font、textAlign、textBaseline。
- 阴影效果:shadowBlur、shadowColor、shadowOffsetX、shadowOffsetY。
- 全局属性:globalAlpha、globalCompositeOperation。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript"> function $$(id) {
return document.getElementById(id); } window.onload = function () {
var cnv = $$("canvas"); var cxt = cnv.getContext("2d"); var text = "绿叶学习网"; cxt.font = "bold 20px 微软雅黑"; cxt.fillStyle = "HotPink"; cxt.save(); //save()保存状态 cxt.fillText(text, 50, 40); cxt.fillStyle = "LightSkyBlue "; cxt.fillText(text, 50, 80); cxt.restore(); //restore()恢复状态 cxt.fillText(text, 50, 120); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray"></canvas>
</body>
</html>
边栏推荐
- Intuition and Implementation: batch normalization
- Shell——文本处理命令
- HTAP X 云原生: TiDB 加速释放数据价值,实现数据敏捷
- Stable currency risk profile: are usdt and usdc safe?
- 一次mysql的.ibd文件过大处理过程记录
- 关于MongoDB报错:connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
- JUC multithreading: creation and working principle of thread pool
- Thinkpad VMware 安装虚拟机出现此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态(问题解决方法)
- Wechat applet: Halloween avatar box generation tool
- Introduction to veeambackup & replication
猜你喜欢

文物数字藏品,开启文化传承的新方式

Are you still reading the log by command? Use kibana, one picture is better than ten thousand lines of log

TikTok全球短视频霸主地位或被YouTube反超

纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”

Industry analysis - quick intercom, building intercom

单端口RAM实现FIFO

疯狂的数字藏品,下一个造富神话?

直觉与实现:Batch Normalization

分布式唯一 ID 生成方案浅谈

mysql多表查询
随机推荐
STM32 watchdog study
灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs
How goby exports scan results
纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
Equivalence class partition method for test case design method
数字IC手撕代码--交通灯
28000 word summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little floating (Part 2)
每周 Postgres 世界动态 2022w25
VQA不只需要图片,还需要外部知识!华盛顿大学&微软提出提出REVIVE,用GPT-3和Wikidata来辅助回答问题!...
Redis的缓存雪崩、缓存击穿、缓存穿透与缓存预热、缓存降级
Redis主从复制原理
Recruiting talents and seeking development | Jincang of the National People's Congress won the "best employer school recruitment case Award"
动荡的中介生意,不安的租房人
Redis为什么这么快?Redis是单线程还是多线程?
Wechat applet: repair collection interface version cloud development expression package
Dynamics 365Online Lookup查找字段多选
c语言入门教程–-6循环语句
JUC多线程:线程池的创建及工作原理
【黑马早报】中公教育市值蒸发逾2000亿;新东方直播粉丝破2000万;HM关闭中国首店;万科郁亮称房地产已触底;微信上线“大爆炸”功能...
投资人跌下神坛:半年0出手,转行送外卖