当前位置:网站首页>Canvas draw text and clear draw
Canvas draw text and clear draw
2022-07-23 19:25:00 【M78_ Domestic 007】
Draw text
There are two ways to draw text :fillText and strokeText
fillText() There are five parameters , The first parameter is the drawing content ( character string ), The second parameter is the starting point x Axis coordinates , The third parameter is the starting point y The last two parameters of axis coordinates are optional , Is the maximum width and maximum height .
strokeText similar , But it has no padding , Simply put, text cannot be changed in color .
Text color usage fillStyle Attribute specifies
Text font usage font Attribute specifies :font:" font size Font style ", Both values are indispensable .
textAlign Property specifies the horizontal alignment ;
Attribute value has :start、left、center、right、end
textBaseline Then specify the vertical direction .
Attribute value has :top、hanging、middle 、alphabetic、ideographic、bottom
After learning to draw text , Let's improve the histogram written yesterday .
<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()
// Fake data
arr=[500,600,800,700,500,800]
arr1=[" First quarter "," The two quarter "," third quater "," In the fourth quarter "," Minimum quarter "," The highest quarter ",]
// Set the maximum height
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>effect :
Clear paint
clearRect() Method to clear the specified pixels in a given rectangle
There are four parameters :
The first parameter is the vertex of the upper left corner of the rectangle x Axis coordinates ;
The second parameter is the vertex of the upper left corner of the rectangle y Axis coordinates ;
The third parameter is the width of the rectangle ;
The fourth parameter is the height of the rectangle .
Code demonstration :
<canvas id="cav" width="400" height="400"></canvas>
<script>
var cav=document.querySelector("#cav")
var cxt=cav.getContext("2d")
// Set fill color
cxt.fillStyle="pink"
// Create a rectangle filled with color
cxt.fillRect(100,100,100,200)
// Clear half
cxt.clearRect(100,100,100,100)After running the code, you can see that half of the created rectangle has been cleared

In addition to the clear drawing here ,window Object also provides us with a method to clear the drawing . Is to reset the canvas width,canvas.width=canvas.width, This also clears the previous drawing .
边栏推荐
- Mee | Zhejiang University Chenglei group develops a new method for designing and constructing synthetic flora
- 【机器学习】吴恩达:终生学习
- 树学习总结
- [paper reading] gettext: trajectory flow map enhanced transformer for next POI recommendation
- mBio | 海洋所孙超岷组在深海原位验证了微生物介导的单质硫形成新通路
- As a background developer, you must know two kinds of filters
- DP problem collection
- [machine learning] Wu Enda: lifelong learning
- Application of jishili electrometer in testing scheme of new energy battery
- Gvim/vim tips
猜你喜欢

入门数据库Days3

Data link layer -------- Ethernet and ARP

ES6其他语法及扩展语法总结

国外芯片,为什么有中文资料和网页?

有向图之求两点之间所有路径

How can zero foundation self-study software testing? Ten year test Laoniao's strongest software test learning Roadmap

多线程&高并发(全网最新:面试题+导图+笔记)面试手稳心不慌

【leetcode天梯】链表 · 206 反转链表

J9数字论:数字行业的FOMO现象我们应该怎么克服?

【leetcode天梯】链表 · 022 链表中倒数第k个节点
随机推荐
What is stack and the difference between stacks
TCL脚本语言详解(1)
关于:在企业局域网中启用 Delivery Optimization
使用 frp 实现内网穿透
MEE | 浙大程磊组开发设计与构建合成菌群新方法
Alibaba最新神作!耗时187天肝出来1015页分布式全栈手册太香了
结构体大小的计算(结构体内存对齐)
作为一名后台开发人员,你必须知道的两种过滤器
正则化不同导致的tuple错误
Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
树莓派3b串口登录前准备工作
Lendingclub loan status business details current, charge off, issued, full paid, grace period
【开发经验】开发项目踩坑集合【持续更新】
Leetcode daily question (1514. path with maximum probability)
【C语言】程序环境和预处理
As a background developer, you must know two kinds of filters
Challenges of decentralized storage
Mee | Zhejiang University Chenglei group develops a new method for designing and constructing synthetic flora
当代励志“女”录
【leetcode天梯】链表 · 206 反转链表