MCPcopy
hub / github.com/1j01/jspaint / selectBox

Function selectBox

src/tools.js:291–372  ·  view source on GitHub ↗
(rect_x, rect_y, rect_width, rect_height)

Source from the content-addressed store, hash-verified

289 description: localize("Selects a rectangular part of the picture to move, copy, or edit."),
290 cursor: ["precise", [16, 16], "crosshair"],
291 selectBox(rect_x, rect_y, rect_width, rect_height) {
292 if (rect_width > 1 && rect_height > 1) {
293 var free_form_selection = selection;
294 if (selection) {
295 // for silly multitools feature
296 meld_selection_into_canvas();
297 }
298 if (ctrl) {
299 undoable({ name: "Crop" }, () => {
300 var cropped_canvas = make_canvas(rect_width, rect_height);
301 cropped_canvas.ctx.drawImage(main_canvas, -rect_x, -rect_y);
302 main_ctx.copy(cropped_canvas);
303 canvas_handles.show();
304 $canvas_area.trigger("resize"); // does this not also call canvas_handles.show()?
305 });
306 } else if (free_form_selection) {
307 // for silly multitools feature,
308 // create a selection that's the Free-Form selection XOR the normal selection
309
310 var x_min = Math.min(free_form_selection.x, rect_x);
311 var y_min = Math.min(free_form_selection.y, rect_y);
312 var x_max = Math.max(free_form_selection.x + free_form_selection.width, rect_x + rect_width);
313 var y_max = Math.max(free_form_selection.y + free_form_selection.height, rect_y + rect_height);
314
315 var contents_canvas = make_canvas(
316 x_max - x_min,
317 y_max - y_min,
318 );
319 var rect_canvas = make_canvas(
320 x_max - x_min,
321 y_max - y_min,
322 );
323 rect_canvas.ctx.drawImage(
324 main_canvas,
325 // source:
326 rect_x,
327 rect_y,
328 rect_width,
329 rect_height,
330 // dest:
331 rect_x - x_min,
332 rect_y - y_min,
333 rect_width,
334 rect_height,
335 );
336
337 contents_canvas.ctx.drawImage(
338 free_form_selection.canvas,
339 free_form_selection.x - x_min,
340 free_form_selection.y - y_min,
341 );
342 contents_canvas.ctx.globalCompositeOperation = "xor";
343 contents_canvas.ctx.drawImage(rect_canvas, 0, 0);
344
345 undoable({
346 name: `${localize("Free-Form Select")}⊕${localize("Select")}`,
347 icon: get_icon_for_tools([
348 get_tool_by_id(TOOL_FREE_FORM_SELECT),

Callers

nothing calls this directly

Calls 12

undoableFunction · 0.90
make_canvasFunction · 0.90
get_icon_for_toolsFunction · 0.90
get_tool_by_idFunction · 0.90
get_icon_for_toolFunction · 0.90
localizeFunction · 0.85
copyMethod · 0.80
cut_out_backgroundMethod · 0.80
showMethod · 0.65
minMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected