Advertisement
here2share

# Tk_pattern_art_animation.py

Jul 6th, 2022
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. # Tk_pattern_art_animation.py ZZZ
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import time
  6. import random
  7. import math
  8.  
  9. nx = 500
  10. ny = 500
  11.  
  12. root = Tk()
  13. root.title("")
  14. root.geometry("%dx%d+50+50"%(nx,ny))
  15.  
  16. canvas = Canvas(root, width=nx, height=ny)
  17. canvas.place(x=0, y=0)
  18.  
  19. def RGB__(rgb):
  20.     r,g,b = [int(i) for i in rgb]
  21.     return "#%02x%02x%02x" % (r,g,b)
  22.  
  23. mouseX, mouseY = None, None
  24. def mouse_click(event):
  25.     global mouseX, mouseY
  26.     mouseX, mouseY = event.x, event.y
  27.  
  28. def mouse_release(event):
  29.     global mouseX, mouseY
  30.     mouseX, mouseY = None, None
  31.  
  32. def mouse_drag(event):
  33.     global mouseX, mouseY
  34.     if mouseX and mouseY:
  35.         mouseX, mouseY = event.x, event.y
  36.  
  37. canvas.bind("<Button-1>", mouse_click)
  38. canvas.bind("<Button-3>", mouse_release)
  39. canvas.bind("<B1-Motion>", mouse_drag)
  40.  
  41. def display():
  42.     canvas.create_rectangle((x,y,x+1,y+1), fill=RGB__(rgb), outline='')
  43.  
  44. while 1: # infinite loop       
  45.     # random color
  46.     rgb = [random.randint(0, 255) for i in range(3)]
  47.  
  48.     # patterns animation
  49.     pattern_animation_duration = random.uniform(0.1, 2.0) # duration time of animation
  50.     pattern_animation_end = time.time() + pattern_animation_duration
  51.     pattern_animation_start = time.time()
  52.     pattern_animation_scale = random.uniform(0.01, 0.01) # scale of animation
  53.     pattern_animation = random.choice(["fade_in", "fade_out", "rotate", "scale_x", "scale_y", "scale_rgb", "scale_alpha", "scale_all", "gradient", "xy_flip"])
  54.  
  55.     # rotation angle
  56.     rotation_angle = random.random() * math.pi * 2
  57.  
  58.     # triangle points
  59.     triangle_points = [random.randint(0, nx) for i in range(6)]
  60.  
  61.     # polygon points
  62.     polygon_points = [random.randint(0, nx) for i in range(random.randint(6, 10)*2)]
  63.  
  64.     # parameters
  65.     alpha = random.randint(1, 255)
  66.     fill_opacity = random.uniform(0.5, 1.0)
  67.     outline_opacity = random.uniform(0.5, 1.0)
  68.     width = random.randint(1, 10)
  69.     fill = RGB__(rgb)
  70.     outline = RGB__(rgb)
  71.  
  72.     # first color (r, g, b) of gradient
  73.     rgb_a = [random.randint(0, 255) for i in range(3)]
  74.  
  75.     # second color (r, g, b) of gradient
  76.     rgb_b = [random.randint(0, 255) for i in range(3)]
  77.  
  78.     # first angle of gradient
  79.     angle_a = random.randint(0, 360)
  80.  
  81.     # second angle of gradient
  82.     angle_b = random.randint(0, 360)
  83.  
  84.     # length of gradient
  85.     len1 = math.hypot(nx, ny)
  86.  
  87.     # X side of gradient
  88.     x1 = nx/2*math.cos(math.radians(45)*math.pi/180)+len1*math.cos(math.radians(angle_a)*math.pi/180)
  89.     y1 = ny/2*math.sin(math.radians(45)*math.pi/180)-len1*math.sin(math.radians(angle_a)*math.pi/180)
  90.  
  91.     # Y side of gradient
  92.     x2 = nx/2*math.cos(math.radians(45)*math.pi/180)+len1*math.cos(math.radians(angle_b)*math.pi/180)
  93.     y2 = ny/2*math.sin(math.radians(45)*math.pi/180)-len1*math.sin(math.radians(angle_b)*math.pi/180)
  94.  
  95.     k = 0.0
  96.     max_k = 1.0
  97.     if random.choice([True, False]):
  98.         max_k = max_k * -1.0
  99.  
  100.     x, y = 0, 0
  101.    
  102.     for i in range(nx):
  103.         x = i
  104.  
  105.         # distance (x, y) to (mouseX, mouseY)
  106.         if mouseX and mouseY:
  107.             dist = math.hypot(x-mouseX, y-mouseY)
  108.  
  109.         # 1. patterns animation
  110.         r = 255
  111.         g = 255
  112.         b = 255
  113.         # fading in
  114.         if pattern_animation == "fade_in":
  115.             k = (1.0 - (pattern_animation_end - time.time()) / pattern_animation_duration)
  116.             r = int(r * k)
  117.             g = int(g * k)
  118.             b = int(b * k)
  119.         # fading out
  120.         elif pattern_animation == "fade_out":
  121.             k =  (time.time() - pattern_animation_start) / pattern_animation_duration
  122.             r = int(r * k)
  123.             g = int(g * k)
  124.             b = int(b * k)
  125.         # rotation
  126.         elif pattern_animation == "rotate":
  127.             x = math.sin(rotation_angle)+i
  128.             k =  (time.time() - pattern_animation_start) / pattern_animation_duration
  129.             r = int(r * k)
  130.             g = int(g * k)
  131.             b = int(b * k)
  132.         # X side of scale (resize)
  133.         elif pattern_animation == "scale_x":
  134.             k =  (time.time() - pattern_animation_start) / pattern_animation_duration
  135.             x = math.sin((x_move_direction*math.pi/180)*i+math.radians(pattern_animation_scale*k*360*random.uniform(0.5, 1.0)))*i+i
  136.             r = int(r * k)
  137.             g = int(g * k)
  138.             b = int(b * k)
  139.         # Y side of scale (resize)
  140.         elif pattern_animation == "scale_y":
  141.             k =  (time.time() - pattern_animation_start) / pattern_animation_duration
  142.             y = math.cos((y_move_direction*math.pi/180)*i+math.radians(pattern_animation_scale*k*360*random.uniform(0.5, 1.0)))*i+y+i
  143.             r = int(r * k)
  144.             g = int(g * k)
  145.             b = int(b * k)
  146.         # RGB component of color (resize)
  147.         elif pattern_animation == "scale_rgb":
  148.             k =  (time.time() - pattern_animation_start) / pattern_animation_duration
  149.             r = int(r * (1.0+pattern_animation_scale*k*random.uniform(0.5, 1.0)))
  150.             g = int(g * (1.0+pattern_animation_scale*k*random.uniform(0.5, 1.0)))
  151.             b = int(b * (1.0+pattern_animation_scale*k*random.uniform(0.5, 1.0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement