当前位置:网站首页>JS绘制极坐标颜色渐变
JS绘制极坐标颜色渐变
2022-06-29 23:53:00 【 星云 】
SVG如何做到极坐标渐变
原生SVG是不支持这样渐变的。矢量绘图软件在作出这些渐变后也要在导出时将渐变转换为位图,内嵌在矢量图里,但是这样做要么不能解决放大缩小带来的失真问题,要么使得矢量图太大。那么如何用SVG作出这种渐变呢?
通常SVG渐变是这样做的:
<defs>
<linearGradient id="grad1" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#fff" stop-opacity="1"/>
<stop offset="100%" stop-color="#000" stop-opacity="1"/>
</linearGradient>
</defs>
表示从y=100%, x=0%开始到y=0%,x=100%结束中,0%处为颜色#fff,100%处为颜色#000;即左下角为白色,右上角为黑色。
整体SVG:
<svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg"><defs>
<linearGradient id="grad1" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#fff" stop-opacity="1"/>
<stop offset="100%" stop-color="#000" stop-opacity="1"/>
</linearGradient>
</defs>
<circle cx="50" cy="50" r="50" fill="url(#grad1)"/>
</svg>
但是这样只是得到一个圆形的线性渐变而不是我们想要的下面这样的渐变

如何做旋转渐变
因为不支持而做不到,那只能绕个弯子,关键是CSS也不支持……
办法倒也不是没有,在SVG中插入<foreignObject/>然后直接用JS在<canvas/>上画出渐变。画出来的话,占用空间更小一点而且分辨率没有太多限制,除非分辨率太高需要很久才能画完。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> canvas {
width: 200px; height: 200px; } </style>
</head>
<body>
<canvas id="canvas" width="100" height="100"></canvas>
</body>
<script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext("2d"); var w = canvas.width; var h = canvas.height; const CX = w / 2; const CY = h / 2; const OFFSET = 90;//旋转角度 const START = 90;//开始渐变角度 const RANGE = 180;//渐变角度 const ROUNT = 360;//周角 const WHITE = 255;//最亮颜色值 for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
var theta = (Math.atan2(y - CY, x - CX) + Math.PI) / (Math.PI * 2) * (ROUNT - 1); theta = (theta + OFFSET) % ROUNT + START theta = Math.max(Math.min(Math.floor(((ROUNT - 1) - theta) / RANGE * WHITE), WHITE), 0); var color = theta.toString(16); while (color.length < 2) {
color = '0' + color } color = '#000000' + color ctx.fillStyle = color; ctx.fillRect(x, y, 1, 1) } } </script>
</html>
边栏推荐
- MySQL functions and constraints
- Embedded development: Hardware in the loop testing
- Cacti settings for spin polling
- Construction of module 5 of actual combat Battalion
- Set up enterprise NTP time server
- 我想知道今天还可以开户么?另外想问,现在在线开户安全么?
- FPGA开发(2)——IIC通信
- [LeetCode] 只出现一次的数字【136】
- 6.28 problem solving
- Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
猜你喜欢

QPainter的使用入门:绘制象棋界面
500 error occurred after importing skins folder into solo blog skin

Koa2 learning and using

koa2学习和使用

Matplotlib histogram of Matplotlib visualization plt bar()

Andorid source build/envsetup.sh 该知道的细节
solo 博客皮肤导入 skins 文件夹后出现 500 错误

MySQL multi table query

Yunhe enmo Guoqiang, identifiez - le, saisissez - le, avant l'ébullition de la base de données nationale

Sword finger offer 14- ii Cutting rope II
随机推荐
Provide effective performance evaluation 
Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
QPainter的使用入门:绘制象棋界面
云服务器的安全设置常识
Leetcode (633) -- sum of squares
Analysis of define incdir command in TCL script of Modelsim
MySQL functions and constraints
golang7_ TCP programming
vlog常用参数解析
Is China Merchants Securities reliable? Is it safe to open a stock account?
matlab习题 —— 程序控制流程练习
打造一个 API 快速开发平台,牛逼!
QT learning 03 birth and essence of QT
Common knowledge of ECS security settings
Leetcode (76) -- Minimum Covering substring
Web APIs 环境对象 丨黑马程序员
Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
Xutils3 transfer set
High performance and high availability computing architecture of "microblog comments" in microblog system