当前位置:网站首页>canvas绘制文本和清除绘制
canvas绘制文本和清除绘制
2022-07-23 17:24:00 【M78_国产007】
绘制文本
绘制文本的方法有两种:fillText和strokeText
fillText()有五个参数,第一个参数为绘制内容(字符串),第二个参数为起点x轴坐标,第三个参数为起点y轴坐标后两个参数可选,为最大宽度和最大高度。
strokeText类似,但是它无填充,简单来说就是文字不能被修改颜色。
文本颜色使用fillStyle属性指定
文本字体使用font属性指定:font:"字体大小 字体样式",两个值缺一不可。
textAlign属性指定水平方向对齐方式;
属性值有:start、left、center、right、end
textBaseline则指定垂直方向。
属性值有:top、hanging、middle 、alphabetic、ideographic、bottom
学会绘制文本后,我们完善一下昨天写的柱状图。
<style>
#cav{
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="cav" width="500" height="400"></canvas>
<script>
var cav =document.querySelector("#cav")
var ctx= cav.getContext("2d")
ctx.moveTo(50,350)
ctx.lineTo(450,350)
ctx.stroke()
//假数据
arr=[500,600,800,700,500,800]
arr1=["一季度","二季度","三季度","四季度","最低季度","最高季度",]
//设置最大高度
var maxh=300/Math.max(...arr)
for (let i = 0; i < arr.length; i++) {
ctx.beginPath()
ctx.rect(80+i*60,350-maxh*arr[i],50,maxh*arr[i])
ctx.fillText(arr1[i],85+i*60,370)
ctx.fillText(arr[i],90+i*60,350-maxh*arr[i]-10)
ctx.font="14px weiruanyahei"
ctx.fillStyle="purple"
ctx.fill()
}
</script>效果:
清除绘制
clearRect() 方法清空给定矩形内的指定像素
有四个参数:
第一个参数为矩形的左上角顶点的x轴坐标;
第二个参数为矩形的左上角顶点的y轴坐标;
第三个参数为矩形的宽度;
第四个参数为矩形的高度。
代码演示一下:
<canvas id="cav" width="400" height="400"></canvas>
<script>
var cav=document.querySelector("#cav")
var cxt=cav.getContext("2d")
//设置填充色
cxt.fillStyle="pink"
//创建一个填充颜色的矩形
cxt.fillRect(100,100,100,200)
//清除一半
cxt.clearRect(100,100,100,100)运行代码后可以看到创建的矩形被清除了一半

除了这里的清除绘制外,window对象还为我们提供了一个方法清除绘制。就是重设画布width,canvas.width=canvas.width,这样同样也能够清除前面的绘制。
边栏推荐
- [onnx] the problem of dynamic input size (multi output / multi input)
- Leetcode sword finger offer II 115. reconstruction sequence: diagram topology sorting
- Detailed explanation: tmp1750 chip three channel linear LED driver
- [the whole process of Game Modeling and model production] create the game soldier character with ZBrush
- Application of jishili electrometer in testing scheme of new energy battery
- DevStack云计算平台快速搭建
- Jumpserver administrator account is locked
- 界面设计四大原则
- C#Split的用法,Split分割字符串
- .net core implements background tasks (scheduled tasks) longbow Tasks component (III)
猜你喜欢
![Paddlenlp's UIE classification model [taking emotional propensity analysis news classification as an example] including intelligent annotation scheme)](/img/00/e97dcac9f07e5d4512614536a17cd4.png)
Paddlenlp's UIE classification model [taking emotional propensity analysis news classification as an example] including intelligent annotation scheme)

Access intranet rds+mysql through SSH

elk笔记25--快速体验APM

AE 教程,如何在 After Effects 中对 Illustrator 分图层文档进行动画绘制?

Tcl 语言之Synopsys Tcl篇(3)(数字IC)

FPGA flash reading and writing based on SPI

Navigation component of jetpack compose uses

基于FPGA的UART接口设计
![[2020] [paper notes] phase change materials and Hypersurfaces——](/img/cc/a69afb3acd4b73a17dbbe95896404d.png)
[2020] [paper notes] phase change materials and Hypersurfaces——
.NET Core 实现后台任务(定时任务)Longbow.Tasks 组件(三)
随机推荐
1259. Disjoint handshake dynamic programming
Tree learning summary
UPC 2022 summer personal training game 12 (number of combinations b)
elk笔记25--快速体验APM
398. Random number index hash table method
SQL 语句练习
Learn and understand Architecture Design from business development
mBio | 海洋所孙超岷组在深海原位验证了微生物介导的单质硫形成新通路
Error reporting caused by the use of responsebodyadvice interface and its solution
Redis [2022 latest interview question]
mysql的访问端口是什么意思_数据库端口是什么端口号
The original path is not original [if there is infringement, please contact the original blogger to delete]
Fragment
[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz
How can win11 add 3D effects to pictures? Win11 method of adding picture 3D effect
What is stack and the difference between stacks
还在用Xshell?你out了,推荐一个更现代的终端连接工具
C语言小项目 -- 通讯录(静态版+动态版+文件版)
Challenges of decentralized storage
FPGA实现IIC协议(一)IIC总线协议