核心代码:
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 # @unudimport @unresolvedimport getdistance = pointtool.getdistance findclo = pointtool.findcloe三八妇女节活动通知xcept: import math def getdistance(p1, p2): return mat我是舞蹈家h.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2) def findclo(points): plen = len(points) for i in range(plen): clost = [none, none, none, none, none] p1 = points[i] for j in range(plen): p2 = points[j] dte1 = getdistance(p1, p2) if p1 != p2: placed = fal for k in range(5): if not placed: if not clost[k]: clost[k] = p2 placed = true for k in range(5): if not placed: if dte1 < getd压力方向istance(p1, clost[k]): clost[k] = p2 placed = true p1.clost = clostclass target: def __init__(lf, x, y): lf.x = x lf.y = yclass point(qobject): valuechanged = pyqtsignal() def __init__(lf, x, ox, y, oy, *args, **kwargs): super(point, lf).__init__(*args, **kwargs) lf.__x = x lf._x = x lf.originx = ox lf._y = y lf.__y = y lf.originy = oy # 5个闭合点 lf.clost = [0, 0, 0, 0, 0] # 圆半径 lf.radius = 2 + random() * 2 # 连线颜色 lf.linecolor = qcolor(156, 217, 249) # 圆颜色 lf.circlecolor = qcolor(156, 217, 249) def initanimation(lf): # 属性动画 if not hasattr(lf, 'xanimation'): lf.xanimation = qpropertyanimation( lf, b'x', lf, valuechanged=lf.valuechanged.emit, easingcurve=qeasingcurve.inoutsine) lf.yanimation = qpropertyanimation( lf, b'y', lf, valuechanged=lf.valuechanged.emit, easingcurve=qeasingcurve.inoutsine, finished=lf.updateanimation) lf.updateanimation() def updateanimation(lf): lf.xanimation.stop() lf.yanimation.stop() duration = (1 + random()) * 1000 lf.xanimation.tduration(duration) lf.yanimation.tduration(duration) lf.xanimation.tstartvalue(lf.__x) lf.xanimation.tendvalue(lf.originx - 50 + random() * 100) lf.yanimation.tstartvalue(lf.__y) lf.yanimation.tendvalue(lf.originy - 50 + random() * 100) lf.xanimation.start() lf.yanimation.start() @pyqtproperty(float) def x(lf): return lf._x @x.tter def x(lf, x): lf._x = x @pyqtproperty(float) def y(lf): return lf._y @y.tter def y(lf, y): lf._y = yclass window(qwidget): def __init__(lf, *args, **kwargs): super(window, lf).__init__(*args, **kwargs) lf.tmoutracking(true) lf.resize(800, 600) lf.points = [] lf.target = target(lf.width() / 2, lf.height() / 2) lf.initpoints() def paintevent(lf, event): super(window, lf).paintevent(event) painter = qpainter() painter.begin(lf) painter.trenderhint(qpainter.antialiasing) painter.fillrect(lf.rect(), qt.black) lf.animate(painter) painter.end() def moumoveevent(lf, event): super(window, lf).moumoveevent(event) # 鼠标移动时更新xy坐标 lf.target.x = event.x() lf.target.y = event.y() lf.update() def initpoints(lf): t = time() lf.points.clear() # 创建点 stepx = lf.width() / 20 stepy = lf.height() / 20 for x in range(0, lf.width(), int(stepx)): for y in range(0, lf.height(), int(stepy)): ox = x + random() * stepx oy = y + random() * stepy point = point(ox, ox, oy, oy) point.valuechanged.connect(lf.update) lf.points.append(point) print(time() - t) t = time() # 每个点寻找5个闭合点 二年级下册 findclo(lf.points) print(time() - t) def animate(lf, painter): for p in lf.points: # 检测点的范围 value = abs(getdistance(lf.target, p)) if value < 4000: # 其实就是修改颜色透明度 p.linecolor.talphaf(0.3) p.circlecolor.talphaf(0.6) elif value < 20000: p.linecolor.talphaf(0.1) p.circlecolor.talphaf(0.3) elif value < 40000: p.linecolor.talphaf(0.02) p.circlecolor.talphaf(0.1) el: p.linecolor.talphaf(0) p.circlecolor.talphaf(0) # 画线条 if p.linecolor.alpha(): for pc in p.clost: if not pc: continue path = qpainterpath() path.moveto(p.x, p.y) path.lineto(pc.x, pc.y) painter.save() painter.tpen(p.linecolor) painter.drawpath(path) painter.restore() # 画圆 painter.save() painter.tpen(qt.nopen) painter.t摘抄文章brush(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鼠标特效的资料请关注www.887551.com其它相关文章!
本文发布于:2023-04-06 04:43:45,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/2752773bc5ada343649a11666e3c3044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:利用PyQt5模拟实现网页鼠标移动特效.doc
本文 PDF 下载地址:利用PyQt5模拟实现网页鼠标移动特效.pdf
留言与评论(共有 0 条评论) |