-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Dev
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,86 @@ | ||
|
||
//Returns angle between two vectors, unit can be "deg" or radians | ||
function v2a(vector1, vector2, unit) { | ||
vector1_length = Math.sqrt(vector1[0] ** 2 + vector1[1] ** 2); | ||
vector2_length = Math.sqrt(vector2[0] ** 2 + vector2[1] ** 2); | ||
vector_product = vector1[0] * vector2[0] + vector1[1] * vector2[1]; | ||
|
||
//angle as degrees | ||
if (unit == "deg") { | ||
var angle = | ||
(Math.acos(vector_product / (vector1_length * vector2_length)) * 180) / | ||
Math.PI; | ||
return angle; | ||
} | ||
|
||
//angle as radians | ||
else { | ||
var angle = Math.acos(vector_product / (vector1_length * vector2_length)); | ||
return angle; | ||
} | ||
} | ||
vector1_length = Math.sqrt(vector1[0] ** 2 + vector1[1] ** 2); | ||
vector2_length = Math.sqrt(vector2[0] ** 2 + vector2[1] ** 2); | ||
vector_product = vector1[0] * vector2[0] + vector1[1] * vector2[1]; | ||
|
||
//angle as degrees | ||
if (unit == "deg") { | ||
var angle = (Math.acos(vector_product / (vector1_length * vector2_length)) * 180) / Math.PI; | ||
return angle; | ||
} | ||
|
||
//angle as radians | ||
else { | ||
var angle = Math.acos(vector_product / (vector1_length * vector2_length)); | ||
return angle; | ||
} | ||
} | ||
|
||
function largest_drawable_square(cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y) { | ||
|
||
function largest_drawable_square(cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y){ | ||
let height = 0; //Positive values down, negative values up. | ||
let width = 0; //positive values to the right, negative to the left. | ||
let width = 0; //positive values to the right, negative to the left. | ||
|
||
//checks which quadrant mouse is in relation to the initially clicked point (think unit circle quadrants or whatever) | ||
let quadrant = 'top_right' | ||
let quadrant = "top_right"; | ||
|
||
if (cursor_end_y - cursor_start_y > 0) { | ||
quadrant = "bottom_right" | ||
quadrant = "bottom_right"; | ||
} | ||
|
||
if (cursor_end_x - cursor_start_x < 0) { | ||
quadrant = "bottom_left" | ||
if (cursor_end_y - cursor_start_y < 0){ | ||
quadrant = "top_left" | ||
quadrant = "bottom_left"; | ||
if (cursor_end_y - cursor_start_y < 0) { | ||
quadrant = "top_left"; | ||
} | ||
} | ||
|
||
|
||
const square_width = cursor_end_x - cursor_start_x | ||
const square_height = cursor_end_y - cursor_start_y | ||
//finds the cursor x or y position which would give the greatest width or height (both?) | ||
//current mouse position is in top right or bottom left quadrant (quadrant 'top_right' and "bottom_left") | ||
if (quadrant == 'top_right' || quadrant == "bottom_left") { | ||
if (Math.abs(cursor_end_x - cursor_start_x) > Math.abs(cursor_end_y - cursor_start_y)) { | ||
width = cursor_end_x - cursor_start_x; | ||
height = -width | ||
//current mouse position is in top right or bottom left quadrant (quadrant top_right or bottom_left) | ||
if (quadrant == "top_right" || quadrant == "bottom_left") { | ||
if (Math.abs(square_width) > Math.abs(square_height)) { | ||
width = square_width; | ||
height = -width; | ||
} | ||
|
||
else { | ||
height = (cursor_end_y - cursor_start_y) | ||
width = -height | ||
height = square_height; | ||
width = -height; | ||
} | ||
} | ||
//quadrant "top_left" and bottom_right | ||
//quadrant top_left or bottom_right | ||
else if (quadrant == "top_left" || quadrant == "bottom_right") { | ||
|
||
if (Math.abs(cursor_end_x - cursor_start_x) > Math.abs(cursor_end_y - cursor_start_y)) { | ||
width = cursor_end_x - cursor_start_x; | ||
height = width | ||
} | ||
if (Math.abs(square_width) > Math.abs(square_height)) { | ||
width = square_width; | ||
height = width; | ||
} | ||
else { | ||
width = cursor_end_y - cursor_start_y; | ||
height = width | ||
width = square_height; | ||
height = width; | ||
} | ||
} | ||
return {cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y, width, height} | ||
return {cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y, width, height, }; | ||
} | ||
|
||
|
||
function get_cursor_position(canvas, event) { | ||
//finds the absolute coordinates clicked, given as distence from top left. | ||
return [event.offsetX, event.offsetY]; | ||
} | ||
|
||
|
||
function draw_square(canvas_info, background_img, cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y) { | ||
function draw_square(canvas_info, background_img, cursor_start_x, cursor_end_x,cursor_start_y, cursor_end_y) { | ||
let canvas = canvas_info; | ||
const ctx = canvas.getContext("2d", { alpha: false }); | ||
let current_square = largest_drawable_square( cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y) | ||
let current_square = largest_drawable_square(cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y); | ||
|
||
let parameter_x = cursor_start_x + ~~current_square.width; | ||
let parameter_y = cursor_start_y + ~~current_square.height; | ||
//Draws the guiding box if it fits the canvas | ||
if ( (parameter_x < canvas.width && parameter_x > 0) && (parameter_y < canvas.height && parameter_y > 0 ) ){ | ||
if ( (parameter_x < canvas.width && parameter_x > 0) && (parameter_y < canvas.height && parameter_y > 0) ) { | ||
ctx.drawImage(background_img, 0, 0, canvas.width, canvas.height); | ||
ctx.beginPath(); | ||
ctx.rect(cursor_start_x, cursor_start_y, current_square.width, current_square.height); | ||
ctx.stroke(); | ||
} | ||
} | ||
|
||
function get_cursor_position(canvas, event) { | ||
//finds the absolute coordinates clicked, given as distence from top left. | ||
return [event.offsetX, event.offsetY]; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.