当前位置:网站首页>Canvas foundation 1 - draw a straight line (easy to understand)
Canvas foundation 1 - draw a straight line (easy to understand)
2022-07-06 13:50:00 【Ling Xiaoding】
List of articles
This article begins with canvas, Easy to understand , Xiaobai has no trouble , According to MOOCS liuyubobobo The teacher's video lesson learning and sorting
Video lesson guide : For class network liuyubobobo teacher Gorgeous countdown effect Canvas Fundamentals of drawing and animation
List of articles
1、 Declaration label
- Width and height are on the label
<canvas id="canvas" width="1024" height="768" style="background-color: rgb(200,200,200);display: block;margin: 20px auto;" >
The current browser does not support canvas, Please change your browser and try again
</canvas>
- Width and height in js Set in
<canvas id="canvas" style="background-color: rgb(200,200,200);display: block;margin: 20px auto;">
The current browser does not support canvas, Please change your browser and try again
</canvas>
window.onload = function () {
// obtain
var canvas = document.getElementById('canvas')
canvas.width = 1024
canvas.height = 768
// Determine whether the browser can be used canvas
if (canvas.getContext('2d')) {
// Use 2d mapping
var context = canvas.getContext('2d')
// Use context draw
} else {
alert(' The current browser does not support canvas, Please change your browser and try again ')
}
}
2、 Draw a straight line
canvas First set the state and then draw

Draw a straight line js Code
context.moveTo(x, y) The starting point
context.lineTo(x, y) End
context.lineWidth = 5 Line width
context.strokeStyle = ‘#005588’ Line style
context.stroke() draw
window.onload = function () {
var canvas = document.getElementById('canvas')
canvas.width = 1024
canvas.height = 768
if (canvas.getContext('2d')) {
var context = canvas.getContext('2d')
// Use context draw
// From coordinates (100,100) Start from
context.moveTo(100, 100)
// To coordinates (700,700) Draw a straight line for the end
context.lineTo(700, 700)
// The line width is 5
context.lineWidth = 5
// The style and color of the line
context.strokeStyle = '#005588'
// Execute the operation of drawing a straight line
context.stroke()
} else {
alert(' The current browser does not support canvas, Please change your browser and try again ')
}
}
effect 
3、 A figure composed of lines - Draw a triangle
window.onload = function () {
var canvas = document.getElementById('canvas')
canvas.width = 1024
canvas.height = 768
if (canvas.getContext('2d')) {
var context = canvas.getContext('2d')
// Use context draw
// From coordinates (100,100) Start from
context.moveTo(100, 100)
context.lineTo(700, 700)
context.lineTo(100, 700)
context.lineTo(100, 100)
// The line width is 5
context.lineWidth = 5
// The style and color of the line
context.strokeStyle = '#005588'
// Execute the operation of drawing a straight line
context.stroke()
} else {
alert(' The current browser does not support canvas, Please change your browser and try again ')
}
}

4、 Polygon fill and stroke
- context.fillStyle = ‘rgb(2,100,30)’ Fill color
- context.fill() fill
window.onload = function () {
var canvas = document.getElementById('canvas')
canvas.width = 1024
canvas.height = 768
if (canvas.getContext('2d')) {
var context = canvas.getContext('2d')
// Use context draw
// From coordinates (100,100) Start from
context.moveTo(100, 100)
// To coordinates (700,700) Draw a straight line for the end
context.lineTo(700, 700)
context.lineTo(100, 700)
context.lineTo(100, 100)
// The line width is 5
context.lineWidth = 5
// The style and color of the line
context.strokeStyle = '#f00'
// Execute the operation of drawing a straight line
context.stroke()
// Polygon fill
context.fillStyle = 'rgb(2,100,30)'
context.fill()
} else {
alert(' The current browser does not support canvas, Please change your browser and try again ')
}
}

5、 Define two shapes
- context.beginPath() start
- context.closePath() end
- These two methods wrap the two paths and separate them from the other paths
window.onload = function () {
var canvas = document.getElementById('canvas')
canvas.width = 1024
canvas.height = 768
if (canvas.getContext('2d')) {
var context = canvas.getContext('2d')
// Use context draw
// From coordinates (100,100) Start from
context.beginPath()
context.moveTo(100, 100)
// To coordinates (700,700) Draw a straight line for the end
context.lineTo(700, 700)
context.lineTo(100, 700)
context.lineTo(100, 100)
context.closePath()
// The line width is 5
context.lineWidth = 5
// The style and color of the line
context.strokeStyle = '#f00'
// Execute the operation of drawing a straight line
context.stroke()
// // Polygon fill
// context.fillStyle = 'rgb(2,100,30)'
// context.fill()
context.beginPath()
context.moveTo(200, 100)
context.lineTo(700, 600)
context.closePath()
context.strokeStyle = '#000'
context.stroke()
} else {
alert(' The current browser does not support canvas, Please change your browser and try again ')
}
}

