1.创建一个画布(Canvas)
一个画布在网页中是一个矩形框,通过 canvas 元素来绘制.
注意: 默认情况下 canvas 元素没有边框和内容。
canvas简单实例如下:
<canvas id="myCanvas" width="200" height="100"></canvas>
2.使用 JavaScript 来绘制图像
canvas 元素本身是没有绘图能力的。所有的绘制工作必须在 JavaScript 内部完成:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
实例解析:
首先,找到 canvas 元素:
var c=document.getElementById("myCanvas");
然后,创建 context 对象:
var ctx=c.getContext("2d");
getContext(“2d”) 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
下面的两行代码绘制一个红色的矩形:
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
设置fillStyle属性可以是CSS颜色,渐变,或图案。fillStyle 默认设置是#000000(黑色)。
fillRect(x,y,width,height)
方法定义了矩形当前的填充方式。
Canvas 坐标
canvas 是一个二维网格。
canvas 的左上角坐标为 (0,0)
上面的 fillRect 方法拥有参数 (0,0,150,75)。
意思是:在画布上绘制 150x75 的矩形,从左上角开始 (0,0)。
Canvas - 路径
在Canvas上画线,我们将使用以下两种方法:
moveTo(x,y)
定义线条开始坐标
lineTo(x,y)
定义线条结束坐标
绘制线条我们必须使用到 “ink” 的方法,就像stroke().
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();
在canvas中绘制圆形, 我们将使用以下方法:
arc(x,y,r,start,stop)
实际上我们在绘制圆形时使用了 “ink” 的方法, 比如 stroke() 或者 fill().
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
Canvas - 文本
使用 canvas 绘制文本,重要的属性和方法如下:
font - 定义字体
fillText(text,x,y)
- 在 canvas 上绘制实心的文本
strokeText(text,x,y)
- 在 canvas 上绘制空心的文本
使用 fillText()
:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("Hello World",10,50);
使用 strokeText()
:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.strokeText("Hello World",10,50);
Canvas - 渐变
渐变可以填充在矩形, 圆形, 线条, 文本等等, 各种形状可以自己定义不同的颜色。
以下有两种不同的方式来设置Canvas渐变:
createLinearGradient(x,y,x1,y1)
- 创建线条渐变
createRadialGradient(x,y,r,x1,y1,r1)
- 创建一个径向/圆渐变
当我们使用渐变对象,必须使用两种或两种以上的停止颜色。
addColorStop()
方法指定颜色停止,参数使用坐标来描述,可以是0至1.
使用渐变,设置fillStyle或strokeStyle的值为 渐变,然后绘制形状,如矩形,文本,或一条线。
使用 createLinearGradient()
:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// 创建渐变
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// 填充渐变
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
使用 createRadialGradient()
:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// 创建渐变
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// 填充渐变
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
Canvas - 图像
把一幅图像放置到画布上, 使用以下方法:
PS: 不要在样式中定义 <canvas>
的宽和高,否则,里面所画的图片会自动放大或者缩小。
drawImage(image,x,y)
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("scream");
ctx.drawImage(img,10,10);
Canvas Example
const [width, height, random] = [this.width, this.height, this.random];
let [dataArr, start, progress, progressFlag] = [[], null, null, true];
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, width, height); // 清空画布
for (let i = 0; i < 20; i++) {
this.leftFlag = i % 2 === 0 ? true : false
const [x, y, color, size, lineWidth] = [random(width / 4, this.leftFlag), random(height), this.color[random(4)], this.size[random(4)], random(10)]
dataArr.push([x, y, color, size, lineWidth])
}
const move = (step) => {
dataArr.map(item => {
let [x, y, color, size, lineWidth] = item
size += step
ctx.save()
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + size, y);
ctx.lineTo(parseInt(x + size * 1.5), parseInt(y + (size * Math.sqrt(3)) / 2));
ctx.lineTo(x + size, parseInt(y + size * Math.sqrt(3)));
ctx.lineTo(x, parseInt(y + size * Math.sqrt(3)));
ctx.lineTo(parseInt(x - size * 0.5), parseInt(y + (size * Math.sqrt(3)) / 2));
ctx.closePath();
ctx.lineJoin = "round";
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.stroke();
ctx.restore()
})
}
// 对 move 通过动画传入 step
确定鼠标是否在某个图形中
在 canvas 中提供了一个方法能够检测某个点(坐标)是否在图形中,那就是:isPointInPath,它的参数是坐标点的 x 和 y 值(对应的在多边形内可用 isPointInStroke 检测)
注意只能检测最后绘制的图形
function isMouseInGraph(mouse){
context.beginPath();
context.rect(shape.x, shape.y, shape.w, shape.h);
return context.isPointInPath(mouse.x, mouse.y);
}