2023-01-17 18:25:56 +00:00
|
|
|
<template>
|
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
viewBox="0 0 1000 1000"
|
|
|
|
preserveAspectRatio="none"
|
2023-01-17 22:42:21 +00:00
|
|
|
@mousedown.left="draw_start"
|
|
|
|
@mouseup.left="draw_finish"
|
2023-01-17 23:34:42 +00:00
|
|
|
@contextmenu.prevent
|
|
|
|
@mousedown.right="drag_start"
|
|
|
|
@mouseup.right="drag_finish"
|
|
|
|
@mousemove="on_mousemove"
|
|
|
|
@dblclick.left="remove_rect"
|
2023-01-17 18:25:56 +00:00
|
|
|
>
|
|
|
|
<Rect
|
|
|
|
v-for="(rect, index) in rectangles"
|
|
|
|
:key="'rect' + index"
|
|
|
|
:rectangle="rect"
|
|
|
|
/>
|
2023-01-17 23:34:42 +00:00
|
|
|
<Rect
|
|
|
|
v-if="preview_visible"
|
|
|
|
:highlighted="true"
|
|
|
|
:rectangle="preview_rectangle"
|
|
|
|
/>
|
2023-01-17 18:25:56 +00:00
|
|
|
</svg>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { Vue, Options } from "vue-class-component";
|
|
|
|
import { Vector2D, Rectangle } from "./rectangles";
|
|
|
|
import Rect from "./Rect.vue";
|
|
|
|
|
|
|
|
function get_event_thous(event: MouseEvent): Vector2D {
|
|
|
|
if (event.currentTarget === null) {
|
|
|
|
return new Vector2D();
|
|
|
|
}
|
|
|
|
|
|
|
|
let target = event.currentTarget as Element;
|
|
|
|
|
|
|
|
return new Vector2D(
|
|
|
|
Math.round((event.offsetX / target.clientWidth) * 1000),
|
|
|
|
Math.round((event.offsetY / target.clientHeight) * 1000)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Options({
|
|
|
|
components: {
|
|
|
|
Rect,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
export default class RectPad extends Vue {
|
|
|
|
private readonly min_rect_area = 4;
|
2023-01-17 23:34:42 +00:00
|
|
|
private drawing = false;
|
|
|
|
private preview_corner1 = new Vector2D();
|
|
|
|
private preview_corner2 = new Vector2D();
|
|
|
|
private dragging = false;
|
2023-01-17 23:42:07 +00:00
|
|
|
private drag_rect?: Rectangle;
|
2023-01-17 23:34:42 +00:00
|
|
|
private drag_origin = new Vector2D();
|
2023-01-17 18:25:56 +00:00
|
|
|
private rectangles: Rectangle[] = [];
|
|
|
|
|
2023-01-17 23:34:42 +00:00
|
|
|
private get preview_visible(): boolean {
|
|
|
|
return this.drawing || this.dragging;
|
|
|
|
}
|
|
|
|
|
|
|
|
private get preview_rectangle(): Rectangle {
|
|
|
|
return new Rectangle(
|
|
|
|
this.preview_corner1,
|
|
|
|
this.preview_corner2
|
|
|
|
).normalize();
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:42:07 +00:00
|
|
|
private pop_rectangle(point: Vector2D): Rectangle | undefined {
|
|
|
|
const idx = this.rectangles.findIndex(
|
2023-01-17 23:34:42 +00:00
|
|
|
(rect) =>
|
|
|
|
point.x >= rect.left &&
|
|
|
|
point.y >= rect.top &&
|
|
|
|
point.x <= rect.left + rect.width &&
|
|
|
|
point.y <= rect.top + rect.height
|
|
|
|
);
|
2023-01-17 23:42:07 +00:00
|
|
|
|
|
|
|
if (idx === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rects = this.rectangles.splice(idx, 1);
|
|
|
|
return rects[0];
|
2023-01-17 23:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 22:42:21 +00:00
|
|
|
private draw_start(event: MouseEvent) {
|
2023-01-17 23:34:42 +00:00
|
|
|
if (this.preview_visible) {
|
2023-01-17 22:15:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:34:42 +00:00
|
|
|
this.drawing = true;
|
|
|
|
this.preview_corner1 = get_event_thous(event);
|
|
|
|
this.preview_corner2 = get_event_thous(event);
|
2023-01-17 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 23:34:42 +00:00
|
|
|
private draw_finish() {
|
|
|
|
if (!this.drawing) {
|
2023-01-17 22:42:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:34:42 +00:00
|
|
|
this.drawing = false;
|
2023-01-17 18:25:56 +00:00
|
|
|
|
|
|
|
if (this.preview_rectangle.area >= this.min_rect_area) {
|
|
|
|
this.rectangles.push(this.preview_rectangle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:34:42 +00:00
|
|
|
private drag_start(event: MouseEvent) {
|
|
|
|
if (this.preview_visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let point = get_event_thous(event);
|
2023-01-17 23:42:07 +00:00
|
|
|
this.drag_rect = this.pop_rectangle(point);
|
2023-01-17 23:34:42 +00:00
|
|
|
|
2023-01-17 23:42:07 +00:00
|
|
|
if (this.drag_rect === undefined) {
|
2023-01-17 23:34:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dragging = true;
|
|
|
|
this.drag_origin = point;
|
|
|
|
|
2023-01-17 23:42:07 +00:00
|
|
|
this.preview_corner1 = this.drag_rect.origin;
|
|
|
|
this.preview_corner2 = this.drag_rect.corner;
|
2023-01-17 23:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 23:42:07 +00:00
|
|
|
private drag_finish() {
|
2023-01-17 23:34:42 +00:00
|
|
|
if (!this.dragging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dragging = false;
|
2023-01-17 23:42:07 +00:00
|
|
|
this.rectangles.push(this.preview_rectangle);
|
2023-01-17 23:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private on_mousemove(event: MouseEvent) {
|
|
|
|
let point = get_event_thous(event);
|
|
|
|
|
|
|
|
if (this.drawing) {
|
|
|
|
this.preview_corner2 = point;
|
2023-01-17 23:42:07 +00:00
|
|
|
} else if (this.dragging && this.drag_rect) {
|
2023-01-17 23:34:42 +00:00
|
|
|
let movement = point.minus(this.drag_origin);
|
|
|
|
|
2023-01-17 23:42:07 +00:00
|
|
|
this.preview_corner1 = this.drag_rect.origin.plus(movement);
|
|
|
|
this.preview_corner2 = this.drag_rect.corner.plus(movement);
|
2023-01-17 23:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private remove_rect(event: MouseEvent) {
|
2023-01-17 23:42:07 +00:00
|
|
|
this.pop_rectangle(get_event_thous(event));
|
2023-01-17 18:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
svg {
|
|
|
|
cursor: crosshair;
|
|
|
|
}
|
|
|
|
</style>
|