QML粒子系统制作心形效果

说明:

虽然 5 月 20 号过了,还是来画下心。这里我使用 QML 的粒子系统来制作心形效果。

粒子模拟的核心是 ParticleSystem 控制共享时间线的。一个场景可以有多个粒子系统,每个粒子系统都有一个独立的时间轴。使用 Emitter 元素发射粒子,并使用使其可视化,元素 ParticlePainter 可以是图像,QML Item 或着色器 Item。发射器还使用矢量空间提供粒子的方向。发射的粒子不再可以由发射器操纵。粒子模块提供 Affector,可以在发射粒子后操纵粒子的参数。

QML 粒子系统的知识请参照 QMLBook 一书,或示例搜索 Particle 。

代码效果:

(为什么是绿色的?爱情长青!!!)

代码链接:

https://github.com/gongjianbo/MyTestCode/tree/master/Qml/TestQml_20200521_Love

主要代码:

(需要准备心形的图片,我这里用的图片来做 Particle 和 Mask)

//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("龚建波 1992")

   header: TabBar {
        id: bar
        width: parent.width
        TabButton { text: qsTr("Hollow") }
        TabButton { text: qsTr("Solid") }
        TabButton { text: qsTr("Circle") }
    }

    StackLayout {
        width: parent.width
        currentIndex: bar.currentIndex
        anchors.fill: parent
        HollowHeart{ }
        SolidHeart{ }
        CircleHeart{ }
    }
}
//CircleHeart.qml
import QtQuick 2.12
import QtQuick.Particles 2.12

Item {
    id: control

    ParticleSystem {
        id: sys
        width: 300
        height: 300
        anchors.centerIn: parent

        ImageParticle {
            anchors.fill: parent
            system: sys
            source: "qrc:/heart_1.png"
            color: Qt.rgba(0,0.5,0,0.8)
            //colorVariation: 0.5 //彩色效果
        }

        Emitter {
            id: emitter
            //anchors.fill: parent
            system: sys

            emitRate: 10  //发射速率
            lifeSpan: 5000 //生存周期ms
            size: 25 //尺寸
            sizeVariation: 5
            // 移动效果
            velocityFromMovement: 8
            velocity: PointDirection {xVariation: 2; yVariation: 2;}
            acceleration: PointDirection {xVariation: 10; yVariation: 10;}

            x:circle.cx
            y:circle.cy
        }

        Rectangle{
            //测试范围用的矩形
            z: -1
            anchors.fill: parent
            border.color: "green"
            color: "transparent"
        }
    }

    Item {
        id: circle
        property real radius: 100
        property real cx: radius * Math.sin(percent*2*Math.PI)+sys.width/2
        property real cy: radius * Math.cos(percent*2*Math.PI)+sys.height/2
        property real percent: 0

        SequentialAnimation on percent {
            loops: Animation.Infinite
            running: true
            NumberAnimation { from: 0.0; to: 1 ; duration: 5000;  }
        }
    }
}
//HollowHeart.qml
import QtQuick 2.12
import QtQuick.Particles 2.12

Item {
    id: control

    ParticleSystem {
        id: sys
        width: 300
        height: 300
        anchors.centerIn: parent

        ImageParticle {
            anchors.fill: parent
            system: sys
            source: "qrc:/heart_1.png"
            color: Qt.rgba(0,0.5,0,0.8)
            //colorVariation: 0.5 //彩色效果
        }

        Emitter {
            id: emitter
            anchors.fill: parent
            system: sys
            //maximumEmitted: 500
            emitRate: 100  //发射速率
            lifeSpan: 2500 //生存周期ms
            size: 25 //尺寸
            endSize: 40  //最终尺寸
            sizeVariation: 5
            // 移动效果
            acceleration: AngleDirection{
                magnitude: 10
                angleVariation: 360
            }
            // mask只能用source
            shape: MaskShape {
                source: "qrc:/heart_2.png" //空心
            }
        }

    }
}
//SolidHeart.qml
import QtQuick 2.12
import QtQuick.Particles 2.12

Item {
    id: control

    ParticleSystem {
        id: sys
        width: 300
        height: 300
        anchors.centerIn: parent

        ImageParticle {
            anchors.fill: parent
            system: sys
            source: "qrc:/heart_1.png"
            color: Qt.rgba(0,0.5,0,0.8)
            //colorVariation: 0.5 //彩色效果
        }

        Emitter {
            id: emitter
            anchors.fill: parent
            system: sys
            //maximumEmitted: 500
            emitRate: 100  //发射速率
            lifeSpan: 2500 //生存周期ms
            size: 25 //尺寸
            endSize: 50  //最终尺寸
            sizeVariation: 5
            // mask只能用source
            shape: MaskShape {
                source: "qrc:/heart_1.png" //实心
            }
        }

    }
}

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龚建波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值