6、 Neves demo Example
- html part
<canvas id="canvas" width="800" height="800" style="background-color: aliceblue;display: block;margin: 10px auto;">
The current browser does not support canvas, Please change your browser and try again
</canvas>
- Self made data
var tangram = [
{
p: [{
x: 0,y: 0},{
x: 800,y: 0},{
x: 400,y: 400},],
color: '#caff67'
}, {
p: [{
x: 0,y: 0},{
x: 400,y: 400},{
x: 0,y: 800},],
color: '#67becf'
}, {
p: [{
x: 0,y: 800},{
x: 400,y: 400},{
x: 400,y: 800},],
color: '#f50'
}, {
p: [{
x: 400,y: 800}, {
x: 800,y: 800}, {
x: 400,y: 400}, ],
color: '#0f5'
}, {
p: [{
x: 400,y: 400}, {
x: 800,y: 0}, {
x: 600,y: 600}, ],
color: '#25f'
}, {
p: [{
x: 800,y: 0}, {
x: 600,y: 600}, {
x: 800,y: 800}, ],
color: '#f33'
},
]
- js part
let canvas = document.getElementById('canvas')
if (canvas.getContext('2d')) {
var context = canvas.getContext('2d')
tangram.forEach((item, index) => {
draw(item, context)
})
}
function draw(piece, cxt) {
cxt.beginPath()
cxt.moveTo(piece.p[0].x, piece.p[0].y)
piece.p.forEach((item, index) => {
cxt.lineTo(item.x, item.y)
})
cxt.closePath()
cxt.fillStyle = piece.color
cxt.fill()
cxt.lineWidth = 2
cxt.strokeStyle = '#000'
cxt.stroke()
}
7、 Draw a square demo
<canvas id="canvas" width="1024" height="768" style="display: block; background-color: #eee;margin: 10px auto;">
Out of commission
canvas
</canvas>
<script> var canvas = document.getElementById('canvas') var context = canvas.getContext('2d') drawRect(context, 100, 100, 400, 400, 10, '#600', '#008833') function drawRect(cxt, x, y, width, height, borderWidth, borderColor, borderFill) {
cxt.beginPath() cxt.moveTo(x, y) cxt.lineTo(x + width, y) cxt.lineTo(x + width, y + height) cxt.lineTo(x + width, y + height) cxt.lineTo(x, y + height) cxt.closePath() cxt.lineWidth = borderWidth cxt.strokeStyle = borderColor cxt.fillStyle = borderFill cxt.fill() cxt.stroke() } </script>
summary
This article asks canvas Section 1 , After that, it will continue to update , If you feel it is still practical , It's OK to follow or like , Thank you very much!
边栏推荐
- 深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
- 3. Number guessing game
- The latest tank battle 2022 - Notes on the whole development -2
- 5. Download and use of MSDN
- 关于双亲委派机制和类加载的过程
- js判断对象是否是数组的几种方式
- Pit avoidance Guide: Thirteen characteristics of garbage NFT project
- 记一次猫舍由外到内的渗透撞库操作提取-flag
- [面試時]——我如何講清楚TCP實現可靠傳輸的機制
- Mortal immortal cultivation pointer-2
猜你喜欢

Wei Pai: the product is applauded, but why is the sales volume still frustrated

1.C语言初阶练习题(1)

.Xmind文件如何上传金山文档共享在线编辑?

ABA问题遇到过吗,详细说以下,如何避免ABA问题

7-5 走楼梯升级版(PTA程序设计)

2. Preliminary exercises of C language (2)

这次,彻底搞清楚MySQL索引

Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems

7-7 7003 组合锁(PTA程序设计)

Mortal immortal cultivation pointer-2
随机推荐
2. C language matrix multiplication
Floating point comparison, CMP, tabulation ideas
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
【头歌educoder数据表中数据的插入、修改和删除】
7-6 矩阵的局部极小值(PTA程序设计)
Differences among fianl, finally, and finalize
[modern Chinese history] Chapter 6 test
6. Function recursion
7-15 h0161. 求最大公约数和最小公倍数(PTA程序设计)
canvas基础1 - 画直线(通俗易懂)
fianl、finally、finalize三者的区别
5. Function recursion exercise
Redis的两种持久化机制RDB和AOF的原理和优缺点
5. Download and use of MSDN
C语言入门指南
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
【九阳神功】2016复旦大学应用统计真题+解析
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
实验五 类和对象
[中国近代史] 第九章测验