Spaces:
Sleeping
Sleeping
Commit
β’
8c826e3
1
Parent(s):
770f9e5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
|
5 |
+
# Initialize session state
|
6 |
+
if 'camera_offset' not in st.session_state:
|
7 |
+
st.session_state.camera_offset = {"x": 0, "y": 0}
|
8 |
+
if 'objects' not in st.session_state:
|
9 |
+
st.session_state.objects = []
|
10 |
+
|
11 |
+
def update_canvas(direction=None, new_x=None, new_y=None, add_object=False, drag_x=0, drag_y=0):
|
12 |
+
if direction:
|
13 |
+
speed = 5
|
14 |
+
if direction == "up":
|
15 |
+
st.session_state.camera_offset["y"] -= speed
|
16 |
+
elif direction == "down":
|
17 |
+
st.session_state.camera_offset["y"] += speed
|
18 |
+
elif direction == "left":
|
19 |
+
st.session_state.camera_offset["x"] -= speed
|
20 |
+
elif direction == "right":
|
21 |
+
st.session_state.camera_offset["x"] += speed
|
22 |
+
|
23 |
+
if new_x is not None and new_y is not None:
|
24 |
+
st.session_state.camera_offset["x"] = int(new_x) - 300
|
25 |
+
st.session_state.camera_offset["y"] = int(new_y) - 200
|
26 |
+
|
27 |
+
if add_object:
|
28 |
+
new_object = {
|
29 |
+
"x": st.session_state.camera_offset["x"] + 300,
|
30 |
+
"y": st.session_state.camera_offset["y"] + 200,
|
31 |
+
"size": 10,
|
32 |
+
"color": f"rgb({random.randint(0,255)},{random.randint(0,255)},{random.randint(0,255)})"
|
33 |
+
}
|
34 |
+
st.session_state.objects.append(new_object)
|
35 |
+
|
36 |
+
st.session_state.camera_offset["x"] += int(drag_x)
|
37 |
+
st.session_state.camera_offset["y"] += int(drag_y)
|
38 |
+
|
39 |
+
def render_canvas():
|
40 |
+
state = json.dumps({
|
41 |
+
"cameraOffset": st.session_state.camera_offset,
|
42 |
+
"objects": st.session_state.objects
|
43 |
+
})
|
44 |
+
|
45 |
+
return f"""
|
46 |
+
<div id="canvasContainer">
|
47 |
+
<canvas id="infiniteCanvas" width="600" height="400" style="border: 1px solid black; cursor: grab;"></canvas>
|
48 |
+
<p id="coordinates">Player coordinates: X: {300 + st.session_state.camera_offset['x']}, Y: {200 + st.session_state.camera_offset['y']}</p>
|
49 |
+
</div>
|
50 |
+
<script>
|
51 |
+
let cameraOffset = {st.session_state.camera_offset};
|
52 |
+
let objects = {json.dumps(st.session_state.objects)};
|
53 |
+
const playerSize = 20;
|
54 |
+
const gridSize = 50;
|
55 |
+
const canvas = document.getElementById('infiniteCanvas');
|
56 |
+
const ctx = canvas.getContext('2d');
|
57 |
+
const coordsDisplay = document.getElementById('coordinates');
|
58 |
+
|
59 |
+
function drawGrid() {{
|
60 |
+
ctx.strokeStyle = '#ddd';
|
61 |
+
ctx.lineWidth = 0.5;
|
62 |
+
for (let x = -cameraOffset.x % gridSize; x < canvas.width; x += gridSize) {{
|
63 |
+
ctx.beginPath();
|
64 |
+
ctx.moveTo(x, 0);
|
65 |
+
ctx.lineTo(x, canvas.height);
|
66 |
+
ctx.stroke();
|
67 |
+
}}
|
68 |
+
for (let y = -cameraOffset.y % gridSize; y < canvas.height; y += gridSize) {{
|
69 |
+
ctx.beginPath();
|
70 |
+
ctx.moveTo(0, y);
|
71 |
+
ctx.lineTo(canvas.width, y);
|
72 |
+
ctx.stroke();
|
73 |
+
}}
|
74 |
+
}}
|
75 |
+
|
76 |
+
function drawPlayer() {{
|
77 |
+
ctx.fillStyle = 'red';
|
78 |
+
ctx.fillRect(canvas.width / 2 - playerSize / 2, canvas.height / 2 - playerSize / 2, playerSize, playerSize);
|
79 |
+
}}
|
80 |
+
|
81 |
+
function drawObjects() {{
|
82 |
+
objects.forEach(obj => {{
|
83 |
+
ctx.fillStyle = obj.color;
|
84 |
+
ctx.fillRect(
|
85 |
+
obj.x - cameraOffset.x,
|
86 |
+
obj.y - cameraOffset.y,
|
87 |
+
obj.size,
|
88 |
+
obj.size
|
89 |
+
);
|
90 |
+
}});
|
91 |
+
}}
|
92 |
+
|
93 |
+
function draw() {{
|
94 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
95 |
+
drawGrid();
|
96 |
+
drawObjects();
|
97 |
+
drawPlayer();
|
98 |
+
}}
|
99 |
+
|
100 |
+
draw();
|
101 |
+
</script>
|
102 |
+
"""
|
103 |
+
|
104 |
+
st.title("Infinite Canvas")
|
105 |
+
|
106 |
+
# Render canvas
|
107 |
+
st.components.v1.html(render_canvas(), height=450)
|
108 |
+
|
109 |
+
# Control buttons
|
110 |
+
col1, col2, col3, col4 = st.columns(4)
|
111 |
+
with col1:
|
112 |
+
if st.button("β"):
|
113 |
+
update_canvas(direction="up")
|
114 |
+
with col2:
|
115 |
+
if st.button("β"):
|
116 |
+
update_canvas(direction="down")
|
117 |
+
with col3:
|
118 |
+
if st.button("β"):
|
119 |
+
update_canvas(direction="left")
|
120 |
+
with col4:
|
121 |
+
if st.button("β"):
|
122 |
+
update_canvas(direction="right")
|
123 |
+
|
124 |
+
# Go to coordinates
|
125 |
+
col1, col2, col3 = st.columns(3)
|
126 |
+
with col1:
|
127 |
+
new_x = st.number_input("X Coordinate", value=300 + st.session_state.camera_offset['x'])
|
128 |
+
with col2:
|
129 |
+
new_y = st.number_input("Y Coordinate", value=200 + st.session_state.camera_offset['y'])
|
130 |
+
with col3:
|
131 |
+
if st.button("Go to Coordinates"):
|
132 |
+
update_canvas(new_x=new_x, new_y=new_y)
|
133 |
+
|
134 |
+
# Add object button
|
135 |
+
if st.button("Add Object"):
|
136 |
+
update_canvas(add_object=True)
|
137 |
+
|
138 |
+
# Drag sliders
|
139 |
+
drag_x = st.slider("Drag X", -50, 50, 0)
|
140 |
+
drag_y = st.slider("Drag Y", -50, 50, 0)
|
141 |
+
|
142 |
+
if drag_x != 0 or drag_y != 0:
|
143 |
+
update_canvas(drag_x=drag_x, drag_y=drag_y)
|
144 |
+
st.experimental_rerun()
|
145 |
+
|
146 |
+
# Force rerun to update canvas
|
147 |
+
if st.button("Update Canvas"):
|
148 |
+
st.experimental_rerun()
|