HTML5 Canvas

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			#myCanvas {
				background-image: url(img/map.png);
			}
		</style>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript">
			var img; //规定要使用的图像、画布或视频。
			var x = 150; //在画布上放置图像的 x 坐标位置。
			var y = 150; //在画布上放置图像的 y 坐标位置。
			var width = 33; //可选。要使用的图像的宽度。(伸展或缩小图像)
			var height = 24; //可选。要使用的图像的高度。(伸展或缩小图像)
			$(function() {
				myCanvas = $('#myCanvas')[0];
				myContext = myCanvas.getContext('2d');
				//添加图片
				img = new Image();
				img.src = 'img/house.png';
				myContext.drawImage(
					img, x, y, width, height
				);
				//画填充长方形
				myContext.fillStyle = "#FF0000"; //填充红色
				myContext.fillRect(200, 220, 30, 20); //意思是:在画布上绘制 30x20 的矩形,从左上角开始 (200,220)。
				//画空心长方形
				myContext.strokeStyle = '#FF0000'
				myContext.lineWidth = 1;
				myContext.strokeRect(200, 250, 30, 20)
				//画圆
				myContext.fillStyle = "#00FF00"; //填充绿色
				myContext.beginPath();
				myContext.arc(300, 250, 20, 0, 2 * Math.PI); //意思是:中心:300*250,半径:20,起始角:0,结束角:2 * Math.PI
				myContext.closePath();
				myContext.stroke(); //画圆边框
				myContext.fill(); //填充圆
				//画线
				myContext.moveTo(10, 10);
				myContext.lineTo(150, 50);
				myContext.lineTo(10, 50);
				myContext.stroke();
				//写字
				myContext.fillStyle = '#000000';
				myContext.font = "16px Arial";
				myContext.fillText("Hello World", 200, 50);
			});
		</script>
	</head>

	<body>
		<canvas id="myCanvas" width="600" height="400">这个浏览器不支持HTML5元素。</canvas>
		<img src="img/house.png" style="display: none;" />
	</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值