亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

利用PyQt5模擬實(shí)現(xiàn)網(wǎng)頁鼠標(biāo)移動特效

 更新時間:2022年03月18日 08:38:04   作者:Vertira  
不知道大家有沒有發(fā)現(xiàn),博客園有些博客左側(cè)會有鼠標(biāo)移動特效。通過移動鼠標(biāo),會形成類似蜘蛛網(wǎng)的特效,本文將用PyQt5實(shí)現(xiàn)這一特效,需要的可以參考一下

核心代碼:

from random import random
from time import time
from PyQt5.QtCore import QPropertyAnimation, QObject, pyqtProperty, QEasingCurve,\
    Qt, QRectF, pyqtSignal
from PyQt5.QtGui import QColor, QPainterPath, QPainter
from PyQt5.QtWidgets import QWidget
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
try:
    import pointtool  # @UnusedImport @UnresolvedImport
    getDistance = pointtool.getDistance
    findClose = pointtool.findClose
except:
    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 = closest
class Target:
    def __init__(self, x, y):
        self.x = x
        self.y = y
class 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個閉合點(diǎn)
        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 = y
class 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)
        # 鼠標(biāo)移動時更新xy坐標(biāo)
        self.target.x = event.x()
        self.target.y = event.y()
        self.update()
    def initPoints(self):
        t = time()
        self.points.clear()
        # 創(chuàng)建點(diǎn)
        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()
        # 每個點(diǎn)尋找5個閉合點(diǎn)
        findClose(self.points)
        print(time() - t)
    def animate(self, painter):
        for p in self.points:
            # 檢測點(diǎn)的范圍
            value = abs(getDistance(self.target, p))
            if value < 4000:
                # 其實(shí)就是修改顏色透明度
                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_())

運(yùn)行結(jié)果如下:

以上就是利用PyQt5模擬實(shí)現(xiàn)網(wǎng)頁鼠標(biāo)移動特效的詳細(xì)內(nèi)容,更多關(guān)于PyQt5鼠標(biāo)特效的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python實(shí)現(xiàn)甘特圖繪制的示例詳解

    Python實(shí)現(xiàn)甘特圖繪制的示例詳解

    相信在平常實(shí)際工作當(dāng)中,需要對整體的項目做一個梳理,這時如果有一個網(wǎng)頁應(yīng)用能夠?qū)φw項目有一個可視化頁面的展示,是不是會對你的實(shí)際工作有所幫助呢?今天小編就通過Python+Streamlit框架來繪制甘特圖并制作可視化大屏,需要的可以參考一下
    2023-04-04
  • Python PyQt5標(biāo)準(zhǔn)對話框用法示例

    Python PyQt5標(biāo)準(zhǔn)對話框用法示例

    這篇文章主要介紹了Python PyQt5標(biāo)準(zhǔn)對話框用法,結(jié)合實(shí)例形式分析了PyQt5常用的標(biāo)準(zhǔn)對話框及相關(guān)使用技巧,需要的朋友可以參考下
    2017-08-08
  • 詳解Python描述符的工作原理

    詳解Python描述符的工作原理

    在 Python 開發(fā)中,你可能聽說過「描述符」這個概念,由于我們很少直接使用它,所以大部分開發(fā)人員并不了解它的原理. 但作為熟練使用 Python,想要進(jìn)階的你,建議還是了解一下描述符的原理,這也便于你更深層次地理解 Python 的設(shè)計思想,需要的朋友可以參考下
    2021-06-06
  • 詳解pyinstaller selenium python3 chrome打包問題

    詳解pyinstaller selenium python3 chrome打包問題

    這篇文章主要介紹了詳解pyinstaller selenium python3 chrome打包問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python中正則表達(dá)式詳解

    Python中正則表達(dá)式詳解

    Python 的 re 模塊(Regular Expression 正則表達(dá)式)提供各種正則表達(dá)式的匹配操作,Python 會將正則表達(dá)式轉(zhuǎn)化為字節(jié)碼,利用 C 語言的匹配引擎進(jìn)行深度優(yōu)先的匹配。
    2017-05-05
  • Python 實(shí)現(xiàn)選擇排序的算法步驟

    Python 實(shí)現(xiàn)選擇排序的算法步驟

    下面小編就為大家分享一篇Python 實(shí)現(xiàn)選擇排序的算法步驟,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解如何在Django項目中使用Jinja2模板引擎

    詳解如何在Django項目中使用Jinja2模板引擎

    Django是一個強(qiáng)大的Python Web框架,它提供了一個內(nèi)置的模板引擎,然而,在某些場景中,開發(fā)者可能傾向于使用更快、更靈活的模板引擎,比如Jinja2,在本文中,我們將詳細(xì)探討如何在Django項目中使用Jinja2模板引擎,并提供豐富的示例
    2023-11-11
  • 利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼

    利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼

    這篇文章主要介紹了利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • python3.6利用pyinstall打包py為exe的操作實(shí)例

    python3.6利用pyinstall打包py為exe的操作實(shí)例

    今天小編就為大家分享一篇python3.6利用pyinstall打包py為exe的操作實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python實(shí)現(xiàn)計數(shù)排序與桶排序?qū)嵗a

    python實(shí)現(xiàn)計數(shù)排序與桶排序?qū)嵗a

    這篇文章主要介紹了python計數(shù)排序與桶排序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論