canvas绘制图形

canvas绘制图形

简单了解canvas

  1. 创建一个canvas画布
  2. 引入绘画脚本
  3. 使用draw函数进行绘画

index.html


<html>
  <head lang="en">
    <meta charset="utf-8"/>
	<title>canvas绘制矩形</title>
	<script type="text/javascript" src="canvas.js"></script>//引入绘画脚本
	<style type="text/css">
	    body{
		   margin:0;
		   padding:0;
		}
	</style>
  </head>
  <body onload="draw('canvas')">//使用draw函数进行绘画
    <canvas id="canvas" width="400" height="300"></canvas>//创建画布
  </body>
</html>

绘制矩形

效果图如下:在这里插入图片描述

  1. 获取canvas元素
    document.getElementById();
  2. 取得上下文
    canvas.getContext(‘2d’);
  3. 填充与绘画边框
  4. 设置绘制样式
    context. fillStyle,context.strokeStyle
  5. 指定画笔宽度
    context.lineWidth
  6. 设置颜色值
  7. 绘制矩形
    context.fillRect(x,y,width,height);
    context.fillRect(x,y,width,height);

canvas.js

function draw(id){
	var canvas = document.getElementById(id);//获取canvas元素
	var context = canvas.getContext('2d');//取得上下文
	context.fillStyle = "blue";//填充样式
	context.strokeStyle = "red";//边框样式
	context.lineWidth = 5;//画笔宽度
	context.fillRect(0,0,400,300);//填充context.fillRect(x,y,width,height)
	context.strokeRect(50,50,180,120);//边框
}

绘制圆形

效果图:
在这里插入图片描述

  1. 开始创建路径
    context.beginPath();
  2. 创建圆形路径
    context.arc(x,y,radius,startAngle,endAngle,anticlockwise);
    startAngle开始角度,endAngle结束角度,anticlockwise是否按顺时针方向绘制
    角度换成弧度方法:
    var radius = degree*Math.PI/180;
    其中Math.PI为180°
  3. 创建完成关闭路径
    context.closepath();
  4. 设置绘制样式然后调用绘制方法进行绘制
    context.fillSyle=‘rgba(255,0,0,0.25)’;
    context.fill();

intex.html


<html>
  <head lang="en">
    <meta charset="utf-8"/>
	<title>canvas绘制圆形</title>
	<script type="text/javascript" src="canvas.js"></script>
	<style type="text/css">
	    body{
		   margin:0;
		   padding:0;
		}
	</style>
  </head>
  <body onload="draw('canvas')">
    <canvas id="canvas" width="400" height="400"></canvas>
  </body>
</html>

canvas.js

function draw(id){
	var canvas = document.getElementById(id);
	var context = canvas.getContext('2d');
	context.fillStyle = "yellow";
	context.fillRect(0,0,400,400);
	for(var i=0;i<10;i++){
		context.beginPath();
		context.arc(i*25,i*26,i*10,0,Math.PI*2,true);
		context.closePath();
		context.fillStyle = "rgba(255,0,0,0.25)";
		context.fill();
	}
}

绘制文字

效果图:
在这里插入图片描述
fillText方法用填充的方式绘制字符串:
context.fillText(text,x,y,[maxwidth]);
strokeText方法用轮廓的方法绘制字符串:
stroke.fillText(text,x,y,[maxwidth]);

  1. 设置文字字体
    context.font=“font-size font-family”;
  2. 设置文字垂直对齐方式
    context.textBaseline=‘alphabetic’;
  3. 设置文字水平对齐方式
    context.textAlign=‘start’;

canvas.js

function draw(id) {
    var canvas = document.getElementById(id);
    var context = canvas.getContext("2d");
    context.fillStyle = "#CA0C16";
    context.fillRect(0,0,800,300);
    context.font="40px 微软雅黑";
    context.fillStyle = 'white';
    context.textBaseline ='hanging';
    context.textAlign ='start';
    context.fillText('欢迎观看',150,20);
    context.fillText('uncle_huang',190,80);
    context.fillText('CSDN博客',400,150);
}

保存文件

canvas.toDataURL(type);`

window.location - canvas.toDataURL('image/png');

绘制动画

效果图:
在这里插入图片描述
设置动画的间隔时间:
setInterval(code,millisec);

canvas.js

var i;
var context;
function draw(id) {
    var canvas = document.getElementById(id);
    context = canvas.getContext("2d");

    setInterval(painting,100);
    i=0;
}

function painting() {
    context.fillStyle = "#CA0C16";
    context.fillRect(i,0,10 ,10);
    i+=20;
}

擦除图像的方法:
context.clearRect(x,y,width,height);

canvas.js


var i;
var context;
var width,height;
function draw(id) {
    var canvas = document.getElementById(id);
    context = canvas.getContext("2d");
    width=canvas.width;
    height=canvas.height;

    setInterval(painting,100);
    i=0;
}

function painting() {
    context.fillStyle = "white";
    context.clearRect(0,0,width,height)
    context.fillStyle = "#CA0C16";
    context.fillRect(i,20,10,10)
    i+=20;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_Huang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值