Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_pattern_art_animation.py ZZZ
- from tkinter import *
- from PIL import Image, ImageTk
- import time
- import random
- import math
- nx = 500
- ny = 500
- root = Tk()
- root.title("")
- root.geometry("%dx%d+50+50"%(nx,ny))
- canvas = Canvas(root, width=nx, height=ny)
- canvas.place(x=0, y=0)
- def RGB__(rgb):
- r,g,b = [int(i) for i in rgb]
- return "#%02x%02x%02x" % (r,g,b)
- mouseX, mouseY = None, None
- def mouse_click(event):
- global mouseX, mouseY
- mouseX, mouseY = event.x, event.y
- def mouse_release(event):
- global mouseX, mouseY
- mouseX, mouseY = None, None
- def mouse_drag(event):
- global mouseX, mouseY
- if mouseX and mouseY:
- mouseX, mouseY = event.x, event.y
- canvas.bind("<Button-1>", mouse_click)
- canvas.bind("<Button-3>", mouse_release)
- canvas.bind("<B1-Motion>", mouse_drag)
- def display():
- canvas.create_rectangle((x,y,x+1,y+1), fill=RGB__(rgb), outline='')
- while 1: # infinite loop
- # random color
- rgb = [random.randint(0, 255) for i in range(3)]
- # patterns animation
- pattern_animation_duration = random.uniform(0.1, 2.0) # duration time of animation
- pattern_animation_end = time.time() + pattern_animation_duration
- pattern_animation_start = time.time()
- pattern_animation_scale = random.uniform(0.01, 0.01) # scale of animation
- pattern_animation = random.choice(["fade_in", "fade_out", "rotate", "scale_x", "scale_y", "scale_rgb", "scale_alpha", "scale_all", "gradient", "xy_flip"])
- # rotation angle
- rotation_angle = random.random() * math.pi * 2
- # triangle points
- triangle_points = [random.randint(0, nx) for i in range(6)]
- # polygon points
- polygon_points = [random.randint(0, nx) for i in range(random.randint(6, 10)*2)]
- # parameters
- alpha = random.randint(1, 255)
- fill_opacity = random.uniform(0.5, 1.0)
- outline_opacity = random.uniform(0.5, 1.0)
- width = random.randint(1, 10)
- fill = RGB__(rgb)
- outline = RGB__(rgb)
- # first color (r, g, b) of gradient
- rgb_a = [random.randint(0, 255) for i in range(3)]
- # second color (r, g, b) of gradient
- rgb_b = [random.randint(0, 255) for i in range(3)]
- # first angle of gradient
- angle_a = random.randint(0, 360)
- # second angle of gradient
- angle_b = random.randint(0, 360)
- # length of gradient
- len1 = math.hypot(nx, ny)
- # X side of gradient
- x1 = nx/2*math.cos(math.radians(45)*math.pi/180)+len1*math.cos(math.radians(angle_a)*math.pi/180)
- y1 = ny/2*math.sin(math.radians(45)*math.pi/180)-len1*math.sin(math.radians(angle_a)*math.pi/180)
- # Y side of gradient
- x2 = nx/2*math.cos(math.radians(45)*math.pi/180)+len1*math.cos(math.radians(angle_b)*math.pi/180)
- y2 = ny/2*math.sin(math.radians(45)*math.pi/180)-len1*math.sin(math.radians(angle_b)*math.pi/180)
- k = 0.0
- max_k = 1.0
- if random.choice([True, False]):
- max_k = max_k * -1.0
- x, y = 0, 0
- for i in range(nx):
- x = i
- # distance (x, y) to (mouseX, mouseY)
- if mouseX and mouseY:
- dist = math.hypot(x-mouseX, y-mouseY)
- # 1. patterns animation
- r = 255
- g = 255
- b = 255
- # fading in
- if pattern_animation == "fade_in":
- k = (1.0 - (pattern_animation_end - time.time()) / pattern_animation_duration)
- r = int(r * k)
- g = int(g * k)
- b = int(b * k)
- # fading out
- elif pattern_animation == "fade_out":
- k = (time.time() - pattern_animation_start) / pattern_animation_duration
- r = int(r * k)
- g = int(g * k)
- b = int(b * k)
- # rotation
- elif pattern_animation == "rotate":
- x = math.sin(rotation_angle)+i
- k = (time.time() - pattern_animation_start) / pattern_animation_duration
- r = int(r * k)
- g = int(g * k)
- b = int(b * k)
- # X side of scale (resize)
- elif pattern_animation == "scale_x":
- k = (time.time() - pattern_animation_start) / pattern_animation_duration
- 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
- r = int(r * k)
- g = int(g * k)
- b = int(b * k)
- # Y side of scale (resize)
- elif pattern_animation == "scale_y":
- k = (time.time() - pattern_animation_start) / pattern_animation_duration
- 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
- r = int(r * k)
- g = int(g * k)
- b = int(b * k)
- # RGB component of color (resize)
- elif pattern_animation == "scale_rgb":
- k = (time.time() - pattern_animation_start) / pattern_animation_duration
- r = int(r * (1.0+pattern_animation_scale*k*random.uniform(0.5, 1.0)))
- g = int(g * (1.0+pattern_animation_scale*k*random.uniform(0.5, 1.0)))
- 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