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

核心代码:

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个闭合点
        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)
        # 鼠标移动时更新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模拟实现网页鼠标移动特效的详细内容,更多关于PyQt5鼠标特效的资料请关注我们其它相关文章!

(0)

相关推荐

  • 使用pyqt5 实现ComboBox的鼠标点击触发事件

    一.自定义MyComboBox # MyComboBox.py from PyQt5.QtWidgets import QComboBox from PyQt5.QtCore import pyqtSignal class MyComboBox(QComboBox): clicked = pyqtSignal() #创建一个信号 def showPopup(self): #重写showPopup函数 self.clicked.emit() #发送信号 super(MyComboBox, self

  • PyQt5重写QComboBox的鼠标点击事件方法

    最近学PyQt5,想要做一个串口调试助手来练练手,之前用了正点原子的串口上位机,觉得点击ComboBox自动检测串口这个功能很棒,之前用QT5写串口调试助手的时候也想加入这个功能,但是一直没有成功,之后就不了了之,现在用了PyQt之后就想着一定要实现这个功能,百度了之后看了很多资料都没有找到直接的解决方法,但是大家都是在强调重写鼠标点击事件,然后自己慢慢实验终于写出来了. 我的开发环境是PyCharm+Python3.6+PyQt5.9.2 1.建立工程创建界面什么的我就不写了,百度一大堆,重点

  • 当鼠标移动时出现特效的JQuery代码

    复制代码 代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <style type="text/css"> *{ text-align: center; font-size: 12px; } table{ border-collapse: collaps

  • js实现图片放大并跟随鼠标移动特效

    图片跟随鼠标移动并放大js特效,供大家参考,具体内容如下 很多网站有类似于淘宝放大镜的效果,只不过这里说的是 " 不仅能直接放大,而且会跟随鼠标移动 " ! 类似于" DEDECMS "官网的案例中心的效果 ! 本案例代码,效果图,代码,参考如下: JS代码: <script> //展示 function showBigPic(filepath) { //将文件路径传给img大图 document.getElementById('bigPic').src

  • pyqt5移动鼠标显示坐标的方法

    如下所示: # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel) from PyQt5.QtCore import Qt class AppDemo(QMainWindow): def __init__(self): super(AppDemo, self).__init__() self.init_ui() def init_ui(self): sel

  • 利用PyQt5模拟实现网页鼠标移动特效

    核心代码: 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

  • 10个经典的网页鼠标特效代码

    1.鼠标指向出现实用特殊提示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv

  • 利用Java+Selenium+OpenCV模拟实现网页滑动验证

    目录 一.需求分析 二.模拟步骤 1.使用selenium打开某音网页 2.找到小滑块以及小滑块所在的背景图 3.计算小滑块需要滑动的距离 4.按住小滑块并滑动 三.学习过程中比较棘手的问题 1.截图问题 2.返回结果与实际滑动距离相差太多,甚至无规律可循 3.openCV的下载安装 四.总结 目前很多网页都有滑动验证,目的就是防止不良爬虫扒他们网站的数据,我这次本着学习的目的使用Java和selenium学习解决滑动验证的问题,前前后后花了一周时间(抄代码),终于成功了某音的滑动验证! 效果展

  • JavaScript利用canvas实现鼠标跟随特效

    目录 前言 创建canvas画布 定义鼠标 x / y 初始化canvas 画箭头 循环动画 鼠标事件 前言 canvas是一个很神奇的玩意儿,比如画表格.画海报图都要用canvas去做,前几天有用css去做一个鼠标跟随的恶魔之眼的动画效果,想着能不能用canvas也做一个鼠标跟随的效果呢? 创建canvas画布 canvas画布创建可以直接用canvas标签即可 <canvas></canvas> 也没有过多的样式,基本都是用js去完成画布中的内容的 const canvas =

  • js实现完全自定义可带多级目录的网页鼠标右键菜单方法

    本文实例讲述了js实现完全自定义可带多级目录的网页鼠标右键菜单方法.分享给大家供大家参考.具体分析如下: 这是很不错的一个网页鼠标特性,这个代码可以控制网页中鼠标的右键菜单,完全按照你的意思打造,可以带多级的目录显示. 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.

  • jquery实现美观的导航菜单鼠标提示特效代码

    本文实例讲述了jquery实现美观的导航菜单鼠标提示特效代码.分享给大家供大家参考.具体如下: 这是一个奇妙的导航菜单鼠标提示特效,俗称"链接提示",鼠标放在导航菜单的链接上即可显示出该链接所指向网页的大致内容,提示用户到达需要的页面. 先来看看运行效果截图: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-nav-tips-nav-menu-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC &quo

  • 利用PyQt5+Matplotlib 绘制静态/动态图的实现代码

    代码编辑环境 Win10+(Pycharmm or Vscode)+PyQt 5.14.2 功能实现 静态作图:数据作图,取决于作图函数,可自行修改 动态作图:产生数据,获取并更新数据,最后刷新显示,可用于实现数据实时采集并显示的场景 效果展示 代码块(业务与逻辑分离)业务–UI界面代码 文件名:Ui_realtimer_plot.py # -*- coding: utf-8 -*- # Added by the Blog author VERtiCaL on 2020/07/12 at SSR

  • Python如何利用正则表达式爬取网页信息及图片

    一.正则表达式是什么? 概念: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规则字符串"用来表达对字符串的一种过滤逻辑. 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. 个人理解: 简单来说就是使用正则表达式来写一个过滤器来过滤了掉杂乱的无用的信息(eg:网页源代码-)从中来获取自己想要的内容 二.实战项目 1.爬取内容 获取上海所有三甲医院的名称并保

  • Python利用PyQt5制作一个获取网络实时NBA数据并播报的GUI程序

    制作NBA数据爬虫 捋顺思路 我们在这里选择的是百度体育带来的数据,我们在百度当中直接搜索NBA跳转到网页,我们可以看到,百度已经为我们提供了相关的数据 我们点击进去后,可以发现这是一个非常简洁的网址 我们看一下这个地址栏,发现毫无规律https://tiyu.baidu.com/live/detail/576O5Zu955S35a2Q6IGM5Lia56%2Bu55CD6IGU6LWbI2Jhc2tldGJhbGwjMjAyMS0wNi0xMyPniLXlo6t2c%2BWspritq%2Bi

  • Python利用PyQt5制作一个获取网络实时数据NBA数据播报GUI功能

    制作NBA数据爬虫 捋顺思路 我们在这里选择的是百度体育带来的数据,我们在百度当中直接搜索NBA跳转到网页,我们可以看到,百度已经为我们提供了相关的数据 我们点击进去后,可以发现这是一个非常简洁的网址 我们看一下这个地址栏,发现毫无规律https://tiyu.baidu.com/live/detail/576O5Zu955S35a2Q6IGM5Lia56%2Bu55CD6IGU6LWbI2Jhc2tldGJhbGwjMjAyMS0wNi0xMyPniLXlo6t2c%2BWspritq%2Bi

随机推荐