当前位置:网站首页>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>
边栏推荐
- FPGA Development (1) -- serial port communication
- Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
- Unity splashimage scaling problem
- New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer
- Which securities company should I choose to open an account online? Also, is it safe to open an account online?
- gyctf_2020_document
- 西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
- History of deep learning
- Solr basic operation 10
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
猜你喜欢

架构实战营模块5作业

Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus

项目一:部署 LAMP ecshop电商平台

设置安全组、域名备案、申请ssl证书

8软件工程环境

Project 1: deploy lamp ECSHOP e-commerce platform

漫画安全HIDS、EDR、NDR、XDR

Et la tarte aux framboises 4? Quels sont les jeux possibles?

Leetcode (680) -- verifying palindrome string II

云原生爱好者周刊:炫酷的 Grafana 监控面板集合
随机推荐
Solr basic operations 7
Mysql:sql overview and database system introduction | dark horse programmer
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
New CorelDRAW technical suite2022 latest detailed function introduction
QT learning 06 widgets and window types
About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
There is no web-based development for the reward platform. Which is suitable for native development or mixed development?
Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
旋转彩色三叶草
数莓派 4怎么样?可能的玩法有哪些?
Cacti maximum monitoring number test
solo 博客皮肤导入 skins 文件夹后出现 500 错误
數莓派 4怎麼樣?可能的玩法有哪些?
I wonder if I can open an account today? In addition, is it safe to open an account online now?
项目一:部署 LAMP ecshop电商平台
After crossing, she said that the multiverse really exists
QT learning 03 birth and essence of QT
爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
Gradle连载7-配置签名
Leetcode (76) -- Minimum Covering substring