当前位置:网站首页>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!
边栏推荐
- 【毕业季·进击的技术er】再见了,我的学生时代
- ArrayList的自动扩容机制实现原理
- The difference between overloading and rewriting
- [modern Chinese history] Chapter 9 test
- Custom RPC project - frequently asked questions and explanations (Registration Center)
- Differences among fianl, finally, and finalize
- 2.初识C语言(2)
- 9. Pointer (upper)
- 简单理解ES6的Promise
- 深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
猜你喜欢
仿牛客技术博客项目常见问题及解答(三)
强化学习系列(一):基本原理和概念
FAQs and answers to the imitation Niuke technology blog project (I)
canvas基础1 - 画直线(通俗易懂)
Using spacedesk to realize any device in the LAN as a computer expansion screen
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
6. Function recursion
FAQs and answers to the imitation Niuke technology blog project (II)
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
A piece of music composed by buzzer (Chengdu)
随机推荐
渗透测试学习与实战阶段分析
[modern Chinese history] Chapter 6 test
Aurora system model of learning database
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
Difference and understanding between detected and non detected anomalies
仿牛客技术博客项目常见问题及解答(三)
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
C language Getting Started Guide
Wei Pai: the product is applauded, but why is the sales volume still frustrated
FAQs and answers to the imitation Niuke technology blog project (I)
Miscellaneous talk on May 14
2. Preliminary exercises of C language (2)
MySQL锁总结(全面简洁 + 图文详解)
实验七 常用类的使用(修正帖)
7-5 走楼梯升级版(PTA程序设计)
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
简单理解ES6的Promise
The difference between abstract classes and interfaces
【头歌educoder数据表中数据的插入、修改和删除】