canvas设置空心文字

在这里插入图片描述

查看专栏目录

canvas示例教程100+专栏,提供canvas的基础知识,高级动画,相关应用扩展等信息。canvas作为html的一部分,是图像图标地图可视化的一个重要的基础,学好了canvas,在其他的一些应用上将会起到非常重要的帮助。

如何使用canvas设置空心文字呢?canvas内置了一个设置空心文字的方法,使用ctx.strokeText即可。在之前的文章中,我们谈到设置实心的文字。 文字的填充色使用fillStyle,文字的边框颜色使用strokeStyle。文字的大小和文字使用ctx.font来做设置。

语法:

context.strokeText(text, x, y [, maxWidth]);

参数:

各个参数含义和作用如下:

text String
用来填充的文本信息。
x Number
填充文本的起点横坐标。
y Number
填充文本的起点纵坐标。
maxWidth(可选)Number
填充文本占据的最大宽度,当文本占据宽度超过此最大宽度时候,通过压缩每个文本宽度进行适配,而非换行。

下面是大剑师的一个示例,供参考:

示例效果图

在这里插入图片描述

示例源代码(共83行)

/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: [email protected]
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-01-09
*/
<template>
	<div class="djs_container">
		<div class="top">
			<h3>canvas设置空心文字</h3>
			<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
			<h4>
				<el-button type="primary" size="mini" @click="draw()">绘制</el-button>
				<el-button type="danger" size="mini" @click="clearCanvas()">清除</el-button>
			</h4>
		</div>
		<div class="dajianshi ">
			<canvas id="dajianshi" ref="mycanvas" width="980" height="490"></canvas>
		</div>

	</div>
</template>
<script>
	export default {
    
    
		data() {
    
    
			return {
    
    
				ctx: null,
				canvas: null,
			}
		},
		mounted() {
    
    
			this.setCanvas()
		},
		methods: {
    
    
			clearCanvas(){
    
    
				this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
			},
						
			setCanvas() {
    
    
				this.canvas = document.getElementById('dajianshi');
				if (!this.canvas.getContext) return;
				this.ctx = this.canvas.getContext("2d");
			},
			draw() {
    
    
				this.ctx.strokeStyle='red';
				this.ctx.font = '80px STheiti, SimHei';
				this.ctx.strokeText('基本的空心文字', 224, 166);
				
				this.ctx.strokeStyle='green';
				this.ctx.strokeText('含最大长度的空心文字', 524, 366,200);
	
			},

		}
	}
</script>

<style scoped>
	.djs_container {
    
    
		width: 1000px;
		height: 680px;
		margin: 50px auto;
		border: 1px solid #994170;
		position: relative;
	}

	.top {
    
    
		margin: 0 auto 0px;
		padding: 10px 0;
		background: #994170;
		color: #fff;
	}

	.dajianshi {
    
    
		margin: 5px auto 0;
		border: 1px solid #ccc;
		width: 980px;
		height: 490px;
		background-color: #f9f9f9;
	}
</style>



canvas基本属性

属性 属性 属性
canvas fillStyle filter
font globalAlpha globalCompositeOperation
height lineCap lineDashOffset
lineJoin lineWidth miterLimit
shadowBlur shadowColor shadowOffsetX
shadowOffsetY strokeStyle textAlign
textBaseline width

canvas基础方法

方法 方法 方法
arc() arcTo() addColorStop()
beginPath() bezierCurveTo() clearRect()
clip() close() closePath()
createImageData() createLinearGradient() createPattern()
createRadialGradient() drawFocusIfNeeded() drawImage()
ellipse() fill() fillRect()
fillText() getImageData() getLineDash()
isPointInPath() isPointInStroke() lineTo()
measureText() moveTo() putImageData()
quadraticCurveTo() rect() restore()
rotate() save() scale()
setLineDash() setTransform() stroke()
strokeRect() strokeText() transform()
translate()

猜你喜欢

转载自blog.csdn.net/cuclife/article/details/135434278