canvas画图

使用canvas画图的步骤:

一:在body中定义画板<canvas></canvas>

二:在css中设置画板样式

三:在js中获取画板对象

四:利用画板对象画图(文本,线,圆,矩形,渐变)

1,画矩形

<head>
<style type="text/css">
#juxing {
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="juxing">1矩形</canvas>
<script type="text/javascript">
var ju=document.getElementById("juxing");
var jutx=ju.getContext("2d");//创建 context 对象:getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
    jutx.fillStyle="#CCHH";//设置fillStyle属性可以是CSS颜色,渐变,或图案。fillStyle 默认设置是#000000(黑色)。
    jutx.fillRect(50,30,100,50);//fillRect(x,y,width,height) 方法定义了矩形当前的填充方式。
</script>
</body>

2,画 

<head>
<style type="text/css">
#myCanvas {
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="myCanvas">2圆</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
    ctx.beginPath();
    ctx.arc(95,50,40,0,2*Math.PI);//arc(x,y,r,start,stop)
    ctx.stroke();
</script> 
</body>

 3,画线条

<head>
<style type="text/css">
#line{
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="line">3线条</canvas>
<script type="text/javascript">
var line=document.getElementById("line");
var linetx=line.getContext("2d");
    linetx.moveTo(0,0);//moveTo(x,y) 定义线条开始坐标
    linetx.lineTo(400,200);//lineTo(x,y) 定义线条结束坐标
    linetx.stroke();//定义开始坐标(0,0), 和结束坐标 (400,200). 然后使用 stroke() 方法来绘制线条:
</script>
</body>

 4,画线性渐变

<head>
<style type="text/css">
#lineStr{
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="lineStr">4线性渐变</canvas>
<script type="text/javascript">
var linestr=document.getElementById("lineStr");
var linersTx=linestr.getContext("2d");

//Create gradient
var gray=linersTx.createLinearGradient(0,0,200,10);//createLinearGradient(x,y,x1,y1) 创建线条渐变
    gray.addColorStop(0,"red");//addColorStop()方法指定颜色停止,参数使用坐标来描述,可以是0至1.
    gray.addColorStop(1,"black");
    
    // Fill with gradient    
    linersTx.fillStyle=gray;
    linersTx.fillRect(10,10,200,100);
</script>
</body>

5,画景象/圆渐变

<head>
<style type="text/css">
#radiaStr{
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="radiaStr">5景象/圆渐变</canvas>
<script type="text/javascript">
var c1=document.getElementById("radiaStr");
var ctx1=c1.getContext("2d");

// Create gradient
var grd1=ctx1.createRadialGradient(75,50,5,90,60,100);
    grd1.addColorStop(0,"red");
    grd1.addColorStop(1,"white");

// Fill with gradient
    ctx1.fillStyle=grd1;
    ctx1.fillRect(10,10,150,80);
</script>
</body>

6,显示图片

<head>
<style type="text/css">
#imgc{
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="imgc">6图片</canvas>
<img src="../imags/jiazai.png" id="userimg"/>
<script type="text/javascript">
var ic=document.getElementById("imgc");
var icT=ic.getContext("2d");//把一幅图像放置到画布上, 使用以下方法:drawImage(image,x,y)
var img=document.getElementById("userimg");
    icT.drawImage(img,30,20);//drawImage(image,x,y)
    
</script>
</body>

7,文本

<head>
<style type="text/css">
#fontT{
    with: 15rem;
    height: 5rem;
    border: 0.1rem solid blue;
}
</style>
</head>
<body>
<canvas id="fontT">7文本</canvas>
<script type="text/javascript">
var cF=document.getElementById("fontT");
var cFT=cF.getContext("2d");
    cFT.font="5rem Arial";//font  定义字体ctx.font="30px Arial";
    cFT.fillText("Natalie",30,100);//fillText(text,x,y) - 在 canvas 上绘制实心的文本
    cFT.strokeText("Natalie",20,80);//strokeText(text,x,y) - 在 canvas 上绘制空心的文本
</script>
</body>
原文地址:https://www.cnblogs.com/yinyl/p/8027095.html