Spaces:
Runtime error
Runtime error
add translate/rotate/scale
Browse files- app.py +1 -0
- static/poseEditor.js +39 -8
app.py
CHANGED
@@ -53,6 +53,7 @@ with gr.Blocks() as demo:
|
|
53 |
height = gr.Slider(label="Height", mininmum=512, maximum=1024, step=64, value=512, key="Height", interactive=True)
|
54 |
startBtn = gr.Button(value="Start edit")
|
55 |
with gr.Column(scale=2):
|
|
|
56 |
html = gr.HTML(html_text)
|
57 |
saveBtn = gr.Button(value="Save")
|
58 |
|
|
|
53 |
height = gr.Slider(label="Height", mininmum=512, maximum=1024, step=64, value=512, key="Height", interactive=True)
|
54 |
startBtn = gr.Button(value="Start edit")
|
55 |
with gr.Column(scale=2):
|
56 |
+
gr.HTML("<ul><li>ctrl + drag to scale</li><li>alt + drag to translate</li><li>shift + drag to rotate(move right first, then up or down)</li></ul>")
|
57 |
html = gr.HTML(html_text)
|
58 |
saveBtn = gr.Button(value="Save")
|
59 |
|
static/poseEditor.js
CHANGED
@@ -109,8 +109,9 @@ function getNearestCandidateIndex(x, y) {
|
|
109 |
// ドラッグ中に座標を保持するための変数
|
110 |
let isDragging = false;
|
111 |
let dragIndex = -1;
|
112 |
-
let
|
113 |
-
let
|
|
|
114 |
|
115 |
function getCanvasPosition(event) {
|
116 |
const rect = canvas.getBoundingClientRect();
|
@@ -125,11 +126,12 @@ function handleMouseDown(event) {
|
|
125 |
const index = getNearestCandidateIndex(x, y);
|
126 |
|
127 |
// ドラッグ処理の開始
|
128 |
-
if (0 <= index) {
|
129 |
isDragging = true;
|
130 |
dragIndex = index;
|
131 |
-
|
132 |
-
|
|
|
133 |
}
|
134 |
}
|
135 |
|
@@ -141,9 +143,38 @@ function handleMouseMove(event) {
|
|
141 |
|
142 |
const [x, y] = getCanvasPosition(event);
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
clearCanvas();
|
149 |
drawBodyPose(candidate, subset);
|
|
|
109 |
// ドラッグ中に座標を保持するための変数
|
110 |
let isDragging = false;
|
111 |
let dragIndex = -1;
|
112 |
+
let dragStartX = 0;
|
113 |
+
let dragStartY = 0;
|
114 |
+
let draggingCandidate = null;
|
115 |
|
116 |
function getCanvasPosition(event) {
|
117 |
const rect = canvas.getBoundingClientRect();
|
|
|
126 |
const index = getNearestCandidateIndex(x, y);
|
127 |
|
128 |
// ドラッグ処理の開始
|
129 |
+
if (0 <= index || event.altKey || event.ctrlKey || event.shiftKey) {
|
130 |
isDragging = true;
|
131 |
dragIndex = index;
|
132 |
+
dragStartX = x;
|
133 |
+
dragStartY = y;
|
134 |
+
draggingCandidate = JSON.parse(JSON.stringify(candidate))
|
135 |
}
|
136 |
}
|
137 |
|
|
|
143 |
|
144 |
const [x, y] = getCanvasPosition(event);
|
145 |
|
146 |
+
const dragOffsetX = x - dragStartX;
|
147 |
+
const dragOffsetY = y - dragStartY;
|
148 |
+
|
149 |
+
|
150 |
+
if (event.ctrlKey) {
|
151 |
+
let xScale = 1 + dragOffsetX / canvas.width;
|
152 |
+
let yScale = 1 + dragOffsetY / canvas.height;
|
153 |
+
for (let i = 0 ; i < candidate.length; i++) {
|
154 |
+
candidate[i][0] = (draggingCandidate[i][0] - dragStartX) * xScale + dragStartX;
|
155 |
+
candidate[i][1] = (draggingCandidate[i][1] - dragStartY) * yScale + dragStartY;
|
156 |
+
}
|
157 |
+
} else if (event.shiftKey) {
|
158 |
+
// rotate
|
159 |
+
let angle = Math.atan2(dragOffsetY, dragOffsetX);
|
160 |
+
for (let i = 0 ; i < candidate.length; i++) {
|
161 |
+
let x = draggingCandidate[i][0] - dragStartX;
|
162 |
+
let y = draggingCandidate[i][1] - dragStartY;
|
163 |
+
candidate[i][0] = x * Math.cos(angle) - y * Math.sin(angle) + dragStartX;
|
164 |
+
candidate[i][1] = x * Math.sin(angle) + y * Math.cos(angle) + dragStartY;
|
165 |
+
}
|
166 |
+
|
167 |
+
} else if (event.altKey) {
|
168 |
+
// 全体移動
|
169 |
+
for (let i = 0 ; i < candidate.length; i++) {
|
170 |
+
candidate[i][0] = draggingCandidate[i][0] + dragOffsetX;
|
171 |
+
candidate[i][1] = draggingCandidate[i][1] + dragOffsetY;
|
172 |
+
}
|
173 |
+
} else {
|
174 |
+
// 個別移動
|
175 |
+
candidate[dragIndex][0] = draggingCandidate[dragIndex][0] + dragOffsetX;
|
176 |
+
candidate[dragIndex][1] = draggingCandidate[dragIndex][1] + dragOffsetY;
|
177 |
+
}
|
178 |
|
179 |
clearCanvas();
|
180 |
drawBodyPose(candidate, subset);
|