Skip to content

Commit

Permalink
exported normalize funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfears committed Jun 25, 2024
1 parent 98d091a commit f4d76ae
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
82 changes: 77 additions & 5 deletions src/lib/CanvasModal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,82 @@ import type {Point, DrawData} from '$lib/types.ts';



const openCanvasModal = function(Title:string, messege:string) {
console.log("openCanvasModal");
console.log("function called");
openModal(CanvasModal, {title: Title, messege: messege});
function normalizeData(data:Point[][], width:number, height?:number, offsetX?:number, offsetY?:number):Point[][]{

if(!height){
height = -1; //auto scale height
}
if(!offsetX){
offsetX = 0;
}
if(!offsetY){
offsetY = 0;
}
let smallestX = Number.MAX_VALUE;
let bigestX = Number.MIN_VALUE;
let smallestY = Number.MAX_VALUE;
let bigestY = Number.MIN_VALUE;
let outputData:Point[][] = [];
for(let line of data){
for(let point of line){
if(point.x < smallestX){
smallestX = point.x;
}
if(point.x > bigestX){
bigestX = point.x;
}
if(point.y < smallestY){
smallestY = point.y;
}
if(point.y > bigestY){
bigestY = point.y;
}
}
}

let defaultWidth = bigestX - smallestX;
let defaultHeight = bigestY - smallestY;
let ratio = defaultWidth / defaultHeight;
console.log("width", width);
console.log("height", height);
if(height < 2 && width < 2){
height = defaultHeight;
width = defaultWidth;
}
if(height < 2){
height = width / ratio;
}
if(width < 2){
width = height * ratio;
}
for(let line of data){
let newLine:Point[] = [];
for(let point of line){
point.x = (((point.x - smallestX) / (bigestX - smallestX)) * width) + offsetX;
point.y = (((point.y - smallestY) / (bigestY - smallestY)) * height) + offsetY;
newLine.push(point);
}
outputData.push(newLine);
}
return outputData;
}

export { openCanvasModal, CanvasModal};
function renderCanvas(drawData:Point[][]){
console.log(drawData);
if(ctx){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = 'black';
ctx.lineWidth = 3;
for(let line of drawData){
ctx.beginPath();
console.log(line[0]);
ctx.moveTo(line[0].x, line[0].y);
for(let i = 1; i < line.length; i++){
ctx.lineTo(line[i].x, line[i].y);
}
ctx.stroke();
}
}

}
export { normalizeData, renderCanvas};
4 changes: 2 additions & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Reexport your entry components here

import { openCanvasModal } from './CanvasModal/index.js';
import { normalizeData, renderCanvas } from './CanvasModal/index.js';
export { default as Card } from "./card/card.svelte";
export { default as AssetSelector } from "./assetSelector/assetSelector.svelte";
export { default as ImagePoster } from "./imagePoster/imagePoster.svelte";
Expand All @@ -9,6 +9,6 @@ export { default as YoutubePoster } from "./youtubePoster/youtubePoster.svelte";
export { default as CanvasModal } from "./CanvasModal/Modal.svelte";

export type * from "./types.js";
export { openCanvasModal };
export { normalizeData, renderCanvas};


0 comments on commit f4d76ae

Please sign in to comment.