File size: 4,994 Bytes
8fdc036 |
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 |
import {
Graphics,
type IRenderer,
type Container,
Sprite,
RenderTexture
} from "pixi.js";
import { type Command } from "../utils/commands";
import { spring } from "svelte/motion";
import type { Writable } from "svelte/store";
export interface CropCommand extends Command {
start: (
width: number,
height: number,
previous_crop: [number, number, number, number],
preview?: boolean,
set_previous?: boolean
) => void;
stop: () => number;
continue: (
crop_size: [number, number, number, number],
preview?: boolean
) => void;
}
export function crop_canvas(
renderer: IRenderer,
mask_container: Container,
crop: Writable<[number, number, number, number]>,
current_opacity = 0
): CropCommand {
let text: RenderTexture;
let sprite: Sprite;
const mask_graphics = new Graphics();
let previous_crop: [number, number, number, number];
let final_crop: [number, number, number, number];
let width: number;
let height: number;
let alpha_spring = spring(current_opacity, {
stiffness: 0.1,
damping: 0.5
});
let spring_value = current_opacity;
alpha_spring.subscribe((value) => {
if (!final_crop) return;
spring_value = value;
crop_mask(width, height, final_crop, true);
});
function crop_mask(
width: number,
height: number,
crop_size: [number, number, number, number],
preview: boolean
): void {
mask_graphics.clear();
if (preview) {
mask_graphics.beginFill(0xffffff, spring_value);
mask_graphics.drawRect(0, 0, width, height);
mask_graphics.endFill();
}
mask_graphics.beginFill(0xffffff, 1);
mask_graphics.drawRect(...crop_size);
mask_graphics.endFill();
renderer.render(mask_graphics, {
renderTexture: text
});
}
let clean = true;
let stopped = false;
return {
start: (
_width: number,
_height: number,
_previous_crop: [number, number, number, number],
_preview = true,
set_previous = true
) => {
clean = false;
text = RenderTexture.create({
width: _width,
height: _height
});
crop_mask(_width, _height, _previous_crop, _preview);
sprite = new Sprite(text);
mask_container.mask = sprite;
width = _width;
height = _height;
if (set_previous)
previous_crop = JSON.parse(JSON.stringify(_previous_crop));
},
continue: (crop_size: [number, number, number, number], preview = true) => {
final_crop = JSON.parse(JSON.stringify(crop_size));
if (spring_value === 0.2) {
crop_mask(width, height, final_crop, preview);
} else {
alpha_spring.set(0.2);
}
},
undo() {
this.start(width, height, previous_crop, false);
crop.set([
previous_crop[0] / width,
previous_crop[1] / height,
previous_crop[2] / width,
previous_crop[3] / height
]);
clean = true;
},
stop() {
stopped = true;
return spring_value;
},
execute() {
if (clean) {
this.start(width, height, final_crop, false);
crop.set([
final_crop[0] / width,
final_crop[1] / height,
final_crop[2] / width,
final_crop[3] / height
]);
clean = true;
} else {
if (!stopped) {
alpha_spring.set(0);
}
crop.set([
final_crop[0] / width,
final_crop[1] / height,
final_crop[2] / width,
final_crop[3] / height
]);
clean = true;
}
}
};
}
export function resize_and_reposition(
original_width: number,
original_height: number,
anchor: "t" | "r" | "l" | "b" | "tl" | "tr" | "bl" | "br" | "c",
aspect_ratio: number,
max_width: number,
max_height: number
): {
new_width: number;
new_height: number;
x_offset: number;
y_offset: number;
} {
let new_width = original_width;
let new_height = original_height;
// Calculate new dimensions based on aspect ratio
if (anchor.includes("t") || anchor.includes("b") || anchor == "c") {
new_width = original_height * aspect_ratio;
}
if (anchor.includes("l") || anchor.includes("r") || anchor == "c") {
new_height = original_width / aspect_ratio;
}
// Ensure aspect ratio is maintained
new_height = new_height || new_width / aspect_ratio;
new_width = new_width || new_height * aspect_ratio;
// Apply max width/height constraints and adjust dimensions
if (new_width > max_width) {
new_width = max_width;
new_height = new_width / aspect_ratio;
}
if (new_height > max_height) {
new_height = max_height;
new_width = new_height * aspect_ratio;
}
// Calculate offsets to ensure anchor position is maintained
let x_offset = 0;
let y_offset = 0;
if (anchor.includes("r")) {
x_offset = original_width - new_width;
} else if (anchor.includes("l")) {
x_offset = 0;
} else {
// Center or top/bottom center
x_offset = (original_width - new_width) / 2;
}
if (anchor.includes("b")) {
y_offset = original_height - new_height;
} else if (anchor.includes("t")) {
y_offset = 0;
} else {
// Center or left/right center
y_offset = (original_height - new_height) / 2;
}
return {
new_width: new_width,
new_height: new_height,
x_offset: x_offset,
y_offset: y_offset
};
}
|