Skip to content

Instantly share code, notes, and snippets.

@leovoel
Created November 11, 2016 20:02
Show Gist options
  • Save leovoel/1c9bda13f5ec6b911e26628664d6a049 to your computer and use it in GitHub Desktop.
Save leovoel/1c9bda13f5ec6b911e26628664d6a049 to your computer and use it in GitHub Desktop.
from math import sqrt, cos, sin, pi, radians
from glc import Gif
from glc.color import Color
from glc.utils import draw_image
from cairo import ImageSurface
def intersection(x, y, rx, ry):
px, py = 0, 0
if x == 0:
py = -ry if y < 0 else ry
elif y == 0:
px = -rx if x < 0 else rx
elif x != 0 and y != 0:
m = y / x
n1 = ry / (rx * m)
px = (ry / m) * sqrt(1 / (1 + n1 * n1))
px = -px if y < 0 else px
py = m * px
return px, py
def oval(context, x, y, rx, ry, color):
context.save()
context.translate(x, y)
context.scale(rx * 0.01, ry * 0.01)
context.arc(0, 0, 100, 0, 2 * pi)
context.set_source_rgba(*color)
context.fill()
context.restore()
def line(context, x0, y0, x1, y1, color, line_width=5):
context.save()
context.move_to(x0, y0)
context.line_to(x1, y1)
context.set_source_rgba(*color)
context.set_line_width(line_width)
context.stroke()
context.restore()
with Gif('eyefunny.gif') as a:
a.set_bg_color('0xff36393e').set_duration(6)
w, h = a.w, a.h
cx, cy = w * 0.5, h * 0.5
l = a.render_list
with open('../dev/72x72/1f528.png', 'rb') as f:
emoji = ImageSurface.create_from_png(f)
emojiw, emojih = emoji.get_width(), emoji.get_height()
def before_render(render_list, surface, context, t):
leadr = w * 0.3
leadx, leady = cx + cos(2 * pi * t) + sin(4 * pi * t) * leadr, cy + sin(2 * pi * t) * leadr
step = w // 10
size = step * 0.5
eyerx, eyery = size * 0.5, size * 0.6
start_x, start_y = int(eyerx), int(eyery)
stepx = step + int(start_x * 1.5)
eye_sep = size * 0.6
offsetx, offsety = 35, 10
for i in range(start_x + offsetx, w - offsetx, stepx):
for j in range(start_y + offsety, h - offsety, step):
ax, ay = i - eye_sep, j
bx, by = i + eye_sep, j
air = size * 0.3
aix, aiy = intersection(leadx - ax - air * 1.5, leady - ay, eyerx - air, eyery - air)
oval(context, ax, ay, eyerx, eyery, Color('white'))
oval(context, aix + ax, aiy + ay, air, air, Color('0x36393e'))
bix, biy = intersection(leadx - bx + air * 1.5, leady - by, eyerx - air, eyery - air)
oval(context, bx, by, eyerx, eyery, Color('white'))
oval(context, bix + bx, biy + by, air, air, Color('0x36393e'))
# line(context, bix + bx, biy + by, leadx, leady, Color('red'), 1)
# line(context, aix + ax, aiy + ay, leadx, leady, Color('red'), 1)
draw_image(context, emoji, leadx - emojiw * 0.5, leady - emojih * 0.5)
l.before_render = before_render
a.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment