HTML5之canvas 9绘制图片

Posted 李大白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML5之canvas 9绘制图片相关的知识,希望对你有一定的参考价值。

绘制图片

Var image=new Image();

image.src=” http://img4.duitang.com/uploads/item/201406/25/20140625182321_4MTau.thumb.700_0.jpeg”;

image.onload=function(){}

Context.drawImage(image,x,y);

Context.drawImage(image,x,y,w,h);

Context.drawIamge(image,sx,sy,sw,sh,dx,dy,dw,dh);

图片平铺

Var pat= context.createPattern(image,”repeat”);

Context.fillStyle=pat;

Context.fillRect(0,0,400,300);

图片裁剪

 

绘制好路径

 

Context.clip();

绘制图片代码

<html>

	<head>
		<meta charset="UTF-8">
		<title>绘制图片</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

	</head>

	<body>
		<canvas id="canvas" width="500" height="500"></canvas>
		
		<script type="text/javascript">
			var oCanvas = document.getElementById("canvas");
			var context = oCanvas.getContext("2d");
			context.fillStyle = "#ededed";
			context.fillRect(0, 0, 500, 500);

			//绘制图片
			var img = new Image(); //创建
			img.src = "img/01.jpg"; //图片地址
			img.onload = function() { //检测所有图像信息载入页面里
				context.drawImage(img, 0, 0); // img对象;0,0:img坐标起点
			};
		</script>
	</body>

</html>

  

 图片平铺代码

 1 <!DOCTYPE html>
 2 <html>
 3 
 4     <head>
 5         <meta charset="UTF-8">
 6         <title>图片平铺</title>
 7         <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
 8 
 9     </head>
10 
11     <body>
12         <canvas id="canvas" width="500" height="500"></canvas>
13         <script type="text/javascript">
14             var oCanvas = document.getElementById("canvas");
15             var context = oCanvas.getContext("2d");
16             context.fillStyle = "#ededed";
17             context.fillRect(0, 0, 500, 500);
18             //平铺
19 
20             var img = new Image(); //创建
21             img.src = "01.jpg"; //图片地址
22             img.onload = function() {
23                 var pat = context.createPattern(img, "repeat");
24                 context.fillStyle = pat;
25                 context.fillRect(0, 0, 500, 500);
26             }
27         </script>
28     </body>
29 
30 </html>

图片裁剪代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>图片裁剪</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		
	</head>
	<body>
		<canvas id="canvas" width="500" height="500"></canvas>
		<script type="text/javascript">
			var oCanvas = document.getElementById("canvas");
			var context = oCanvas.getContext("2d");
			context.fillStyle = "#ededed";
			context.fillRect(0, 0, 500, 500);
			
			
			var img = new Image(); //创建
			img.src = "04.jpg"; //图片地址
			img.onload = function() { //检测所有图像信息载入页面里
//				context.arc(100,100,60,0,2*Math.PI); //画圆
				context.fillRect(100,100,40,40);
				context.strokeRect(100,100,40,40);
				context.clip();//裁
				context.drawImage(img,40,40);//
			};
		</script>
	</body>
</html>

  

以上是关于HTML5之canvas 9绘制图片的主要内容,如果未能解决你的问题,请参考以下文章

HTML5自学笔记[20 ]canvas绘图实例之绘制倒影

html5 两个canvas重叠放在一个div里面,每个canvas各填充一张图片,代码怎么写?

html5 canvas+原生javascript 实时获取文本框内容绘制图片水印

HTML5用canvas怎么实现动画效果

HTML5 Canvas 支持和 Android Webview

HTML5 中的 canvas 画布