File size: 7,585 Bytes
0bd62e5 |
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 |
<script lang="ts">
import { createEventDispatcher } from "svelte";
import Handle from "./Handle.svelte";
import { type EditorContext } from "../ImageEditor.svelte";
import { clamp } from "../utils/pixi";
import { resize_and_reposition } from "./crop";
export let editor_box: EditorContext["editor_box"];
export let crop_constraint: number | null;
const dispatch = createEventDispatcher<{
crop_start: {
x: number;
y: number;
width: number;
height: number;
};
crop_continue: {
x: number;
y: number;
width: number;
height: number;
};
crop_end: {
x: number;
y: number;
width: number;
height: number;
};
}>();
let _height = $editor_box.child_height;
let _width = $editor_box.child_width;
let _top = $editor_box.child_top - $editor_box.parent_top;
let _left = $editor_box.child_left - $editor_box.parent_left;
let _right = $editor_box.child_right - $editor_box.parent_left;
let _bottom = 0;
let dragging = false;
export let w_p: number;
export let h_p: number;
export let l_p: number;
export let t_p: number;
const positions = ["tl", "br", "tr", "bl", "t", "b", "l", "r"] as const;
let finished: boolean;
let timer: NodeJS.Timeout;
let triggered = false;
$: {
if (dragging || position_drag) {
clearTimeout(timer);
finished = false;
} else {
set_finished_with_timeout_and_delay();
}
}
function set_finished_with_timeout_and_delay(): void {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
if (triggered) {
dispatch("crop_end", {
x: l_p,
y: t_p,
width: w_p,
height: h_p
});
triggered = false;
}
finished = true;
position_drag = false;
}, 1000);
}
function handle_change(
{
top,
bottom,
left,
right
}: {
top: number | undefined;
bottom: number | undefined;
left: number | undefined;
right: number | undefined;
},
position: (typeof positions)[number]
): void {
_top = clamp(
top ? top - $editor_box.parent_top : _top,
$editor_box.child_top - $editor_box.parent_top,
$editor_box.child_bottom - $editor_box.parent_top
);
_left = clamp(
left ? left - $editor_box.parent_left : _left,
$editor_box.child_left - $editor_box.parent_left,
$editor_box.child_right - $editor_box.parent_left
);
_right = clamp(
right ? right - $editor_box.parent_left : _right,
$editor_box.child_left - $editor_box.parent_left,
$editor_box.child_right - $editor_box.parent_left
);
_bottom = clamp(
bottom ? bottom - $editor_box.parent_top : _bottom,
$editor_box.child_top - $editor_box.parent_top,
$editor_box.child_bottom - $editor_box.parent_top
);
_width = clamp(
right ? right - _left - $editor_box.parent_left : _width,
0,
_right - _left
);
_width = clamp(
left ? _right - left + $editor_box.parent_left : _width,
0,
_right - _left
);
_height = clamp(
bottom ? bottom - _top - $editor_box.parent_top : _height,
0,
_bottom - _top
);
_height = clamp(
top ? _bottom - top + $editor_box.parent_top : _height,
0,
_bottom - _top
);
const anchors_for_position = {
tl: "br",
tr: "bl",
bl: "tr",
br: "tl",
t: "b",
b: "t",
l: "r",
r: "l",
c: "c"
} as const;
if (crop_constraint) {
const max_w = ["t", "b"].includes(position)
? $editor_box.child_width
: _right - _left;
const max_h = ["l", "r"].includes(position)
? $editor_box.child_height
: _bottom - _top;
let result = resize_and_reposition(
_width,
_height,
anchors_for_position[position],
crop_constraint,
max_w,
max_h
);
_width = result.new_width;
_height = result.new_height;
_left = _left + result.x_offset;
_top = _top + result.y_offset;
_right = _left + _width;
_bottom = _top + _height;
}
w_p = _width / $editor_box.child_width;
h_p = _height / $editor_box.child_height;
l_p =
(_left - $editor_box.child_left + $editor_box.parent_left) /
$editor_box.child_width;
t_p =
(_top - $editor_box.child_top + $editor_box.parent_top) /
$editor_box.child_height;
dispatch(triggered ? "crop_continue" : "crop_start", {
x: l_p,
y: t_p,
width: w_p,
height: h_p
});
triggered = true;
}
function resize(): void {
_width = w_p * $editor_box.child_width;
_height = h_p * $editor_box.child_height;
_left =
l_p * $editor_box.child_width +
($editor_box.child_left - $editor_box.parent_left);
_top =
t_p * $editor_box.child_height +
($editor_box.child_top - $editor_box.parent_top);
_right = _left + _width;
_bottom = _top + _height;
}
$: $editor_box && resize();
let start_x = 0;
let start_y = 0;
let position_drag = false;
function handle_drag_start(e: MouseEvent): void {
position_drag = true;
start_x = e.clientX;
start_y = e.clientY;
if (!finished) return;
finished = false;
dispatch("crop_start", {
x: l_p,
y: t_p,
width: w_p,
height: h_p
});
}
function handle_drag_end(e: MouseEvent): void {
if (!position_drag) return;
position_drag = false;
}
function handle_dragging(e: MouseEvent): void {
if (!position_drag) return;
const x_delta = e.clientX - start_x;
const y_delta = e.clientY - start_y;
_left = clamp(
_left + x_delta,
$editor_box.child_left - $editor_box.parent_left,
$editor_box.child_right - $editor_box.parent_left - _width
);
_top = clamp(
_top + y_delta,
$editor_box.child_top - $editor_box.parent_top,
$editor_box.child_bottom - $editor_box.parent_top - _height
);
_right = _left + _width;
_bottom = _top + _height;
l_p =
(_left - $editor_box.child_left + $editor_box.parent_left) /
$editor_box.child_width;
t_p =
(_top - $editor_box.child_top + $editor_box.parent_top) /
$editor_box.child_height;
start_x = e.clientX;
start_y = e.clientY;
dispatch("crop_continue", {
x: l_p,
y: t_p,
width: w_p,
height: h_p
});
triggered = true;
}
</script>
<svelte:window on:mousemove={handle_dragging} on:mouseup={handle_drag_end} />
<div class="wrap">
<div
class="box"
style:width="{_width}px"
style:height="{_height}px"
style:top="{_top}px"
style:left="{_left}px"
>
{#each positions as position}
<Handle
on:change={({ detail }) => handle_change(detail, position)}
bind:dragging
location={position}
/>
{/each}
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div class="grid" class:finished on:mousedown={handle_drag_start}>
{#each { length: 25 } as _}
<div></div>
{/each}
</div>
</div>
</div>
<style>
.grid {
height: 100%;
width: 100%;
display: grid;
grid-template-rows: 1fr 1px 1fr 1px 1fr;
grid-template-columns: 1fr 1px 1fr 1px 1fr;
overflow: hidden;
transition: 0.2s;
opacity: 1;
/* pointer-events: none; */
}
.grid.finished {
opacity: 0;
}
.grid > div {
width: 100%;
height: 100%;
}
.grid > div:nth-of-type(even) {
background: black;
opacity: 0.5;
}
.wrap {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
/* transform: translateY(-20px); */
}
.box {
position: absolute;
width: 100%;
height: 100%;
border: 1px solid black;
z-index: var(--layer-top);
}
</style>
|