File size: 12,684 Bytes
29580f3 3278e3c 29580f3 2a6062c 29580f3 df4085a 29580f3 07b52cf d775f14 3278e3c dfd748f 3278e3c 29580f3 2a6062c 29580f3 d775f14 29580f3 d775f14 2a6062c 29580f3 d775f14 29580f3 2a6062c 29580f3 dfd748f 29580f3 d775f14 29580f3 dfd748f 29580f3 d775f14 2a6062c d775f14 07b52cf 2a6062c 07b52cf 2a6062c 07b52cf 2a6062c 07b52cf 2a6062c d775f14 dfd748f d775f14 2a6062c 07b52cf 29580f3 2a6062c 29580f3 07b52cf 29580f3 2a6062c 29580f3 2a6062c 29580f3 dfd748f 29580f3 02bc5ce 29580f3 2a6062c 29580f3 df4085a 29580f3 02bc5ce 29580f3 d0f5140 29580f3 d0f5140 29580f3 dfd748f d0f5140 29580f3 d775f14 dfd748f 29580f3 2a6062c 29580f3 2a6062c 29580f3 3278e3c 29580f3 2a6062c 29580f3 2a6062c 29580f3 d775f14 29580f3 d775f14 29580f3 2a6062c 29580f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
<script lang="ts">
import { onMount, onDestroy, createEventDispatcher } from "svelte";
import { BoundingBox, Hand, Trash } from "./icons/index";
import ModalBox from "./ModalBox.svelte";
import Box from "./Box";
import { Colors } from './Colors.js';
import AnnotatedImageData from "./AnnotatedImageData";
enum Mode {creation, drag}
export let imageUrl: string | null = null;
export let interactive: boolean;
export let boxAlpha = 0.5;
export let boxMinSize = 25;
export let handleSize: number;
export let boxThickness: number;
export let boxSelectedThickness: number;
export let value: null | AnnotatedImageData;
export let choices = [];
export let choicesColors = [];
export let disableEditBoxes: boolean = false;
export let singleBox: boolean = false;
export let showRemoveButton: boolean = null;
export let handlesCursor: boolean = true;
if (showRemoveButton === null) {
showRemoveButton = (disableEditBoxes);
}
let canvas: HTMLCanvasElement;
let ctx: CanvasRenderingContext2D;
let image = null;
let selectedBox = -1;
let mode: Mode = Mode.drag;
if (value !== null && value.boxes.length == 0) {
mode = Mode.creation;
}
let canvasXmin = 0;
let canvasYmin = 0;
let canvasXmax = 0;
let canvasYmax = 0;
let scaleFactor = 1.0;
let imageWidth = 0;
let imageHeight = 0;
let editModalVisible = false;
let newModalVisible = false;
const dispatch = createEventDispatcher<{
change: undefined;
}>();
function colorHexToRGB(hex: string) {
var r = parseInt(hex.slice(1, 3), 16),
g = parseInt(hex.slice(3, 5), 16),
b = parseInt(hex.slice(5, 7), 16);
return "rgb(" + r + ", " + g + ", " + b + ")";
}
function colorRGBAToHex(rgba: string) {
const rgbaValues = rgba.match(/(\d+(\.\d+)?)/g);
const r = parseInt(rgbaValues[0]);
const g = parseInt(rgbaValues[1]);
const b = parseInt(rgbaValues[2]);
const hex = "#" + ((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1);
return hex;
}
function draw() {
if (ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (image !== null){
ctx.drawImage(image, canvasXmin, canvasYmin, imageWidth, imageHeight);
}
for (const box of value.boxes.slice().reverse()) {
box.render(ctx);
}
}
}
function selectBox(index: number) {
selectedBox = index;
value.boxes.forEach(box => {box.setSelected(false);});
if (index >= 0 && index < value.boxes.length){
value.boxes[index].setSelected(true);
}
draw();
}
function handlePointerDown(event: PointerEvent) {
if (!interactive) {
return;
}
if (
event.target instanceof Element &&
event.target.hasPointerCapture(event.pointerId)
) {
event.target.releasePointerCapture(event.pointerId);
}
if (mode === Mode.creation) {
createBox(event);
} else if (mode === Mode.drag) {
clickBox(event);
}
}
function clickBox(event: PointerEvent) {
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
// Check if the mouse is over any of the resizing handles
for (const [i, box] of value.boxes.entries()) {
const handleIndex = box.indexOfPointInsideHandle(mouseX, mouseY);
if (handleIndex >= 0) {
selectBox(i);
box.startResize(handleIndex, event);
return;
}
}
// Check if the mouse is inside a box
for (const [i, box] of value.boxes.entries()) {
if (box.isPointInsideBox(mouseX, mouseY)) {
selectBox(i);
box.startDrag(event);
return;
}
}
if (!singleBox) {
selectBox(-1);
}
}
function handlePointerUp(event: PointerEvent) {
dispatch("change");
}
function handlePointerMove(event: PointerEvent) {
if (value === null) {
return;
}
if (mode !== Mode.drag) {
return;
}
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
for (const [_, box] of value.boxes.entries()) {
const handleIndex = box.indexOfPointInsideHandle(mouseX, mouseY);
if (handleIndex >= 0) {
canvas.style.cursor = box.resizeHandles[handleIndex].cursor;
return;
}
}
canvas.style.cursor = "default";
}
function handleKeyPress(event: KeyboardEvent) {
if (!interactive) {
return;
}
switch (event.key) {
case "Delete":
onDeleteBox();
break;
}
}
function createBox(event: PointerEvent) {
const rect = canvas.getBoundingClientRect();
const x = (event.clientX - rect.left - canvasXmin) / scaleFactor;
const y = (event.clientY - rect.top - canvasYmin) / scaleFactor;
let color;
if (choicesColors.length > 0) {
color = colorHexToRGB(choicesColors[0]);
} else if (singleBox) {
if (value.boxes.length > 0) {
color = value.boxes[0].color;
} else {
color = Colors[0];
}
} else {
color = Colors[value.boxes.length % Colors.length];
}
let box = new Box(
draw,
onBoxFinishCreation,
canvasXmin,
canvasYmin,
canvasXmax,
canvasYmax,
"",
x,
y,
x,
y,
color,
boxAlpha,
boxMinSize,
handleSize,
boxThickness,
boxSelectedThickness
);
box.startCreating(event, rect.left, rect.top);
if (singleBox) {
value.boxes = [box];
setDragMode();
} else {
value.boxes = [box, ...value.boxes];
}
selectBox(0);
draw();
dispatch("change");
}
function setCreateMode() {
mode = Mode.creation;
canvas.style.cursor = "crosshair";
}
function setDragMode() {
mode = Mode.drag;
canvas.style.cursor = "default";
}
function onBoxFinishCreation() {
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
if (value.boxes[selectedBox].getArea() < 1) {
onDeleteBox();
} else if (!disableEditBoxes){
newModalVisible = true;
}
}
}
function onEditBox() {
if (selectedBox >= 0 && selectedBox < value.boxes.length && !disableEditBoxes) {
editModalVisible = true;
}
}
function handleDoubleClick(event: MouseEvent){
if (!interactive) {
return;
}
onEditBox();
}
function onModalEditChange(event) {
editModalVisible = false;
const { detail } = event;
let label = detail.label;
let color = detail.color;
let ret = detail.ret;
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
let box = value.boxes[selectedBox];
if (ret == 1) {
box.label = label;
box.color = colorHexToRGB(color);
draw();
dispatch("change");
} else if (ret == -1) {
onDeleteBox();
}
}
}
function onModalNewChange(event) {
newModalVisible = false;
const { detail } = event;
let label = detail.label;
let color = detail.color;
let ret = detail.ret;
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
let box = value.boxes[selectedBox];
if (ret == 1) {
box.label = label;
box.color = colorHexToRGB(color);
draw();
dispatch("change");
} else {
onDeleteBox();
}
}
}
function onDeleteBox() {
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
value.boxes.splice(selectedBox, 1);
selectBox(-1);
if (singleBox) {
setCreateMode();
}
dispatch("change");
}
}
function resize() {
if (canvas) {
scaleFactor = 1;
canvas.width = canvas.clientWidth;
if (image !== null) {
if (image.width > canvas.width) {
scaleFactor = canvas.width / image.width;
imageWidth = image.width * scaleFactor;
imageHeight = image.height * scaleFactor;
canvasXmin = 0;
canvasYmin = 0;
canvasXmax = imageWidth;
canvasYmax = imageHeight;
canvas.height = imageHeight;
} else {
imageWidth = image.width;
imageHeight = image.height;
var x = (canvas.width - imageWidth) / 2;
canvasXmin = x;
canvasYmin = 0;
canvasXmax = x + imageWidth;
canvasYmax = image.height;
canvas.height = imageHeight;
}
} else {
canvasXmin = 0;
canvasYmin = 0;
canvasXmax = canvas.width;
canvasYmax = canvas.height;
canvas.height = canvas.clientHeight;
}
if (canvasXmax > 0 && canvasYmax > 0){
for (const box of value.boxes) {
box.canvasXmin = canvasXmin;
box.canvasYmin = canvasYmin;
box.canvasXmax = canvasXmax;
box.canvasYmax = canvasYmax;
box.setScaleFactor(scaleFactor);
}
}
draw();
dispatch("change");
}
}
const observer = new ResizeObserver(resize);
function parseInputBoxes() {
for (let i = 0; i < value.boxes.length; i++) {
let box = value.boxes[i];
if (!(box instanceof Box)) {
let color = "";
let label = "";
if (box.hasOwnProperty("color")) {
color = box["color"];
if (Array.isArray(color) && color.length === 3) {
color = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
}
} else {
color = Colors[i % Colors.length];
}
if (box.hasOwnProperty("label")) {
label = box["label"];
}
box = new Box(
draw,
onBoxFinishCreation,
canvasXmin,
canvasYmin,
canvasXmax,
canvasYmax,
label,
box["xmin"],
box["ymin"],
box["xmax"],
box["ymax"],
color,
boxAlpha,
boxMinSize,
handleSize,
boxThickness,
boxSelectedThickness
);
value.boxes[i] = box;
}
}
}
$: {
value;
setImage();
parseInputBoxes();
resize();
draw();
}
function setImage(){
if (imageUrl !== null) {
if (image === null || image.src != imageUrl) {
image = new Image();
image.src = imageUrl;
image.onload = function(){
resize();
draw();
}
}
}
}
onMount(() => {
if (Array.isArray(choices) && choices.length > 0) {
if (!Array.isArray(choicesColors) || choicesColors.length == 0) {
for (let i = 0; i < choices.length; i++) {
let color = Colors[i % Colors.length];
choicesColors.push(colorRGBAToHex(color));
}
}
}
ctx = canvas.getContext("2d");
observer.observe(canvas);
if (selectedBox < 0 && value !== null && value.boxes.length > 0) {
selectBox(0);
}
setImage();
resize();
draw();
});
function handleCanvasFocus() {
document.addEventListener("keydown", handleKeyPress);
}
function handleCanvasBlur() {
document.removeEventListener("keydown", handleKeyPress);
}
onDestroy(() => {
document.removeEventListener("keydown", handleKeyPress);
});
</script>
<div
class="canvas-container"
tabindex="-1"
on:focusin={handleCanvasFocus}
on:focusout={handleCanvasBlur}
>
<canvas
bind:this={canvas}
on:pointerdown={handlePointerDown}
on:pointerup={handlePointerUp}
on:pointermove={handlesCursor ? handlePointerMove : null}
on:dblclick={handleDoubleClick}
class="canvas-annotator"
></canvas>
</div>
{#if interactive}
<span class="canvas-control">
<button
class="icon"
class:selected={mode === Mode.creation}
aria-label="Create box"
on:click={() => setCreateMode()}><BoundingBox/></button
>
<button
class="icon"
class:selected={mode === Mode.drag}
aria-label="Edit boxes"
on:click={() => setDragMode()}><Hand/></button
>
{#if showRemoveButton}
<button
class="icon"
aria-label="Remove boxes"
on:click={() => onDeleteBox()}><Trash/></button
>
{/if}
</span>
{/if}
{#if editModalVisible}
<ModalBox
on:change={onModalEditChange}
on:enter{onModalEditChange}
choices={choices}
choicesColors={choicesColors}
label={selectedBox >= 0 && selectedBox < value.boxes.length ? value.boxes[selectedBox].label : ""}
color={selectedBox >= 0 && selectedBox < value.boxes.length ? colorRGBAToHex(value.boxes[selectedBox].color) : ""}
/>
{/if}
{#if newModalVisible}
<ModalBox
on:change={onModalNewChange}
on:enter{onModalNewChange}
choices={choices}
showRemove={false}
choicesColors={choicesColors}
label={selectedBox >= 0 && selectedBox < value.boxes.length ? value.boxes[selectedBox].label : ""}
color={selectedBox >= 0 && selectedBox < value.boxes.length ? colorRGBAToHex(value.boxes[selectedBox].color) : ""}
/>
{/if}
<style>
.canvas-annotator {
border-color: var(--block-border-color);
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
.canvas-control {
display: flex;
align-items: center;
justify-content: center;
border-top: 1px solid var(--border-color-primary);
width: 95%;
bottom: 0;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
margin-top: var(--size-2);
}
.icon {
width: 22px;
height: 22px;
margin: var(--spacing-lg) var(--spacing-xs);
padding: var(--spacing-xs);
color: var(--neutral-400);
border-radius: var(--radius-md);
}
.icon:hover,
.icon:focus {
color: var(--color-accent);
}
.selected {
color: var(--color-accent);
}
.canvas-container:focus {
outline: none;
}
</style>
|