HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了HTML5 Canvas 填充与描边(Fill And Stroke)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@

HTML5 Canvas 填充与描边(Fill And stroke)

演示HTML5 Canvas Fill 与stroke文字效果,基于Canvas如何实

现纹理填充与描边。

一:颜色填充与描边

颜色填充可以通过fillStyle来实现,描边颜色可以通过strokeStyle来实现。简单示例

如下:

// fill and stroke text

ctx.font = '60pt Calibri';

ctx.lineWidth = 3;

ctx.strokeStyle = 'green';

ctx.strokeText('Hello World!',20,100);

ctx.fillStyle = 'red';

ctx.fillText('Hello World!',100);

二:纹理填充与描边

HTML5 Canvas还支持纹理填充,通过加载一张纹理图像,然后创建画笔模式,创建

纹理模式的API为ctx.createPattern(imageTexture,"repeat");第二参数支持四个

值,分别为”repeat-x”,”repeat-y”,”repeat”,”no-repeat”意思是纹理分别沿着

X轴,Y轴,XY方向沿重复或者不重复。纹理描边与填充的代码如下:

var woodfill = ctx.createPattern(imageTexture,"repeat");

ctx.strokeStyle = woodfill;

ctx.strokeText('Hello World!',200);

// fill rectangle

ctx.fillStyle = woodfill;

ctx.fillRect(60,240,260,440);

纹理图片

HTML5 Canvas 填充与描边(Fill And Stroke)


三:运行效果

HTML5 Canvas 填充与描边(Fill And Stroke)

代码

<!DOCTYPE html>
<html>
<head>
<Meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<Meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Canvas Fill And stroke Text Demo</title>
<link href="default.css" rel="stylesheet" />
	<script>
		var ctx = null; // global variable 2d context
		var imageTexture = null;
		window.onload = function() {
			var canvas = document.getElementById("text_canvas");
			console.log(canvas.parentNode.clientWidth);
			canvas.width = canvas.parentNode.clientWidth;
			canvas.height = canvas.parentNode.clientHeight;
			
			if (!canvas.getContext) {
			    console.log("Canvas not supported. Please install a HTML5 compatible browser.");
			    return;
			}
			
			// get 2D context of canvas and draw rectangel
			ctx = canvas.getContext("2d");
			ctx.fillStyle="black";
			ctx.fillRect(0,canvas.width,canvas.height);
			
			// fill and stroke text
			ctx.font = '60pt Calibri';
			ctx.lineWidth = 3;
			ctx.strokeStyle = 'green';
			ctx.strokeText('Hello World!',100);
			ctx.fillStyle = 'red';
			ctx.fillText('Hello World!',100);
			
			// fill and stroke by pattern
			imageTexture = document.createElement('img');
			imageTexture.src = "../pattern.png";
			imageTexture.onload = loaded();
		}
		
		function loaded() {
			// delay to image loaded
			setTimeout(textureFill,1000/30);
		}
		
		function textureFill() {
			// var woodfill = ctx.createPattern(imageTexture,"repeat-x");
			// var woodfill = ctx.createPattern(imageTexture,"repeat-y");
			// var woodfill = ctx.createPattern(imageTexture,"no-repeat");
			var woodfill = ctx.createPattern(imageTexture,"repeat");
			ctx.strokeStyle = woodfill;
			ctx.strokeText('Hello World!',200);
			
			// fill rectangle
			ctx.fillStyle = woodfill;
			ctx.fillRect(60,440);
		}
		
	</script>
</head>
<body>
	<h1>HTML5 Canvas Text Demo - By Gloomy Fish</h1>
	<pre>Fill And stroke</pre>
	<div id="my_painter">
		<canvas id="text_canvas"></canvas>
	</div>
</body>
</html>

大佬总结

以上是大佬教程为你收集整理的HTML5 Canvas 填充与描边(Fill And Stroke)全部内容,希望文章能够帮你解决HTML5 Canvas 填充与描边(Fill And Stroke)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: