Canvas入门
1、 canvas简介
canvas 是什么?
是HTML5中重要的元素,和audio、video元素类似完全不需要任何外部插件就能够运行.
Canvas中文翻译就是”画布”.它提供了强大的图形的处理功能(绘制,变换,像素处理…)。
但是需要注意,canvas 元素本身并不绘制图形,它只是相当于一张空画布。如果开发者需要向 canvas 上绘制图形,则必须使用 JavaScript 脚本进行绘制。
canvas能做什么?
- 基础图形的绘制
- 文字的绘制
- 图形的变形和图片的合成
- 图片和视频的处理
- 动画的实现
- 小游戏的制作
支持的浏览器
大多数现代浏览器都是支持Canvas的,比如 Firefox, safari, chrome, opera的最近版本以及IE9都支持。 IE8及以下不支持HTML5,但是我们可以进行提示用户更新到最新的版本。
入门案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
html,body{
margin: 0px;
}
canvas{
background: #ccc;
}
</style>
</head>
<body>
<canvas id="canvas_1">
我们在设置 canvas 之前需要首先监测用户电脑是否支持 canvas
</canvas>
</body>
<script type="text/javascript">
var canvas_1 = document.getElementById("canvas_1");
canvas_1.width = "500";
canvas_1.height = "500";
</script>
</html>
2、 绘制直线
方法 方法
beginPath() 开始定义路径
closePath() 关闭前面定义的路径
moveTo(float x,float y) 把 canvas 的当前路径的结束点移动到 x, y 对应的点
lineTo(float x,float y) 把 canvas 的当前路径从当前结束点连接到 x , y 对应的点
方法 | 说明 |
---|
beginPath() | 开始定义路径 |
closePath() | 关闭前面定义的路径 |
moveTo(float x,float y) | 把 canvas 的当前路径的结束点移动到 x, y 对应的点 |
beginPath() | 开始定义路径 |
lineTo(float x,float y) | 把 canvas 的当前路径从当前结束点连接到 x , y 对应的点 |
fill() | 填充 canvas 当前路径 |
stroke() | 填充 canvas 当前路径绘制边框 |
fillStyle() | 设置填充 canvas 路径所使用的填充风格 |
strokeStyle() | 设置绘制 canvas 路径的填充风格 |
lineWidth() | 设置笔触线条的宽度 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;">
当前浏览器不支持 canvas
</canvas>
</div>
</body>
<script type="text/javascript">
var canvas_1 = document.getElementById("canvas_1");
var ctx = canvas_1.getContext("2d");
console.log(ctx);
ctx.beginPath();
ctx.moveTo(100,100);
ctx.lineTo(700,400);
ctx.lineTo(400,100);
ctx.lineTo(600,500);
ctx.closePath();
ctx.lineWidth = 10;
ctx.strokeStyle = "red";
ctx.stroke();
ctx.fillStyle = "green";
ctx.fill();
</script>
</html>
3、 绘制矩形
- | - |
---|
strokeRect(float x,float y,float width,float height) | 绘制一个矩形边框 |
fillRect(float x,float y,float width,float height) | 填充一个矩形边框 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<h2>绘制矩形</h2>
<canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;">
当前浏览器不支持 canvas
</canvas>
</div>
</body>
<script type="text/javascript">
var canvas_1 = document.getElementById("canvas_1");
var ctx = canvas_1.getContext("2d");
ctx.fillStyle = '#f00';
ctx.fillRect(30,20,120,60);
ctx.fillStyle = '#ff0';
ctx.fillRect(80,60,120,60);
ctx.strokeStyle = '#00f';
ctx.strokeRect(30,130,120,60);
ctx.lineWidth = 20;
ctx.lineJoin = "round";
ctx.strokeStyle = '#0ff';
ctx.strokeRect(80,160,120,60);
ctx.lineJoin = "bevel";
ctx.strokeStyle = '#f0f';
ctx.strokeRect(130,190,120,60);
ctx.storke();
</script>
</html>
4、 绘制字符串
- | - |
---|
fillText(String Text, float x, float y, [float maxWidth]) | 填充字符串 |
strokeText(String Text, float x, float y, [float maxWidth]) | 绘制字符串边框 |
textAlign | 设置绘制字符串的水平对齐方式(start、end、left、right、center等) |
textBaseAlign | 设置绘制字符串的垂直对齐方式(top、hanging、middle、alphabetic、idecgraphic、bottom 等) |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<h2>绘制文字</h2>
<canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;">
当前浏览器不支持 canvas
</canvas>
</div>
</body>
<script type="text/javascript">
var canvas_1 = document.getElementById("canvas_1");
var ctx = canvas_1.getContext("2d");
ctx.fillStyle = '#00f';
ctx.font = 'italic 50px 隶书';
ctx.textBaseline = 'top';
ctx.fillText('汪先生真是帅',0,0);
ctx.strokeStyle = 'f0f';
ctx.font = 'bold 45px 宋体';
ctx.strokeText('汪先生我爱你',0,50,200);
</script>
</html>
5、 设置阴影
- | - |
---|
shadowBlur | 设置阴影的模糊程度。该值是一个浮点数,该数值越大,阴影的模糊程度也就越大。 |
shadowColor | 设置阴影的颜色。 |
shadowOffsetX | 设置阴影在 X 方向的偏移 |
shadowOffsetY | 设置阴影在 Y 方向的偏移 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<h2>绘制文字</h2>
<canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;">
当前浏览器不支持 canvas
</canvas>
</div>
</body>
<script type="text/javascript">
var canvas_1 = document.getElementById("canvas_1");
var ctx = canvas_1.getContext("2d");
ctx.shadowBlur = 5.6;
ctx.shadowColor = '#222';
ctx.shadowOffsetX = 10;
ctx.shadowOffsetY = -6;
ctx.fillStyle = '#00f';
ctx.font = 'italic 50px 隶书';
ctx.textBaseline = 'top';
ctx.fillText('汪先生真是帅',0,0);
ctx.strokeStyle = 'f0f';
ctx.font = 'bold 45px 宋体';
ctx.strokeText('汪先生我爱你',0,50,200);
</script>
</html>
6、 画圆
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210427173628556.png)
context.arc(x,y,r,sAngle,eAngle,counterclockwise);
- | - |
---|
x | 圆的中心的 x 坐标。 |
y | 圆的中心的 y 坐标。 |
r | 圆的半径。 |
sAngle | 起始角,以弧度计。(弧的圆形的三点钟位置是 0 度)。 |
eAngle | 结束角,以弧度计。 |
counterclockwise | 可选。规定应该逆时针还是顺时针绘图。False = 顺时针(默认),true = 逆时针。 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>绘制圆形</title>
<style type="text/css">
.divContent {
display: inline-block;
width: 200px;
height: 200px;
background-color: #333333;
margin-left: 200px;
}
</style>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="module">
let drawArc1 = function () {
let canvas = document.getElementById("myCanvas");
canvas.width = $(".divContent").css("width").replace("px", "");
canvas.height = $(".divContent").css("height").replace("px", "");
let ctx = canvas.getContext("2d");
ctx.strokeStyle = "#fff";
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height / 2, 50, 0, 2 * Math.PI);
ctx.stroke();
};
let eAngle = 0;
let drawArc2 = function (canvas, ctx) {
canvas.width = $(".divContent").css("width").replace("px", "");
canvas.height = $(".divContent").css("height").replace("px", "");
ctx.strokeStyle = "#fff";
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height / 2, 50, 0, eAngle, true);
ctx.stroke();
eAngle = eAngle + 0.1;
if (eAngle >= 2 * Math.PI) {
eAngle = 0;
}
requestAnimationFrame(function () {
drawArc2(canvas, ctx);
});
};
$(function () {
drawArc1();
const canvas = document.getElementById("myCanvas2");
const ctx = canvas.getContext("2d");
drawArc2(canvas, ctx);
});
</script>
</head>
<body>
<div class="divContent">
<canvas id="myCanvas">
浏览器不支持 Canvas
</canvas>
</div>
<div class="divContent">
<canvas id="myCanvas2">
浏览器不支持 Canvas
</canvas>
</div>
</body>
</html>
7、 矩形
context.rect(x,y,width,height);
- | - |
---|
x | 矩形左上角的 x 坐标。 |
y | 矩形左上角的 y 坐标。 |
r | 圆的半径。 |
width | 矩形的宽度,以像素计,所以不需要,也不能再带单位)。 |
height | 矩形的高度,以像素计,所以不需要,也不能再带单位。 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>绘制矩形</title>
<style type="text/css">
.divContent {
display: inline-block;
width: 300px;
height: 300px;
background-color: #333333;
margin-left: 100px;
}
</style>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="module">
let drawRect1 = function (canvas, ctx) {
let colorArrays = [
"#FFCC66", "#CC00FF", "#996633",
"#3366CC", "#330000", "#FFFF66",
"#000099", "#FF3300", "#007700"
];
let index = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
ctx.beginPath();
ctx.lineWidth = "2";
ctx.strokeStyle = colorArrays[index++];
ctx.rect(j * canvas.width / 3,
i * canvas.height / 3,
canvas.width / 3,
canvas.height);
ctx.stroke();
}
}
};
let drawRect2 = function (canvas, ctx) {
let colorArrays = [
"#FFCC66", "#CC00FF", "#996633",
"#3366CC", "#330000", "#FFFF66",
"#000099", "#FF3300", "#007700"
];
let x = 0, y = 0;
while (x == y) {
x = Math.round(Math.random() * 8);
y = Math.round(Math.random() * 8);
}
let temp = colorArrays[x];
colorArrays[x] = colorArrays[y];
colorArrays[y] = temp;
let index = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
ctx.beginPath();
ctx.lineWidth = "2";
ctx.fillStyle = colorArrays[index++];
ctx.rect(j * canvas.width / 3,
i * canvas.height / 3,
canvas.width / 3,
canvas.height);
ctx.fill();
}
}
setTimeout(function () {
drawRect2(canvas, ctx);
}, 1000);
};
$(function () {
let canvas1 = document.getElementById("myCanvas1");
canvas1.width = $(".divContent").css("width").replace("px", "");
canvas1.height = $(".divContent").css("height").replace("px", "");
let ctx1 = canvas1.getContext("2d");
drawRect1(canvas1, ctx1);
let canvas2 = document.getElementById("myCanvas2");
canvas2.width = $(".divContent").css("width").replace("px", "");
canvas2.height = $(".divContent").css("height").replace("px", "");
let ctx2 = canvas2.getContext("2d");
drawRect2(canvas2, ctx2);
});
</script>
</head>
<body>
<div class="divContent">
<canvas id="myCanvas1">
浏览器不支持 Canvas
</canvas>
</div>
<div class="divContent">
<canvas id="myCanvas2">
浏览器不支持 Canvas
</canvas>
</div>
</body>
</html>