千家信息网

如何用PyQt5模拟实现网页鼠标移动特效

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇"如何用PyQt5模拟实现网页鼠标移动特效"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看
千家信息网最后更新 2026年11月03日如何用PyQt5模拟实现网页鼠标移动特效

这篇"如何用PyQt5模拟实现网页鼠标移动特效"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"如何用PyQt5模拟实现网页鼠标移动特效"文章吧。

核心代码:

from random import randomfrom time import timefrom PyQt5.QtCore import QPropertyAnimation, QObject, pyqtProperty, QEasingCurve,\    Qt, QRectF, pyqtSignalfrom PyQt5.QtGui import QColor, QPainterPath, QPainterfrom PyQt5.QtWidgets import QWidget__Author__ = """By: IronyQQ: 892768447Email: 892768447@qq.com"""__Copyright__ = 'Copyright (c) 2018 Irony'__Version__ = 1.0try:    import pointtool  # @UnusedImport @UnresolvedImport    getDistance = pointtool.getDistance    findClose = pointtool.findCloseexcept:    import math    def getDistance(p1, p2):        return math.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2)    def findClose(points):        plen = len(points)        for i in range(plen):            closest = [None, None, None, None, None]            p1 = points[i]            for j in range(plen):                p2 = points[j]                dte1 = getDistance(p1, p2)                if p1 != p2:                    placed = False                    for k in range(5):                        if not placed:                            if not closest[k]:                                closest[k] = p2                                placed = True                    for k in range(5):                        if not placed:                            if dte1 < getDistance(p1, closest[k]):                                closest[k] = p2                                placed = True            p1.closest = closestclass Target:    def __init__(self, x, y):        self.x = x        self.y = yclass Point(QObject):    valueChanged = pyqtSignal()    def __init__(self, x, ox, y, oy, *args, **kwargs):        super(Point, self).__init__(*args, **kwargs)        self.__x = x        self._x = x        self.originX = ox        self._y = y        self.__y = y        self.originY = oy        # 5个闭合点        self.closest = [0, 0, 0, 0, 0]        # 圆半径        self.radius = 2 + random() * 2        # 连线颜色        self.lineColor = QColor(156, 217, 249)        # 圆颜色        self.circleColor = QColor(156, 217, 249)    def initAnimation(self):        # 属性动画        if not hasattr(self, 'xanimation'):            self.xanimation = QPropertyAnimation(                self, b'x', self, valueChanged=self.valueChanged.emit,                easingCurve=QEasingCurve.InOutSine)            self.yanimation = QPropertyAnimation(                self, b'y', self, valueChanged=self.valueChanged.emit,                easingCurve=QEasingCurve.InOutSine,                finished=self.updateAnimation)            self.updateAnimation()    def updateAnimation(self):        self.xanimation.stop()        self.yanimation.stop()        duration = (1 + random()) * 1000        self.xanimation.setDuration(duration)        self.yanimation.setDuration(duration)        self.xanimation.setStartValue(self.__x)        self.xanimation.setEndValue(self.originX - 50 + random() * 100)        self.yanimation.setStartValue(self.__y)        self.yanimation.setEndValue(self.originY - 50 + random() * 100)        self.xanimation.start()        self.yanimation.start()    @pyqtProperty(float)    def x(self):        return self._x    @x.setter    def x(self, x):        self._x = x    @pyqtProperty(float)    def y(self):        return self._y    @y.setter    def y(self, y):        self._y = yclass Window(QWidget):    def __init__(self, *args, **kwargs):        super(Window, self).__init__(*args, **kwargs)        self.setMouseTracking(True)        self.resize(800, 600)        self.points = []        self.target = Target(self.width() / 2, self.height() / 2)        self.initPoints()    def paintEvent(self, event):        super(Window, self).paintEvent(event)        painter = QPainter()        painter.begin(self)        painter.setRenderHint(QPainter.Antialiasing)        painter.fillRect(self.rect(), Qt.black)        self.animate(painter)        painter.end()    def mouseMoveEvent(self, event):        super(Window, self).mouseMoveEvent(event)        # 鼠标移动时更新xy坐标        self.target.x = event.x()        self.target.y = event.y()        self.update()    def initPoints(self):        t = time()        self.points.clear()        # 创建点        stepX = self.width() / 20        stepY = self.height() / 20        for x in range(0, self.width(), int(stepX)):            for y in range(0, self.height(), int(stepY)):                ox = x + random() * stepX                oy = y + random() * stepY                point = Point(ox, ox, oy, oy)                point.valueChanged.connect(self.update)                self.points.append(point)        print(time() - t)        t = time()        # 每个点寻找5个闭合点        findClose(self.points)        print(time() - t)    def animate(self, painter):        for p in self.points:            # 检测点的范围            value = abs(getDistance(self.target, p))            if value < 4000:                # 其实就是修改颜色透明度                p.lineColor.setAlphaF(0.3)                p.circleColor.setAlphaF(0.6)            elif value < 20000:                p.lineColor.setAlphaF(0.1)                p.circleColor.setAlphaF(0.3)            elif value < 40000:                p.lineColor.setAlphaF(0.02)                p.circleColor.setAlphaF(0.1)            else:                p.lineColor.setAlphaF(0)                p.circleColor.setAlphaF(0)            # 画线条            if p.lineColor.alpha():                for pc in p.closest:                    if not pc:                        continue                    path = QPainterPath()                    path.moveTo(p.x, p.y)                    path.lineTo(pc.x, pc.y)                    painter.save()                    painter.setPen(p.lineColor)                    painter.drawPath(path)                    painter.restore()            # 画圆            painter.save()            painter.setPen(Qt.NoPen)            painter.setBrush(p.circleColor)            painter.drawRoundedRect(QRectF(                p.x - p.radius, p.y - p.radius, 2 * p.radius, 2 * p.radius), p.radius, p.radius)            painter.restore()            # 开启动画            p.initAnimation()if __name__ == '__main__':    import sys    import cgitb    sys.excepthook = cgitb.enable(1, None, 5, '')    from PyQt5.QtWidgets import QApplication    app = QApplication(sys.argv)    w = Window()    w.show()    sys.exit(app.exec_())

运行结果如下:

以上就是关于"如何用PyQt5模拟实现网页鼠标移动特效"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

内容 鼠标 移动 特效 网页 颜色 动画 就是 文章 知识 篇文章 p.y 闭合 代码 价值 半径 坐标 大部分 属性 更多 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网与科技 深圳市人人互联网科技有限公司 国智互联网科技 盈实互联网科技 奇酷互联网络科技 深圳齐家互联网科技 科技互联网创业计划书 互联网科技生活的内容 成都玖崟互联网科技有限公司乐商圈怎么样 欧洲十大科技互联网公司 洛阳昕诺飞互联网科技有限公司 浙南科技城互联网大厦招聘 半导体是互联网高科技吗 郑州欣桑互联网科技有限公司 信承诺互联网科技有限公司 互联网公司属于科技吗 科技和互联网行业的特点 互联网科技的前景 北京品多多互联网科技有限公司 网互联网络科技 中国互联网科技工作委员会毛军 互联网科技活动banner 互联网 科技 科技创新互联网企业 四个字互联网科技公司取名 哈尔滨新华互联网科技学院 智腾互联网科技有限公司 银川新华互联网科技学校联系电话 烟台灵鸽互联网科技有限公司 城市宝互联网科技有限公司
0