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

在这里插入图片描述

查看专栏目录

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

文章目录

    • 线性渐变语法及参数
    • 径向渐变语法及参数
    • 示例效果图
    • 示例源代码(共124行)
    • canvas基本属性
    • 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()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/344773.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【JAVA】谈谈 ReadWriteLock 和 StampedLock

&#x1f34e;个人博客&#xff1a;个人主页 &#x1f3c6;个人专栏&#xff1a;JAVA ⛳️ 功不唐捐&#xff0c;玉汝于成 目录 前言 正文 ReadWriteLock&#xff08;读写锁&#xff09; 基本原理&#xff1a; 接口和实现&#xff1a; 用法示例&#xff1a; StampedL…

统信UOS_麒麟KYLINOS与Windows通过Open SSH实现文件传输

原文链接&#xff1a;统信UOS/麒麟KYLINOS与Windows通过Open SSH实现文件传输 hello&#xff0c;大家好啊&#xff01;今天我要给大家介绍的是在统信UOS或麒麟KYLINOS操作系统与Windows系统之间通过Open SSH实现文件传输的方法。在日常工作中&#xff0c;我们经常需要在不同操作…

使用Pygame库来显示一个简单的窗口,并绘制一些基本的形状和文本

import pygame from pygame.locals import *# 初始化pygame库 pygame.init()# 创建窗口并设置大小和标题 screen_width 800 screen_height 600 screen pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("My Pygame")# 定义颜色…

leetcode 2645. 构造有效字符串的最少插入数-python

题目&#xff1a; 给你一个字符串 word &#xff0c;你可以向其中任何位置插入 “a”、“b” 或 “c” 任意次&#xff0c;返回使 word 有效 需要插入的最少字母数。 如果字符串可以由 “abc” 串联多次得到&#xff0c;则认为该字符串 有效 。 解题方法 1.先判断字符串是否…

七麦数据js逆向(补环境版)

本文目标地址如下&#xff0c;使用base64解码获得 aHR0cHM6Ly93d3cucWltYWkuY24vcmFuay9tYXJrZXRSYW5rL21hcmtldC82L2NhdGVnb3J5LzUvY29sbGVjdGlvbi9hbGwvZGF0ZS8yMDI0LTAxLTEy 本文逆向破解分为扣代码版和补环境版&#xff0c;扣代码版请看专栏另一篇文章 废话不多说了&#…

电脑上不安装Oracle,但是虚拟机装了Oracle,怎么连接到虚拟机里的Oracle数据库呢?

1、准备工作 1.1、确定数据库版本信息 注&#xff1a;如果知道数据库的版本信息&#xff0c;这个步骤可以跳过。 比较简单的方法&#xff0c;直接看数据库的安装位置&#xff0c;也就是数字&#xff08;但是这个方法确定就是&#xff0c;不好确定是多少位的数据库&#xff09;…

python 语法

闭包 在函数嵌套的前提下&#xff0c;内部函数使用了外部函数的变量&#xff0c;并且外部函数返回了内部函数&#xff0c;我们把这个使用外部函数变量的内部函数称为闭包。 def outfunc(arg):def innerFunc(msg):print(f"<{msg}> {arg} <{msg}>")retu…

Typora上传图片失败PicGo

起初我是在Typora中点击一键上传图片&#xff0c;结果如下&#xff0c;报错。可是我找了半天也没错啊。 最后发现原来是一个图片的命名问题&#xff0c;名字太过于复杂&#xff0c;PicGo识别不出&#xff0c;一个图片报错导致其它也上传不了。 我把它复制到其它文件夹之后&…

掌握 Vue 响应式系统,让数据驱动视图(下)

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

使用Pygame库创建了一个窗口,并在窗口中加载了一个名为“ball.png“的图片,通过不断改变物体的位置,实现了一个简单的动画效果

import pygame import sys# 初始化Pygame pygame.init()# 创建窗口 screen pygame.display.set_mode((640, 480))# 加载图片 image pygame.image.load("ball.png")# 将物体初始位置设为屏幕左上角 x 0 y 0# 游戏循环 while True:# 处理事件for event in pygame.e…

Sip - Ubuntu 配置 miniSIPServer 服务器(测试用)

客户提供的账号过期了&#xff0c;简单搭建 SIP 服务器&#xff0c;以便测试使用。个人认为这个配置起来最为简单&#xff0c;且测试功能足够。 官网miniSIPServer - 基于 Windows 以及 Linux 平台的 VoIP (SIP) 服务器软件. miniSIPServer 可能是最容易使用的 VoIP(SIP) 服务器…

【Git】本地仓库文件的创建、修改和删除

目录 一、基本信息设置 1、设置用户名2、设置用户名邮箱 二、Git仓库操作介绍 1、创建一个新的文件夹2、在文件内初始化git仓库&#xff08;创建git仓库&#xff09;3、向仓库中添加文件 1.创建一个文件2.将文件添加到暂存区3.将暂存区添加到仓库 4、修改仓库文件 1.修改文件2.…