当前位置:网站首页>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>
边栏推荐
- DOM 知识点总结
- Set up security groups, record domain names, and apply for SSL certificates
- QT learning 06 widgets and window types
- Solr基础操作8
- After 8 years of polishing, "dream factory of game design" released an epic update!
- shell-位置参数变量和预定义变量
- MySQL:SQL概述及数据库系统介绍 | 黑马程序员
- This simple little function saves 213 hours for our production research team in half a year
- Bee common configuration
- Official website of Greentree
猜你喜欢

QT learning 02 GUI program example analysis

6.29 problem solving

JVM之栈空间

JS绘制极坐标颜色渐变

Leetcode (76) -- Minimum Covering substring

數莓派 4怎麼樣?可能的玩法有哪些?

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

FPGA Development (2) -- IIC communication

Basic tutorial for installing monggodb in win10

西门子低代码 9.14版本: 满足不同需求
随机推荐
多数元素II[求众数类之摩尔投票法]
Teach you step by step to install webwind notes on edge browser
Bee common configuration
ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
Golang6 reflection
333333333333333333333333333333
gyctf_2020_document
QT learning 06 widgets and window types
我想知道今天还可以开户么?另外想问,现在在线开户安全么?
vlog常用参数解析
Do mysqlcdc data not support windowing functions like row_ Number, lead
Buffer flow exercise
Andorid source build/envsetup. SH details to know
Bee常用配置
克隆无向图[bfs访问每条边而不止节点]
Solr基础操作13
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
Koa2 learning and using
AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended