当前位置:网站首页>Canvas learning Chapter 1
Canvas learning Chapter 1
2022-04-23 08:06:00 【Pen drawing Acacia】
One : Drawing graphics
1: Draw a solid rectangle
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect(10, 10, 55, 50);

2: Draw a rectangular border
ctx.fillStyle = "rgb(200,0,0)";
ctx.strokeRect(10, 10, 55, 50);

3: Clear the specified rectangular area , Make the clear part completely transparent .
clearRect(x, y, width, height)
4: Case study
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
ctx.fillRect(25, 25, 100, 100);
ctx.clearRect(45, 45, 60, 60);
ctx.strokeRect(50, 50, 50, 50);

Two : Draw the path
The basic element of graphics is path . A path is a collection of points of different shapes formed by connecting lines or curves of different colors and widths . A path , Even a subpath , It's all closed . Drawing a graph using a path requires some extra steps .
1: First , You need to create a path starting point .
2: Then you use the draw command to draw the path .
3: Then you close the path .
4: Once the path is generated , You can render graphics by tracing or filling the path area .
Here are the functions to use :
1:beginPath()
Create a new path , Generated after , The drawing command is directed to the path to generate the path .
closePath()
2: After closing the path, the drawing command points back to the context .
3:stroke()
Draw the outline of a figure by lines .
4:fill()
Generate solid graphics by filling the content area of the path .
Case study :
ctx.beginPath();
ctx.moveTo(75, 50);
ctx.lineTo(100, 75);
ctx.lineTo(100, 25);
ctx.fill();

Move the strokes :
moveTo(x, y)
Move the stroke to the specified coordinates x as well as y On .
lineTo(x, y)
Draw a line from the current position to the specified x as well as y Line of position .
This method has two parameters :x as well as y , The point representing the end of the line in the coordinate system . The starting point is related to the previous drawing path , The end of the previous path is the beginning of the next , wait ... You can start with moveTo() The function change .
Case study :
function draw() {
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
// Fill in triangles
ctx.beginPath();
ctx.moveTo(25, 25);
ctx.lineTo(105, 25);
ctx.lineTo(25, 105);
ctx.fill();
// Stroke triangle
ctx.beginPath();
ctx.moveTo(125, 125);
ctx.lineTo(125, 45);
ctx.lineTo(45, 125);
ctx.closePath();
ctx.stroke();
}
}

3、 ... and : Draw the arc
arc(x, y, radius, startAngle, endAngle, anticlockwise)
Draw a picture of (x,y) Centered on radius Arc of radius ( round ), from startAngle Start to endAngle end , according to anticlockwise Given direction ( Default is clockwise ) To generate .
arcTo(x1, y1, x2, y2, radius)[ Not commonly used ]
Draw an arc according to the given control point and radius , Then connect the two control points in a straight line .
Case study :
function draw() {
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
for(var i = 0; i < 4; i++){
for(var j = 0; j < 3; j++){
ctx.beginPath();
var x = 25 + j * 50; // x Coordinate value
var y = 25 + i * 50; // y Coordinate value
var radius = 20; // Radius of arc
var startAngle = 0; // Starting point
var endAngle = Math.PI + (Math.PI * j) / 2; // The end point
var anticlockwise = i % 2 == 0 ? false : true; // Clockwise or counter clockwise
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
if (i>1){
ctx.fill();
} else {
ctx.stroke();
}
}
}
}
}

版权声明
本文为[Pen drawing Acacia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624332301.html
边栏推荐
- [problem solving] vs2019 solves the problem that the EXE file generated by compilation cannot be opened
- [programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
- Talking about distributed storage from ES, mongodb, redis and rocketmq
- 内网渗透系列:内网隧道之icmp_tran
- Cloud computing skills competition -- Part 2 of openstack private cloud environment
- 使用 Ingress 实现金丝雀发布
- Introduction to sap query enhanced development
- strcat()、strcpy()、strcmp()、strlen()
- sentinel集成nacos动态更新数据原理
- C read INI file and write data to INI file
猜你喜欢

Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)

内网渗透系列:内网隧道之dnscat2

Sto with billing cross company inventory dump return

Ctf-misc summary

CSV Column Extract列提取

云计算技能大赛 -- openstack私有云环境 第二部分
![[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP](/img/6c/7408180d0c24560b4a68982635520e.jpg)
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP

Intranet penetration series: pingtunnel of Intranet tunnel

Go语学习笔记 - 数组 | 从零开始Go语言

Mysql database backup and recovery under Linux (full + incremental)
随机推荐
SAP self created table log function is enabled
简述存储器的分级策略
Cloud computing skills competition -- the first part of openstack private cloud environment
Redis事务实现乐观锁原理
MySQL -- the secret of lock -- how to lock data
使用 Ingress 实现金丝雀发布
LeetCode 1611. 使整数变为 0 的最少操作次数
MySQL--锁的奥秘--数据怎么锁
【问题解决】VS2019解决编译生成的exe文件打不开的情况
【编程实践/嵌入式比赛】嵌入式比赛学习记录(二):基于TCP的图片流传输
C 输出一种二维数组,特点如下。
Go语学习笔记 - Slice、Map | 从零开始Go语言
Redis -- why is the string length of string emstr the upper limit of 44 bytes?
C problem of marking the position of polygons surrounded by multiple rectangles
GUI,CLI与Unix哲学
访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
Mobile web (Font Icon, plane conversion, color gradient)
Implementation of new
Ignis公链的NFT生态发展:Unicorn.art的捐赠开发之路
国基北盛-openstack-容器云-环境搭建