H5 canvas 测试

 本测试包括在画布上画矩形,线,圆,以及颜色的渐变,实心,空心字,引入图片等。

效果:

155216_7J6Y_2345870.png

代码

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>canvasTest</title>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
window.onload=function(){
	can01();
	can02();
}

function can01(){
	var c=$("#can01")[0];
	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(0,0,200,100);
	//线条
	ctx.moveTo(0,0);
	ctx.lineTo(400,200);
	ctx.stroke();
	//圆
	ctx.beginPath();
	ctx.arc(95,50,40,0,2*Math.PI);
	ctx.stroke();
	//文本
	ctx.font="30px Arial";
	ctx.fillText("Hello World",10,150);
	ctx.strokeText("Hello World",10,100);
}
function can02(){
	var c=$("#can02")[0];
	var ctx=c.getContext("2d");
	// 创建渐变
	var img = new Image();
    img.src = "image.jpg";
	img.onload = function() {
		ctx.drawImage(img,10,10);
	} 
}
</script>
<style>
.can{
width:400px;
height:200px;
border:1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="can01" class="can"></canvas>
<canvas id="can02" class="can"></canvas>
</body>
</html>

 

转载于:https://my.oschina.net/519617/blog/903678

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值