编程怎么画渐变的圆形

时间:2025-01-23 11:05:51 游戏攻略

使用Pycairo库绘制渐变圆形

```python

import cairo

设置画布大小

WIDTH, HEIGHT = 640, 480

创建图形上下文

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)

context = cairo.Context(surface)

设置背景色

context.set_source_rgb(1, 1, 1)

context.paint()

绘制黑色矩形

context.rectangle(50, 50, 100, 100)

context.set_source_rgb(0, 0, 0)

context.fill()

绘制渐变圆

radial = cairo.RadialGradient(50, 50, 0, 50, 50, 1, 1, 0.5, 0.5)

context.set_source(radial)

context.arc(50, 50, 50, 0, 2 * 3.14159)

context.fill()

将结果保存为PNG

surface.write_to_png("gradient_circle.png")

```

使用turtle库绘制渐变圆形

```python

import turtle

设置画布大小

turtle.screensize(1200, 1000)

移动画笔到合适位置

turtle.penup()

turtle.goto(-300, 0)

turtle.pendown()

设置画笔颜色

turtle.pensize(5)

turtle.color("blue", "yellow")

开始填充

turtle.begin_fill()

绘制渐变圆

for i in range(3, 10):

turtle.circle(50, steps=i)

turtle.forward(100)

关闭填充

turtle.end_fill()

隐藏画笔

turtle.hideturtle()

结束

turtle.done()

```

使用CSS绘制径向渐变圆形

```html