当前位置:网站首页>JS draw polar color gradient
JS draw polar color gradient
2022-06-30 00:09:00 【Nebula】
SVG How to do polar coordinate gradient
Native SVG This gradient is not supported . After making these gradients, vector graphics software should also convert gradients into bitmaps when exporting , Embedded in the vector diagram , But doing so may not solve the distortion problem caused by zooming in and out , Or make the vector graph too large . So how to use SVG Make this gradient ?
Usually SVG Gradients do this :
<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>
From y=100%, x=0% Start to y=0%,x=100% At the end ,0% Color at #fff,100% Color at #000; That is, the lower left corner is white , The upper right corner is black .
whole 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>
But this is just a circular linear gradient, not We want the following gradient

How to do rotation gradient
Can't do it because it doesn't support , That's only a detour , The key is CSS Nor does it support ……
It's not that there are no solutions , stay SVG Insert <foreignObject/> And use it directly JS stay <canvas/> Draw a gradient on the . If you draw it , It takes up less space and has no too many restrictions on the resolution , Unless the resolution is too high, it will take a long time to finish .
<!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;// Rotation Angle const START = 90;// Start gradient angle const RANGE = 180;// Gradient angle const ROUNT = 360;// Around the corner const WHITE = 255;// Brightest color value 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>
边栏推荐
- Solr basic operation 10
- MySQL:SQL概述及数据库系统介绍 | 黑马程序员
- Construction of module 5 of actual combat Battalion
- Web APIs environment object - dark horse programmer
- QT learning 03 birth and essence of QT
- HPE launched ARM CPU general server product
- [Shangshui Shuo series] day 8
- Solr基础操作6
- New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer
- Gradle连载7-配置签名
猜你喜欢

After crossing, she said that the multiverse really exists

JS绘制极坐标颜色渐变

Cartoon security HIDS, EDR, NDR, XDR

About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb

西门子低代码 9.14版本: 满足不同需求
![克隆無向圖[bfs訪問每條邊而不止節點]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆無向圖[bfs訪問每條邊而不止節點]

Sword finger offer 13 Range of motion of robot

333333333333333333333333333333

Embedded development: Hardware in the loop testing

漫画安全HIDS、EDR、NDR、XDR
随机推荐
Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}
Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
Cacti maximum monitoring number test
Mysql:sql overview and database system introduction | dark horse programmer
8软件工程环境
After crossing, she said that the multiverse really exists
Web APIs environment object - dark horse programmer
Solr basic operations 7
Halcon practical: design idea of solder joint detection
这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
QT learning 04 Hello QT
Do mysqlcdc data not support windowing functions like row_ Number, lead
代码分析平台 SonarQube 实战
Common knowledge of ECS security settings
How about counting Berry Pie 4? What are the possible ways to play?
漫画安全HIDS、EDR、NDR、XDR
Label Troubleshooting: unable to open the marked image
After 8 years of polishing, "dream factory of game design" released an epic update!
Shell operator
Majority element ii[molar voting method for finding modes]