Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,12 @@ from bokeh.plotting import figure
|
|
4 |
from bokeh.models import ColumnDataSource
|
5 |
from bokeh.events import Tap
|
6 |
|
7 |
-
Constants
|
8 |
WIDTH, HEIGHT = 600, 600
|
9 |
ELASTICITY = 0.3
|
10 |
DAMPING = 0.7
|
11 |
|
12 |
-
Create touch points
|
13 |
num_points = 20
|
14 |
x = np.linspace(50, WIDTH-50, num_points)
|
15 |
y = np.linspace(50, HEIGHT-50, num_points)
|
@@ -21,51 +21,50 @@ velocities = np.zeros_like(touch_points)
|
|
21 |
|
22 |
source = ColumnDataSource(data=dict(x=touch_points[:, 0], y=touch_points[:, 1]))
|
23 |
|
24 |
-
Set up the Bokeh plot
|
25 |
p = figure(width=WIDTH, height=HEIGHT, tools="", toolbar_location=None)
|
26 |
p.circle('x', 'y', size=5, color="navy", alpha=0.5, source=source)
|
27 |
|
28 |
-
Streamlit app
|
29 |
st.title("Artificial Touch Simulation")
|
30 |
|
31 |
-
Create a Streamlit container for the Bokeh plot
|
32 |
chart = st.bokeh_chart(p)
|
33 |
|
34 |
def update_points():
|
35 |
-
global touch_points, velocities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
# Apply spring force
|
39 |
-
force = (original_points - touch_points) * ELASTICITY
|
40 |
-
|
41 |
-
# Update velocity
|
42 |
-
velocities += force
|
43 |
-
|
44 |
-
# Apply damping
|
45 |
-
velocities *= DAMPING
|
46 |
-
|
47 |
-
# Update position
|
48 |
-
touch_points += velocities
|
49 |
-
|
50 |
-
# Update Bokeh data source
|
51 |
-
source.data = dict(x=touch_points[:, 0], y=touch_points[:, 1])
|
52 |
def on_tap(event):
|
53 |
-
global touch_points, velocities
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
st.write(f"Touch at ({x:.2f}, {y:.2f})")
|
64 |
-
update_points()
|
65 |
p.on_event(Tap, on_tap)
|
66 |
|
67 |
-
Main loop
|
68 |
while True:
|
69 |
-
update_points()
|
70 |
-
chart.bokeh_chart(p)
|
71 |
-
st.experimental_rerun()
|
|
|
4 |
from bokeh.models import ColumnDataSource
|
5 |
from bokeh.events import Tap
|
6 |
|
7 |
+
# Constants
|
8 |
WIDTH, HEIGHT = 600, 600
|
9 |
ELASTICITY = 0.3
|
10 |
DAMPING = 0.7
|
11 |
|
12 |
+
# Create touch points
|
13 |
num_points = 20
|
14 |
x = np.linspace(50, WIDTH-50, num_points)
|
15 |
y = np.linspace(50, HEIGHT-50, num_points)
|
|
|
21 |
|
22 |
source = ColumnDataSource(data=dict(x=touch_points[:, 0], y=touch_points[:, 1]))
|
23 |
|
24 |
+
# Set up the Bokeh plot
|
25 |
p = figure(width=WIDTH, height=HEIGHT, tools="", toolbar_location=None)
|
26 |
p.circle('x', 'y', size=5, color="navy", alpha=0.5, source=source)
|
27 |
|
28 |
+
# Streamlit app
|
29 |
st.title("Artificial Touch Simulation")
|
30 |
|
31 |
+
# Create a Streamlit container for the Bokeh plot
|
32 |
chart = st.bokeh_chart(p)
|
33 |
|
34 |
def update_points():
|
35 |
+
global touch_points, velocities
|
36 |
+
|
37 |
+
# Apply spring force
|
38 |
+
force = (original_points - touch_points) * ELASTICITY
|
39 |
+
|
40 |
+
# Update velocity
|
41 |
+
velocities += force
|
42 |
+
|
43 |
+
# Apply damping
|
44 |
+
velocities *= DAMPING
|
45 |
+
|
46 |
+
# Update position
|
47 |
+
touch_points += velocities
|
48 |
+
|
49 |
+
# Update Bokeh data source
|
50 |
+
source.data = dict(x=touch_points[:, 0], y=touch_points[:, 1])
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
def on_tap(event):
|
53 |
+
global touch_points, velocities
|
54 |
+
|
55 |
+
x, y = event.x, event.y
|
56 |
+
distances = np.sqrt(np.sum((touch_points - [x, y])**2, axis=1))
|
57 |
+
affected = distances < 30
|
58 |
+
|
59 |
+
force = (touch_points[affected] - [x, y]) / distances[affected, np.newaxis]
|
60 |
+
velocities[affected] -= force * 10
|
61 |
+
|
62 |
+
st.write(f"Touch at ({x:.2f}, {y:.2f})")
|
63 |
+
update_points()
|
64 |
|
|
|
|
|
65 |
p.on_event(Tap, on_tap)
|
66 |
|
|
|
67 |
while True:
|
68 |
+
update_points()
|
69 |
+
chart.bokeh_chart(p)
|
70 |
+
st.experimental_rerun()
|