From 2cf434f7a9cee42ca7e544263742fe447ad80b6e Mon Sep 17 00:00:00 2001 From: OSTERITE Date: Mon, 23 Jan 2023 21:57:16 +0100 Subject: [PATCH 1/4] replaced "~~" with Math.floor() --- color_function/library/classdefinitions.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/color_function/library/classdefinitions.js b/color_function/library/classdefinitions.js index ba501fd..cffb0ca 100644 --- a/color_function/library/classdefinitions.js +++ b/color_function/library/classdefinitions.js @@ -2,10 +2,6 @@ //5 It might be more efficient to use a single method to handle the hue, saturation, and lightness calculations, instead of having separate methods for each in the Square class. -//6 The class Square_matrix is using the Math.abs function to keep the saturation and lightness values between 0 and 100. An alternative approach would be to use the modulo operator % to ensure that the value is within the desired range, which would be more efficient. - -//7 The class Square_matrix is using the replace method to replace X and Y in the expressions, it could use a more powerful and efficient way like using a template string to replace the X and Y values. - class Square { constructor(ctx, xpos, ypos, hue, saturation, lightness, square_size) { From 46aeb1a06cda4349632855bfe19ab6c661672e0c Mon Sep 17 00:00:00 2001 From: OSTERITE Date: Mon, 23 Jan 2023 21:57:21 +0100 Subject: [PATCH 2/4] replaced "~~" with Math.floor() --- color_function/library/classdefinitions.js | 5 ---- color_function/script.js | 28 ++++++++++------------ 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/color_function/library/classdefinitions.js b/color_function/library/classdefinitions.js index cffb0ca..6f6453e 100644 --- a/color_function/library/classdefinitions.js +++ b/color_function/library/classdefinitions.js @@ -1,8 +1,3 @@ -//IMPROVEMENT AREAS - -//5 It might be more efficient to use a single method to handle the hue, saturation, and lightness calculations, instead of having separate methods for each in the Square class. - - class Square { constructor(ctx, xpos, ypos, hue, saturation, lightness, square_size) { this.xpos = xpos; diff --git a/color_function/script.js b/color_function/script.js index e1587ba..e74e6f1 100644 --- a/color_function/script.js +++ b/color_function/script.js @@ -2,13 +2,13 @@ const c = console.log.bind(console); -const get_hue_expression = document.getElementById("hue_expression"); -const get_saturation_expression = document.getElementById("saturation_expression"); -const get_lightness_expression = document.getElementById("lightness_expression"); -const get_size_lower = document.getElementById("size_lower"); -const get_size_upper = document.getElementById("size_upper"); -const get_pixel_ratio = document.getElementById("pixel_ratio"); -const get_upscale_button = document.getElementById("upscale"); +const get_hue_expression = document.querySelector("#hue_expression"); +const get_saturation_expression = document.querySelector("#saturation_expression"); +const get_lightness_expression = document.querySelector("#lightness_expression"); +const get_size_lower = document.querySelector("#size_lower"); +const get_size_upper = document.querySelector("#size_upper"); +const get_pixel_ratio = document.querySelector("#pixel_ratio"); +const get_upscale_button = document.querySelector("#upscale"); get_hue_expression.addEventListener("change", function () { hue_expression = get_hue_expression.value; @@ -33,8 +33,8 @@ get_lightness_expression.addEventListener("change", function () { get_pixel_ratio.addEventListener("change", function () { pixel_ratio = +get_pixel_ratio.value; - size_upper = ~~(+get_size_upper.value / pixel_ratio); - size_lower = ~~(+get_size_lower.value / pixel_ratio); + size_upper = Math.floor(+get_size_upper.value / pixel_ratio); + size_lower = Math.floor(+get_size_lower.value / pixel_ratio); distance_left_x_zooming = size_lower distance_top_y_zooming = size_lower matrix_squares.create_squares(size_lower, size_lower, size_upper, size_upper, pixel_ratio) @@ -43,9 +43,7 @@ get_pixel_ratio.addEventListener("change", function () { get_size_lower.addEventListener("change", function () { let old_size = size_lower - size_lower = ~~(+get_size_lower.value / pixel_ratio); - - + size_lower = Math.floor(+get_size_lower.value / pixel_ratio); const absolute_width = canvas.width / (size_upper - size_lower + index_zero); var distance_from_top_left = (absolute_width) * (old_size - size_lower) var image_size = ((absolute_width) * (size_upper - old_size + index_zero )) @@ -59,14 +57,12 @@ get_size_lower.addEventListener("change", function () { get_size_upper.addEventListener("change", function () { let old_size = size_upper - size_upper = ~~(+get_size_upper.value / pixel_ratio); + size_upper = Math.floor(+get_size_upper.value / pixel_ratio); const absolute_width = canvas.width / (size_upper - size_lower + index_zero); var image_size = ((absolute_width) * (old_size - size_lower + index_zero)) ctx.drawImage(resizing_img, 0, 0, image_size, image_size); - - matrix_squares.change_size(size_lower, size_upper, old_size, 'higher'); update_images(canvas) }); @@ -78,7 +74,7 @@ get_upscale_button.addEventListener("click", function () { //-----------------------Canvas----------------------------- -let canvas = document.getElementById("canvas"); +let canvas = document.querySelector("#canvas"); const ctx = canvas.getContext("2d", { alpha: false }); //Check class for these settings! From ff3962e06270047474eb62a15981307eb6a5bbe2 Mon Sep 17 00:00:00 2001 From: OSTERITE Date: Tue, 24 Jan 2023 13:02:40 +0100 Subject: [PATCH 3/4] reverted to using "Function" instead of math.eval --- color_function/index.html | 2 +- color_function/library/classdefinitions.js | 20 +++++----- color_function/library/functions.js | 14 ++++--- color_function/script.js | 45 ++++++++++------------ 4 files changed, 40 insertions(+), 41 deletions(-) diff --git a/color_function/index.html b/color_function/index.html index a76f35b..dc58d64 100644 --- a/color_function/index.html +++ b/color_function/index.html @@ -33,8 +33,8 @@ - + diff --git a/color_function/library/classdefinitions.js b/color_function/library/classdefinitions.js index 6f6453e..f0e718c 100644 --- a/color_function/library/classdefinitions.js +++ b/color_function/library/classdefinitions.js @@ -27,17 +27,15 @@ class Square { } hue_changed(expression, x, y) { - - this.hue = math.evaluate(`${expression}`.replace(/X/g, x).replace(/Y/g, y)); + this.hue = Function( `return ${expression.replace(/X/g, x).replace(/Y/g, y)}` )(); this.draw(); } saturation_changed(expression, x, y) { - this.saturation = Math.abs( ((100 + math.evaluate(`${expression}`.replace(/X/g, x).replace(/Y/g, y))) % 200) - 100 ); - + this.saturation = Math.abs( ((100 + Function(`return ${expression.replace(/X/g, x).replace(/Y/g, y)}`)()) % 200) - 100 ); this.draw(); } lightness_changed(expression, x, y) { - this.lightness = Math.abs( ((100 + math.evaluate(`${expression}`.replace(/X/g, x).replace(/Y/g, y))) % 200) - 100 ); + this.lightness = Math.abs( ((100 + Function(`return ${expression.replace(/X/g, x).replace(/Y/g, y)}`)()) % 200) - 100 ); this.draw(); } } @@ -82,9 +80,9 @@ class Square_matrix { let color_x = x * this.pixel_ratio; let color_y = y * this.pixel_ratio; - const hue = math.evaluate(`${this.hue_expression}`.replace(/X/g, color_x).replace(/Y/g, color_y)); - const saturation = Math.abs( ((100 + math.evaluate(`${this.saturation_expression}`.replace(/X/g, color_x).replace(/Y/g, color_y))) % 200) - 100 ); - const lightness = Math.abs( ((100 + math.evaluate(`${this.lightness_expression}`.replace(/X/g, color_x).replace(/Y/g, color_y))) % 200) - 100 ); + const hue = Function( `return ${this.hue_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )(); + const saturation = Math.abs( ((100 + Function( `return + ${this.saturation_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )()) % 200) - 100 ); + const lightness = Math.abs( ((100 + Function( `return + ${this.lightness_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )()) % 200) - 100 ); this.square_matrix[x][y] = new Square( this.ctx, @@ -109,9 +107,9 @@ class Square_matrix { let color_x = x * this.pixel_ratio; let color_y = y * this.pixel_ratio; - const hue = math.evaluate(`${this.hue_expression}`.replace(/X/g, color_x).replace(/Y/g, color_y)); - const saturation = Math.abs( ((100 + math.evaluate(`${this.saturation_expression}`.replace(/X/g, color_x).replace(/Y/g, color_y))) % 200) - 100 ); - const lightness = Math.abs( ((100 + math.evaluate(`${this.lightness_expression}`.replace(/X/g, color_x).replace(/Y/g, color_y))) % 200) - 100 ); + const hue = Function( `return ${this.hue_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )(); + const saturation = Math.abs( ((100 + Function( `return + ${this.saturation_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )()) % 200) - 100 ); + const lightness = Math.abs( ((100 + Function( `return + ${this.lightness_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )()) % 200) - 100 ); this.square_matrix[x][y] = new Square( this.ctx, diff --git a/color_function/library/functions.js b/color_function/library/functions.js index e176419..200735d 100644 --- a/color_function/library/functions.js +++ b/color_function/library/functions.js @@ -1,18 +1,22 @@ //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]; + const vector1_length = Math.sqrt(vector1[0] ** 2 + vector1[1] ** 2); + const vector2_length = Math.sqrt(vector2[0] ** 2 + vector2[1] ** 2); + + if (vector1_length == 0 || vector2_length == 0){ + return NaN + } + const 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; + const 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)); + const angle = Math.acos(vector_product / (vector1_length * vector2_length)); return angle; } } diff --git a/color_function/script.js b/color_function/script.js index e74e6f1..3187b2b 100644 --- a/color_function/script.js +++ b/color_function/script.js @@ -11,23 +11,23 @@ const get_pixel_ratio = document.querySelector("#pixel_ratio"); const get_upscale_button = document.querySelector("#upscale"); get_hue_expression.addEventListener("change", function () { - hue_expression = get_hue_expression.value; + // hue_expression = get_hue_expression.value; ctx.clearRect(0, 0, canvas.width, canvas.height) - matrix_squares.class_method_loop("hue", hue_expression); + matrix_squares.class_method_loop("hue", get_hue_expression.value); update_images(canvas) }); get_saturation_expression.addEventListener("change", function () { - saturation_expression = get_saturation_expression.value; + // saturation_expression = get_saturation_expression.value; ctx.clearRect(0, 0, canvas.width, canvas.height) - matrix_squares.class_method_loop("saturation", saturation_expression); + matrix_squares.class_method_loop("saturation", get_saturation_expression.value); update_images(canvas) }); get_lightness_expression.addEventListener("change", function () { - lightness_expression = get_lightness_expression.value; + // lightness_expression = get_lightness_expression.value; ctx.clearRect(0, 0, canvas.width, canvas.height) - matrix_squares.class_method_loop("lightness", lightness_expression); + matrix_squares.class_method_loop("lightness", get_lightness_expression.value); update_images(canvas) }); @@ -37,6 +37,7 @@ get_pixel_ratio.addEventListener("change", function () { size_lower = Math.floor(+get_size_lower.value / pixel_ratio); distance_left_x_zooming = size_lower distance_top_y_zooming = size_lower + matrix_squares.create_squares(size_lower, size_lower, size_upper, size_upper, pixel_ratio) update_images(canvas) }); @@ -48,9 +49,7 @@ get_size_lower.addEventListener("change", function () { var distance_from_top_left = (absolute_width) * (old_size - size_lower) var image_size = ((absolute_width) * (size_upper - old_size + index_zero )) ctx.drawImage(resizing_img, distance_from_top_left, distance_from_top_left, image_size, image_size); - - - + matrix_squares.change_size(size_lower, size_upper, old_size, 'lower'); update_images(canvas) }); @@ -83,16 +82,15 @@ const ctx = canvas.getContext("2d", { alpha: false }); //----------------Creation of pixels-------------------------------- -const index_zero = 1 let matrix_pixels = []; let size_lower = +get_size_lower.value; let size_upper = +get_size_upper.value; -let size = size_upper - size_lower + index_zero; -var pixel_ratio = parseFloat(get_pixel_ratio.value); +var pixel_ratio= +(get_pixel_ratio.value); +const index_zero = 1 -let hue_expression = get_hue_expression.value; -let saturation_expression = get_saturation_expression.value; -let lightness_expression = get_lightness_expression.value; +// let hue_expression = get_hue_expression.value; +// let saturation_expression = get_saturation_expression.value; +// let lightness_expression = get_lightness_expression.value; //-------------------------------ZOOMING-------------------- @@ -100,7 +98,6 @@ let initial_cursor_position = [] let current_cursor_position = [] let mouse_is_down = false - // Event listener for all cursor on canvas events function handle_canvas_event_zoom(event) { switch (event.type) { @@ -166,9 +163,10 @@ var distance_top_y_zooming = size_lower window.onload = winInit; function winInit() { // ctx.filter = "hue-rotate(200deg)" //INTERESTING! - matrix_squares = new Square_matrix(canvas, hue_expression, saturation_expression, lightness_expression) + matrix_squares = new Square_matrix(canvas, get_hue_expression.value, get_saturation_expression.value, get_lightness_expression.value) matrix_squares.create_squares(size_lower, size_lower, size_upper, size_upper, pixel_ratio) update_images(canvas) + } //\\\\\\\\\\\\\\\\\\\\FUNCTIONS\\\\\\\\\\\\\\\\\\\\\\\ @@ -197,20 +195,19 @@ function update_images(canvas){ // } // runspeed = get_runspeed.value; // animId = setInterval(draw_pixels, 1000 / runspeed); -//TODO: the draw_pixels function must be made again if i want make a changing variable that changes every second or whatever // } // } //------------------END-------------------- //!HUGE? TODO: use webworkers, ask chat gpt-3 for help, not viable? passing information between webworker and main script removes class -//TODO: Research complex plotting or whatever, make an option to change to using complex numbers? +//TODO: !Research complex plotting or whatever, make an option to change to using complex numbers? -//TODO: Create a settings button where settings can be changed/toggled? -//TODO:! Add a button for the option to redraw the black background, creates very interesting patterns when the size of the pixels are < 1 -//TODO: Make an option to turn on the sawtooth pattern for hue too? and create lower and upper limit, this.hue = Math.abs(( (100 + Function("return " + hue_expression)()) % 200) - 100) +//TODO: create a settings button where settings can be changed/toggled? +//TODO:! add a button for the option to redraw the black background, creates very interesting patterns when the size of the pixels are < 1 +//TODO: make an option to turn on the sawtooth pattern for hue too? and create lower and upper limit, this.hue = Math.abs(( (100 + Function("return " + hue_expression)()) % 200) - 100) //!TODO: Create a option to toggle between clicking a button to run script and running script when a variable is changed. -//!TODO: Performance mode and fast mode, ise ctx.drawimage method for fast and redraw every pixel every time for fast mode. +//!TODO: quality mode and fast mode, ise ctx.drawimage method for fast and redraw every pixel every time for fast mode. //TODO: save settings in localstorage //TODO: Create option to make a variable that changes every second f.eks. goes from 1 to 10 then 10 to 1, call it n and then n can be used in the color chooser @@ -235,4 +232,4 @@ function update_images(canvas){ //* WONTFIX Minor fix in the new_pixels function, it creates the corner piece twice //* make it possible to zoom in on inzoomed image. //* made zooming more general -//* made functions into methods +//* made functions into methods \ No newline at end of file From 006265ce67ed909da26ed8d829732d6860548b3b Mon Sep 17 00:00:00 2001 From: OSTERITE Date: Tue, 24 Jan 2023 13:34:47 +0100 Subject: [PATCH 4/4] ready for release --- color_function/library/classdefinitions.js | 19 ++++++------- color_function/library/functions.js | 33 +++++++++++----------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/color_function/library/classdefinitions.js b/color_function/library/classdefinitions.js index f0e718c..f0e2bda 100644 --- a/color_function/library/classdefinitions.js +++ b/color_function/library/classdefinitions.js @@ -7,7 +7,6 @@ class Square { this.lightness = lightness; this.square_size = square_size; this.ctx = ctx; - // this.draw(); } draw() { @@ -77,8 +76,8 @@ class Square_matrix { } for (let y = start_y, runs = length; y <= runs; y++) { - let color_x = x * this.pixel_ratio; - let color_y = y * this.pixel_ratio; + const color_x = x * this.pixel_ratio; + const color_y = y * this.pixel_ratio; const hue = Function( `return ${this.hue_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )(); const saturation = Math.abs( ((100 + Function( `return + ${this.saturation_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )()) % 200) - 100 ); @@ -104,8 +103,8 @@ class Square_matrix { // row for (let x = start_y, runs = length; x <= runs; x++) { for (let y = start_x, runs = width; y <= runs; y++) { - let color_x = x * this.pixel_ratio; - let color_y = y * this.pixel_ratio; + const color_x = x * this.pixel_ratio; + const color_y = y * this.pixel_ratio; const hue = Function( `return ${this.hue_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )(); const saturation = Math.abs( ((100 + Function( `return + ${this.saturation_expression .replace(/X/g, color_x) .replace(/Y/g, color_y)}` )()) % 200) - 100 ); @@ -149,7 +148,7 @@ class Square_matrix { } else if (method === "lightness") { - this.lightness = component; + this.lightness_expression = component; var class_method = Square.prototype.lightness_changed; } @@ -168,7 +167,7 @@ class Square_matrix { this.size_lower = size_lower; this.size_upper = size_upper; - //old_size_bipartite means its either the old size_lower or old size_upper + // old_size_bipartite means its either the old size_lower or old size_upper switch (true) { //size_upper changed @@ -190,16 +189,14 @@ class Square_matrix { } zoom(cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y) { + const zoom_area = largest_drawable_square( cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y ); const canvas_current_x = cursor_start_x + zoom_area.width; const canvas_current_y = cursor_start_y + zoom_area.height; - const canvas_start_x = 0; - const canvas_start_y = 0; - //zooms if a perfect square is makeable, guiding box fits the canvas - if ( canvas_start_x < canvas_current_x && canvas_current_x < this.canvas.width && canvas_start_y < canvas_current_y && canvas_current_y < this.canvas.height ) { + if ( 0 < canvas_current_x && canvas_current_x < this.canvas.width && 0 < canvas_current_y && canvas_current_y < this.canvas.height ) { //start is the smallest value, while end is the largest, opposite for y value because it is distance from top of canvas const start_x = Math.min( ~~(cursor_start_x / this.absolute_width) + this.distance_left_x, ~~((cursor_start_x + zoom_area.width) / this.absolute_width) + this.distance_left_x ); diff --git a/color_function/library/functions.js b/color_function/library/functions.js index 200735d..f220197 100644 --- a/color_function/library/functions.js +++ b/color_function/library/functions.js @@ -1,11 +1,10 @@ //Returns angle between two vectors, unit can be "deg" or radians function v2a(vector1, vector2, unit) { - const vector1_length = Math.sqrt(vector1[0] ** 2 + vector1[1] ** 2); const vector2_length = Math.sqrt(vector2[0] ** 2 + vector2[1] ** 2); - - if (vector1_length == 0 || vector2_length == 0){ - return NaN + + if (vector1_length == 0 || vector2_length == 0) { + return NaN; } const vector_product = vector1[0] * vector2[0] + vector1[1] * vector2[1]; //angle as degrees @@ -22,9 +21,8 @@ function v2a(vector1, vector2, unit) { } 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"; @@ -40,8 +38,8 @@ function largest_drawable_square(cursor_start_x, cursor_end_x, cursor_start_y, c } } - const square_width = cursor_end_x - cursor_start_x - const square_height = cursor_end_y - cursor_start_y + 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 or bottom_left) if (quadrant == "top_right" || quadrant == "bottom_left") { @@ -65,21 +63,22 @@ function largest_drawable_square(cursor_start_x, cursor_end_x, cursor_start_y, c 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 draw_square(canvas_info, background_img, cursor_start_x, cursor_end_x,cursor_start_y, cursor_end_y) { - let canvas = canvas_info; +function draw_square(canvas_info, background_img, cursor_start_x, cursor_end_x, cursor_start_y, cursor_end_y) { + const 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); + const 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; + const parameter_x = cursor_start_x + ~~current_square.width; + const 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 ( (0 < parameter_x && parameter_x < canvas.width) && (0 < parameter_y && parameter_y < canvas.height ) ) { 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.rect( cursor_start_x, cursor_start_y, current_square.width, current_square.height ); ctx.stroke(); } } @@ -87,4 +86,4 @@ function draw_square(canvas_info, background_img, cursor_start_x, cursor_end_x,c function get_cursor_position(canvas, event) { //finds the absolute coordinates clicked, given as distence from top left. return [event.offsetX, event.offsetY]; -} \ No newline at end of file +}