Skip to content

Instantly share code, notes, and snippets.

@salt-die
Created April 8, 2020 13:28
Show Gist options
  • Save salt-die/361bab5e274ed02ae8267b6c0de2ebad to your computer and use it in GitHub Desktop.
Save salt-die/361bab5e274ed02ae8267b6c0de2ebad to your computer and use it in GitHub Desktop.
"""
Move bulge by dragging the mouse. Scale with multitouch.
"""
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.effectwidget import AdvancedEffectBase
from kivy.lang import Builder
EFFECT = AdvancedEffectBase()
EFFECT.glsl = """
uniform vec2 position;
uniform float radius;
uniform float angle;
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords){
vec2 relative_position = position - tex_coords;
vec2 smoosh = -radius * log(length(relative_position)) * normalize(relative_position) / 3.0;
return texture2D(texture, tex_coords + smoosh);}
"""
EFFECT.uniforms = {'position': (.5, .5), 'radius': .07,}
KV = """
#:import EFFECT __main__.EFFECT
EffectWidget:
effects: EFFECT,
Image:
source: 'python_discord_logo.png'
"""
class Bulge(App):
def build(self):
self._touches = []
Window.bind(on_touch_up=self._on_touch_up,
on_touch_down=self._on_touch_down,
on_touch_move=self._on_touch_move)
return Builder.load_string(KV)
def transform_on_touch(self, touch):
ax, ay = self._touches[-2].pos
cx = (touch.x - ax) / Window.width
cy = (touch.y - ay) / Window.height
current_length = (cx**2 + cy**2)**.5
px = (touch.px - ax) / Window.width
py = (touch.py - ay) / Window.height
previous_length = (px**2 + py**2)**.5
EFFECT.uniforms['radius'] += current_length - previous_length
def _on_touch_up(self, _, touch):
self._touches.remove(touch)
def _on_touch_down(self, _, touch):
if touch.button == 'left' and not self._touches:
EFFECT.uniforms['position'] = touch.spos
self._touches.append(touch)
def _on_touch_move(self, _, touch):
if touch.button == 'left':
if len(self._touches) == 1:
EFFECT.uniforms['position'] = touch.spos
else:
self.transform_on_touch(touch)
if __name__ == '__main__':
Bulge().run()
@salt-die
Copy link
Author

salt-die commented Apr 8, 2020

python_discord_logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment