当前位置:网站首页>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!
边栏推荐
- [the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
- 20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
- 7-5 走楼梯升级版(PTA程序设计)
- C language Getting Started Guide
- 7-14 错误票据(PTA程序设计)
- Redis实现分布式锁原理详解
- Leetcode. 3. Longest substring without repeated characters - more than 100% solution
- Redis cache obsolescence strategy
- Miscellaneous talk on May 14
- 7. Relationship between array, pointer and array
猜你喜欢
C语言入门指南
PriorityQueue (large root heap / small root heap /topk problem)
1.C语言初阶练习题(1)
4. Branch statements and loop statements
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
A piece of music composed by buzzer (Chengdu)
Caching mechanism of leveldb
. Net6: develop modern 3D industrial software based on WPF (2)
扑克牌游戏程序——人机对抗
仿牛客技术博客项目常见问题及解答(二)
随机推荐
C language to achieve mine sweeping game (full version)
Mortal immortal cultivation pointer-1
深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
5月27日杂谈
1.C语言初阶练习题(1)
Redis的两种持久化机制RDB和AOF的原理和优缺点
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
实验九 输入输出流(节选)
(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
3. Input and output functions (printf, scanf, getchar and putchar)
5. Download and use of MSDN
Redis cache obsolescence strategy
C language Getting Started Guide
[graduation season · advanced technology Er] goodbye, my student days
Using qcommonstyle to draw custom form parts
[中国近代史] 第六章测验
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
js判断对象是否是数组的几种方式
Redis实现分布式锁原理详解
优先队列PriorityQueue (大根堆/小根堆/TopK问题)