当前位置:网站首页>Chapter 4 line operation of canvas
Chapter 4 line operation of canvas
2022-06-24 07:58:00 【yxqq378287007】
《canvas》 The first 4 Chapter Line operation
The first 4 Chapter Line operation
4.1 Introduction to line operation
- Line operation properties
| attribute | explain |
|---|---|
| lineWidth | Define the line width |
| lineCap | Define line cap style |
| lineJoin | Define the style at the intersection of two lines |
- Line operation method
| Method | explain |
|---|---|
| setLineDash() | Define the virtual and real styles of lines |
4.2 lineWidth attribute
cxt.lineWidth = Integers ;
Default 1, Default unit px.
- Line width
<!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"); //lineWidth The value is 5 //cxt.beginPath(); cxt.moveTo(20, 20); cxt.lineTo(180, 20); cxt.lineWidth = 5; cxt.stroke(); //lineWidth The value is 10 cxt.beginPath(); cxt.moveTo(20, 70); cxt.lineTo(180, 70); cxt.lineWidth = 10; cxt.stroke(); //lineWidth The value is 15 cxt.beginPath(); cxt.moveTo(20, 120); cxt.lineTo(180, 120); cxt.lineWidth = 15; cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray"></canvas>
</body>
</html>
- Curve width
<!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.arc(70, 70, 50, 0, -90 * Math.PI / 180, false); cxt.lineWidth = 5; cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
Suppose the line width lineWidth,strokeRect() Method to draw a rectangle with the actual width of width+lineWidth, The actual height is height+lineWidth,arc() Method to draw a circle with the actual radius of radius+lineWidth.
4.3 lineCap attribute
Define line cap styles at the beginning and end of the line .
cxt.lineCap = " Property value ";
| Property value | explain |
|---|---|
| butt | Wireless cap , Default |
| round | Round cap , The radius is half the line width |
| square | Square thread cap , The width is half of the line width |
- Line cap straight line
<!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.lineWidth = 16; //lineCap Value is the default (butt) //cxt.beginPath(); cxt.moveTo(20, 20); cxt.lineTo(180, 20); //cxt.lineCap = "butt"; cxt.stroke(); //lineCap Value changed to round cxt.beginPath(); cxt.moveTo(20, 70); cxt.lineTo(180, 70); cxt.lineCap = "round"; cxt.stroke(); //lineCap Value changed to square cxt.beginPath(); cxt.moveTo(20, 120); cxt.lineTo(180, 120); cxt.lineCap = "square"; cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray"></canvas>
</body>
</html>
- z
<!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.moveTo(50, 50); cxt.lineTo(100, 50); cxt.lineTo(50, 100); cxt.lineTo(100, 100); cxt.lineWidth = 12; cxt.lineCap = "round"; cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
4.4 lineJoin attribute
Define the style at the intersection of lines .
cxt.lineJoin = " Property value ";
| Property value | explain |
|---|---|
| miter | Sharp corners , Default |
| round | Round corners , The radius is half the line width |
| bevel | Bevel angle , The diagonal is the line width |
- Line junction style
<!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 yoffset = 100; cxt.lineWidth = 12; //cxt.beginPath(); cxt.moveTo(50, 50); cxt.lineTo(100, 50); cxt.lineTo(50, 100); cxt.lineTo(100, 100); //cxt.lineJoin = "miter"; cxt.stroke(); cxt.beginPath(); cxt.moveTo(50, 50+yoffset); cxt.lineTo(100, 50+yoffset); cxt.lineTo(50, 100+yoffset); cxt.lineTo(100, 100+yoffset); cxt.lineJoin = "round"; cxt.stroke(); cxt.beginPath(); cxt.moveTo(50, 50+yoffset*2); cxt.lineTo(100, 50+yoffset*2); cxt.lineTo(50, 100+yoffset*2); cxt.lineTo(100, 100+yoffset*2); cxt.lineJoin = "bevel"; cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="350" style="border:1px dashed gray;"></canvas>
</body>
</html>
4.5 setLineDash() Method
Define the virtual and real styles of lines .
cxt.setLineDash(array);
array Is an array combination .
[10, 5] Express 10px Solid line ,5px blank ,Chrome、Firefox Support setLineDash() Method ,IE I won't support it .
<!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.strokeStyle = "red"; cxt.setLineDash([10, 5]); cxt.strokeRect(50, 50, 80, 80); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
边栏推荐
- [测试开发]初识软件测试
- 10. Tencent cloud IOT device side learning - firmware upgrade
- 『C语言』系统日期&时间
- [data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
- Standing at the center of the storm: how to change the engine of Tencent
- BOM notes
- 日期、时间库使用备注
- Vulnhub靶机:BOREDHACKERBLOG: SOCIAL NETWORK
- The startup mode of cloudbase init is \Cloudbase init has hidden dangers
- 解决错误: LNK2019 无法解析的外部符号
猜你喜欢

第 2 篇:绘制一个窗口

希尔伯特-黄变换

Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022

SVN实测常用操作-记录操作大全

Cloud development who is the source code of undercover applet

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
![[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board](/img/10/6725b51120a6ae8b16d60f5b1ae904.jpg)
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board

Chapter 3: drawing triangles

解决错误: LNK2019 无法解析的外部符号
随机推荐
Latest news of awtk: new usage of grid control
How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
解决错误: LNK2019 无法解析的外部符号
The two most frequently asked locks in the interview
1-4metasploitable2介绍
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
Duilib display memory picture
Gossip: what happened to 3aC?
Free ICP domain name filing interface
4-操作列表(循环结构)
Baidu map, coordinate inversion, picking coordinate position
第 2 篇:繪制一個窗口
Continue to have a fever. Try the asynchronous operation of dart language. The efficiency is increased by 500%
What industries and scenarios can the easynvr/easygbs/easycvr platform developed by tsingsee green rhino video be used in?
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
线程的阻塞问题
常见的数组封装
基于Distiller的模型压缩工具简介
Hilbert Huang Transform
报错“Computation failed in `stat_summary_hex()`”