Qt6 QML Book/画布/渐变

Gradients

渐变

Canvas can fill shapes with color but also with gradients or images.

画布可以用颜色填充形状,也可以用渐变或图像填充形状。

onPaint: {
    var ctx = getContext("2d")

    var gradient = ctx.createLinearGradient(100,0,100,200)
    gradient.addColorStop(0, "blue")
    gradient.addColorStop(0.5, "lightsteelblue")
    ctx.fillStyle = gradient
    ctx.fillRect(50,50,100,100)
}

The gradient in this example is defined along the starting point (100,0) to the end point (100,200), which gives a vertical line in the middle of our canvas. The gradient-stops can be defined as a color from 0.0 (gradient start point) to 1.0 (gradient endpoint). Here we use a blue color at 0.0 (100,0) and a lightsteelblue color at the 0.5 (100,200) position. The gradient is defined as much larger than the rectangle we want to draw, so the rectangle clips gradient to it’s defined the geometry.

这个例子中的渐变被定义为沿着起点(100,0)到终点(100,200),这在我们的画布中间给出了一条垂直线。渐变停止点可以定义为从0.0(渐变起点)到1.0(渐变终点)的颜色。在这里,我们在0.0(100,0)位置使用蓝色blue,在0.5(100,200)位置使用浅蓝色lightsteelblue。渐变被定义为比我们想要绘制的矩形大得多,因此矩形将渐变剪裁到几何体定义的位置。

TIP

The gradient is defined in canvas coordinates not in coordinates relative to the path to be painted. A canvas does not have the concept of relative coordinates, as we are used to by now from QML.

渐变是在画布坐标中定义的,而不是在相对于要绘制的路径的坐标中定义的。画布没有相对坐标的概念,我们现在已经习惯于使用QML。

 示例源码下载 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值