roundabout,
created on Monday, 19 August 2024, 09:20:56 (1724059256),
received on Monday, 19 August 2024, 16:05:16 (1724083516)
Author identity: vlad <vlad.muntoiu@gmail.com>
20250acab1062b149c858a8ef0daed69408211f3
static/picture-annotation.py
@@ -1,9 +1,6 @@
from pyscript import document
from pyodide.ffi import create_proxy
# Remove the loading message
document.getElementById("python-loading").remove()
document.getElementById("shape-options").style.display = "flex"
image = document.getElementById("annotation-image")
@@ -20,20 +17,88 @@ def switch_shape(event):
print("Shape is now of type:", shape)
vertical_ruler = document.getElementById("annotation-ruler-vertical")
horizontal_ruler = document.getElementById("annotation-ruler-horizontal")
vertical_ruler_2 = document.getElementById("annotation-ruler-vertical-secondary")
horizontal_ruler_2 = document.getElementById("annotation-ruler-horizontal-secondary")
helper_message = document.getElementById("annotation-helper-message")
helper_message.innerText = "Select a shape type then click on the image to begin defining it"
def follow_cursor(event):
rect = zone.getBoundingClientRect()
x = event.clientX - rect.left
y = event.clientY - rect.top
vertical_ruler.style.left = str(x) + "px"
horizontal_ruler.style.top = str(y) + "px"
bbox_pos = None
def make_bbox(event):
global new_shape, bbox_pos
zone_rect = zone.getBoundingClientRect()
if bbox_pos is None:
bbox_pos = [event.clientX - zone_rect.left, event.clientY - zone_rect.top]
vertical_ruler_2.style.left = str(bbox_pos[0]) + "px"
horizontal_ruler_2.style.top = str(bbox_pos[1]) + "px"
vertical_ruler_2.style.display = "block"
horizontal_ruler_2.style.display = "block"
else:
x0, y0 = bbox_pos
x1, y1 = event.clientX - zone_rect.left, event.clientY - zone_rect.top
rectangle = document.createElementNS("http://www.w3.org/2000/svg", "rect")
minx = min(x0, x1)
miny = min(y0, y1)
maxx = max(x0, x1)
maxy = max(y0, y1)
rectangle.setAttribute("x", str(minx))
rectangle.setAttribute("y", str(miny))
rectangle.setAttribute("width", str(maxx - minx))
rectangle.setAttribute("height", str(maxy - miny))
rectangle.setAttribute("fill", "none")
rectangle.classList.add("shape-bbox")
new_shape.appendChild(rectangle)
zone.appendChild(new_shape)
vertical_ruler.style.display = "none"
horizontal_ruler.style.display = "none"
vertical_ruler_2.style.display = "none"
horizontal_ruler_2.style.display = "none"
zone.removeEventListener("click", create_proxy(make_bbox))
return
zone.addEventListener(
"mousemove",
create_proxy(follow_cursor)
)
new_shape = None
def open_shape(event):
global last_click_position, new_shape
print("Creating a new shape of type:", shape_type)
new_shape = document.createElementNS("http://www.w3.org/2000/svg", "svg")
new_shape.setAttribute("width", image.width)
new_shape.setAttribute("height", image.height)
if shape_type == "shape-bbox":
rectangle = document.createElementNS("http://www.w3.org/2000/svg", "rect")
rectangle.setAttribute("x", "10")
rectangle.setAttribute("y", "10")
rectangle.setAttribute("width", "100")
rectangle.setAttribute("height", "100")
rectangle.setAttribute("fill", "hotPink")
new_shape.appendChild(rectangle)
bbox_pos = None
zone.addEventListener(
"click",
create_proxy(make_bbox)
)
vertical_ruler.style.display = "block"
horizontal_ruler.style.display = "block"
zone.appendChild(new_shape)
static/style.css
@@ -25,3 +25,27 @@ iconify-icon {
top: 0;
left: 0;
}
.annotation-ruler {
z-index: 2;
position: absolute;
top: 0;
left: 0;
background: var(--color-accent);
display: none;
}
#annotation-ruler-horizontal, #annotation-ruler-horizontal-secondary {
height: 1px;
width: 100%;
}
#annotation-ruler-vertical, #annotation-ruler-vertical-secondary {
width: 1px;
height: 100%;
}
.shape-bbox {
stroke: var(--color-accent);
stroke-width: 2px;
}
templates/picture-annotation.html
@@ -11,7 +11,7 @@
<x-frame style="--width: 768px">
<h1>Annotating: {{ resource.title }}</h1>
<p id="python-loading">Please wait for Python to load...</p>
<p id="annotation-helper-message">Please wait for Python to load...</p>
</x-frame>
<x-frame is="main-area">
<x-buttonbox id="shape-selector">
@@ -45,6 +45,10 @@
</x-buttonbox>
<div id="annotation-zone">
<img id="annotation-image" src="/raw/picture/{{ resource.id }}" alt="">
<div id="annotation-ruler-vertical" class="annotation-ruler"></div>
<div id="annotation-ruler-horizontal" class="annotation-ruler"></div>
<div id="annotation-ruler-vertical-secondary" class="annotation-ruler"></div>
<div id="annotation-ruler-horizontal-secondary" class="annotation-ruler"></div>
</div>
<x-buttonbox style="display: none;" id="shape-options">
<button class="button-flat" title="cancel" disabled>