canvas设置渐变色文字(线性、径向)

本文详细介绍了如何在HTML5canvas中使用createLinearGradient和createRadialGradient创建线性渐变和径向渐变,并展示了如何将这些渐变应用于文字填充和描边。提供了实例代码,包括设置渐变色的文字显示和清除画布功能。
摘要由CSDN通过智能技术生成

在这里插入图片描述

查看专栏目录

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

如何使用canvas设置渐变色文字呢?这里分为设置线性渐变(createLinearGradient)和径向渐变(createRadialGradient)。请使用该对象作为 strokeStyle 或 fillStyle 属性的值。

线性渐变语法及参数

context.createLinearGradient(x0,y0,x1,y1);

x0 渐变开始点的 x 坐标
y0 渐变开始点的 y 坐标
x1 渐变结束点的 x 坐标
y1 渐变结束点的 y 坐标

径向渐变语法及参数

context.createRadialGradient(x0,y0,r0,x1,y1,r1);

x0 渐变的开始圆的 x 坐标
y0 渐变的开始圆的 y 坐标
r0 开始圆的半径
x1 渐变的结束圆的 x 坐标
y1 渐变的结束圆的 y 坐标
r1 结束圆的半径

示例效果图

在这里插入图片描述

示例源代码(共124行)

/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-01-13
*/
<template>
	<div class="djs_container">
		<div class="top">
			<h3>canvas设置渐变色文字(线性、径向)</h3>
			<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
			<h4>
				<el-button type="primary" size="mini" @click="draw1()">线性渐变背景</el-button>
				<el-button type="primary" size="mini" @click="draw2()">线性渐变文字</el-button>
				<el-button type="success" size="mini" @click="draw3()">径向渐变背景</el-button>
				<el-button type="success" size="mini" @click="draw4()">径向渐变文字</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");
			},
			draw1() {
				this.clearCanvas()
				let gradient = this.ctx.createLinearGradient(0, 0, this.canvas.width, this.canvas.height);
				gradient.addColorStop(0,'blue');
				gradient.addColorStop(0.25,'yellow');
				gradient.addColorStop(0.5, 'white');
				gradient.addColorStop(0.75, 'red');
				gradient.addColorStop(1.0,'green');
				this.ctx.fillStyle = gradient;
				this.ctx.fillRect(0,0, this.canvas.width,this.canvas.height); 
			},
			draw2() {
				this.clearCanvas()
				let gradient = this.ctx.createLinearGradient(0, 0, this.canvas.width, this.canvas.height);
				gradient.addColorStop(0,'blue');
				gradient.addColorStop(0.25,'yellow');
				gradient.addColorStop(0.5, 'white');
				gradient.addColorStop(0.75, 'red');
				gradient.addColorStop(1.0,'green');
				this.ctx.fillStyle = gradient;
				this.ctx.font = '80px STheiti, SimHei';
				this.ctx.fillText('还是大剑师兰特', 224, 266);
				
			},
			draw3() {
				this.clearCanvas()
				let gradient = this.ctx.createRadialGradient(490, 245, 20, 490, 245, 200);
				gradient.addColorStop(0,'blue');
				gradient.addColorStop(0.25,'yellow');
				gradient.addColorStop(0.5, 'white');
				gradient.addColorStop(0.75, 'red');
				gradient.addColorStop(1.0,'green');
				this.ctx.fillStyle = gradient;
				this.ctx.fillRect(0,0, this.canvas.width,this.canvas.height); 
			},
			draw4() {
				this.clearCanvas()
				let gradient = this.ctx.createRadialGradient(490, 245, 20, 490, 245, 200);
				gradient.addColorStop(0,'blue');
				gradient.addColorStop(0.25,'yellow');
				gradient.addColorStop(0.5, 'white');
				gradient.addColorStop(0.75, 'red');
				gradient.addColorStop(1.0,'green');
				this.ctx.fillStyle = gradient;
				this.ctx.font = '80px STheiti, SimHei';
				this.ctx.fillText('还是大剑师兰特', 224, 266);
			},

		}
	}
</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基本属性

属性属性属性
canvasfillStylefilter
fontglobalAlphaglobalCompositeOperation
heightlineCaplineDashOffset
lineJoinlineWidthmiterLimit
shadowBlurshadowColorshadowOffsetX
shadowOffsetYstrokeStyletextAlign
textBaselinewidth

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()
  • 57
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
要在Canvas上绘制一个圆形并应用渐变色,可以按照以下步骤进行操作: 1. 获取Canvas的上下文:首先,获取Canvas元素的上下文。可以使用2D上下文(`getContext('2d')`)来进行2D绘图操作。例如: ```javascript const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); ``` 2. 创建渐变对象:使用Canvas上下文提供的方法创建一个渐变对象。可以使用`createRadialGradient`或`createLinearGradient`方法来创建渐变对象,具体取决于你想要使用的渐变类型。例如,以下代码创建一个径向渐变对象: ```javascript const gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius); ``` 3. 添加渐变色停止点:通过使用渐变对象的`addColorStop`方法,可以指定渐变的颜色和位置。例如,以下代码将添加两个颜色停止点,实现从红色到蓝色的渐变: ```javascript gradient.addColorStop(0, 'red'); gradient.addColorStop(1, 'blue'); ``` 4. 绘制圆形并应用渐变色:使用Canvas上下文提供的方法来绘制圆形,并将渐变对象应用于填充颜色。例如,以下代码将在Canvas上绘制一个圆形,并应用上述创建的渐变对象作为填充颜色: ```javascript ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); ctx.fillStyle = gradient; ctx.fill(); ctx.closePath(); ``` 通过以上步骤,就可以在Canvas上成功绘制一个圆形,并应用渐变色作为填充颜色。根据实际需求,可以设置不同的渐变类型、颜色和位置来实现不同的渐变效果。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是大剑师兰特

打赏一杯可口可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值