-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinate-transform.js
95 lines (75 loc) · 2.92 KB
/
coordinate-transform.js
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
/*
Calculates the scale factor based on
- The canvas dimensions
- The print bounding box
- The canvas padding
*/
function calculateScaleFactor() {
//Get the width and height of the print
let printWidth = boundingBox.maxX - boundingBox.minX;
let printHeight = boundingBox.maxY - boundingBox.minY;
//Calculate the available width and height of the canvas after the padding is subtracted
let availableWidth = canvasWidth - 2 * padding;
let availableHeight = canvasHeight - 2 * padding;
//If we are using WEBGL, instead of using canvas width and height we'll use a fixed size
//and assume a normal web ratio of 16:9. We'll scale the values based on the ratio of the
//canvas width and height
if (renderer == WEBGL) {
let baseRes = 720;
let canvasRatio = canvasWidth / canvasHeight;
availableWidth = baseRes * canvasRatio;
availableHeight = baseRes / canvasRatio;
}
//Calculate the scale factor based on the print size and the canvas size
let xScaleFactor = availableWidth / printWidth;
let yScaleFactor = availableHeight / printHeight;
console.log(
availableWidth,
availableHeight,
printWidth,
printHeight,
xScaleFactor,
yScaleFactor
);
//Return the smaller of the two scale factors to make sure it fits
return Math.min(xScaleFactor, yScaleFactor);
}
/*
Calculates the X and Y offsets to that the print is centered on the canvas
*/
function calculateOffsets() {
//Calculate the available width and height of the canvas after the padding is subtracted
let availableWidth = canvasWidth - 2 * padding;
let availableHeight = canvasHeight - 2 * padding;
//Calculate the width and height of the print
let printWidth = boundingBox.maxX - boundingBox.minX;
let printHeight = boundingBox.maxY - boundingBox.minY;
//The X offset is the padding plus the available width minus the scaled print width divided by 2
let xOffset = padding + (availableWidth - printWidth * scaleFactor) / 2;
//The Y offset is the padding plus the available height minus the scaled print height divided by 2
let yOffset = padding + (availableHeight - printHeight * scaleFactor) / 2;
//Return the offsets as an object
return { xOffset, yOffset };
}
//Transform an XYZ vector to the canvas coordinate system based on the scale factor and the offsets
function transformPositionToCanvasCoordinates(vector) {
result = vector.copy();
//Start by subtracting the bounding box minimums
result.x -= boundingBox.minX;
result.y -= boundingBox.minY;
//Scale the vector
result.mult(scaleFactor);
//Add the offsets
result.x += xOffset;
result.y += yOffset;
//Flip the Y axis
result.y = canvasHeight - result.y;
//Expand the Z axis
result.z *= zExpandSlider.value();
//If this is webgl, subtract half of the canvas height and width because the origin is in the center of the canvas
if (renderer == WEBGL) {
result.x -= canvasWidth / 2;
result.y -= canvasHeight / 2;
}
return result;
}