Skip to content

Commit 1c814af

Browse files
committed
Add basis scaling transformation
1 parent 7020831 commit 1c814af

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

main.js

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ let BASIS_HANDLE_ACTIVE = [false, false];
2525
let MOUSE_TOLERANCE = 0.2;
2626
let CURRENT_MODE = "free-movement-mode";
2727
let DELTA_ROTATION = 1;
28+
let DELTA_SCALE = 0.01;
2829
let TARGET_ROTATION = [null, null];
30+
let TARGET_SCALE = [null, null];
2931
let STATE = "idle";
3032

3133
// P5
@@ -66,36 +68,64 @@ function draw() {
6668
drawBasisHandles(BASIS);
6769

6870
if (STATE === "rotating") {
69-
console.log(TARGET_ROTATION);
70-
71-
if (abs(TARGET_ROTATION[0]) < DELTA_ROTATION) {
72-
rotateBasis(BASIS[0], rotationMatrix(TARGET_ROTATION[0]))
73-
TARGET_ROTATION[0] = 0;
74-
}
75-
76-
if (abs(TARGET_ROTATION[1]) < DELTA_ROTATION) {
77-
rotateBasis(BASIS[1], rotationMatrix(TARGET_ROTATION[1]))
78-
TARGET_ROTATION[1] = 0;
79-
}
80-
8171
if (TARGET_ROTATION[0] === 0 && TARGET_ROTATION[1] === 0) {
8272
STATE = "scaling";
8373
}
8474

8575
if (TARGET_ROTATION[0] !== 0) {
86-
const direction = abs(TARGET_ROTATION[0]) / TARGET_ROTATION[0];
87-
rotateBasis(BASIS[0], rotationMatrix(DELTA_ROTATION * direction));
88-
TARGET_ROTATION[0] -= DELTA_ROTATION * direction;
76+
if (abs(TARGET_ROTATION[0]) < DELTA_ROTATION) {
77+
applyTransformation(BASIS[0], rotationMatrix(TARGET_ROTATION[0]))
78+
TARGET_ROTATION[0] = 0;
79+
}
80+
else {
81+
const direction = abs(TARGET_ROTATION[0]) / TARGET_ROTATION[0];
82+
applyTransformation(BASIS[0], rotationMatrix(DELTA_ROTATION * direction));
83+
TARGET_ROTATION[0] -= DELTA_ROTATION * direction;
84+
}
8985
}
9086

9187
if (TARGET_ROTATION[1] !== 0) {
92-
const direction = abs(TARGET_ROTATION[1]) / TARGET_ROTATION[1];
93-
rotateBasis(BASIS[1], rotationMatrix(DELTA_ROTATION * direction));
94-
TARGET_ROTATION[1] -= DELTA_ROTATION * direction;
88+
if (abs(TARGET_ROTATION[1]) < DELTA_ROTATION) {
89+
applyTransformation(BASIS[1], rotationMatrix(TARGET_ROTATION[1]))
90+
TARGET_ROTATION[1] = 0;
91+
}
92+
else {
93+
const direction = abs(TARGET_ROTATION[1]) / TARGET_ROTATION[1];
94+
applyTransformation(BASIS[1], rotationMatrix(DELTA_ROTATION * direction));
95+
TARGET_ROTATION[1] -= DELTA_ROTATION * direction;
96+
}
9597
}
9698
}
9799
else if (STATE === "scaling") {
98-
STATE = "idle"
100+
if (TARGET_SCALE[0] === 0 && TARGET_SCALE[1] === 0) {
101+
STATE = "idle";
102+
}
103+
104+
if (TARGET_SCALE[0] !== 0) {
105+
if (abs(TARGET_SCALE[0]) < DELTA_SCALE) {
106+
const direction = abs(TARGET_SCALE[0]) / TARGET_SCALE[0];
107+
applyTransformation(BASIS[0], scalingMatrix(1 + TARGET_SCALE[0] / magnitude(BASIS[0]) * direction));
108+
TARGET_SCALE[0] = 0;
109+
}
110+
else {
111+
const direction = abs(TARGET_SCALE[0]) / TARGET_SCALE[0];
112+
applyTransformation(BASIS[0], scalingMatrix(1 + DELTA_SCALE / magnitude(BASIS[0]) * direction));
113+
TARGET_SCALE[0] -= DELTA_SCALE * direction;
114+
}
115+
}
116+
117+
if (TARGET_SCALE[1] !== 0) {
118+
if (abs(TARGET_SCALE[1]) < DELTA_SCALE) {
119+
const direction = abs(TARGET_SCALE[1]) / TARGET_SCALE[1];
120+
applyTransformation(BASIS[1], scalingMatrix(1 + TARGET_SCALE[1] / magnitude(BASIS[1]) * direction));
121+
TARGET_SCALE[1] = 0;
122+
}
123+
else {
124+
const direction = abs(TARGET_SCALE[1]) / TARGET_SCALE[1];
125+
applyTransformation(BASIS[1], scalingMatrix(1 + DELTA_SCALE / magnitude(BASIS[1]) * direction));
126+
TARGET_SCALE[1] -= DELTA_SCALE * direction;
127+
}
128+
}
99129
}
100130
else {
101131
CANVAS_HAS_CHANGED = false;
@@ -167,11 +197,17 @@ function mouseDragged() {
167197
}
168198

169199
function playTransformation() {
200+
BASIS[0].set(1, 0);
201+
BASIS[1].set(0, 1);
202+
170203
const transformation = domGetMatrix(DOM_TRANSFROMATION_MATRIX);
171204

172205
TARGET_ROTATION[0] = (atan2(transformation[1][0], transformation[0][0]) - atan2(0, 1)) * RAD_TO_DEG;
173206
TARGET_ROTATION[1] = (atan2(transformation[1][1], transformation[0][1]) - atan2(1, 0)) * RAD_TO_DEG;
174207

208+
TARGET_SCALE[0] = sqrt(pow(transformation[1][0], 2) + pow(transformation[0][0], 2)) - 1;
209+
TARGET_SCALE[1] = sqrt(pow(transformation[1][1], 2) + pow(transformation[0][1], 2)) - 1;
210+
175211
STATE = "rotating";
176212
CANVAS_HAS_CHANGED = true;
177213
}
@@ -321,7 +357,7 @@ function basisToMatrix(basis) {
321357
return [ [basis[0].x, basis[1].x], [basis[0].y, basis[1].y]];
322358
}
323359

324-
function rotateBasis(basis, mat) {
360+
function applyTransformation(basis, mat) {
325361
const a = mat[0][0];
326362
const b = mat[0][1];
327363
const c = mat[1][0];
@@ -341,3 +377,14 @@ function rotationMatrix(degrees) {
341377
[sin(theta), cos(theta)]
342378
];
343379
}
380+
381+
function scalingMatrix(scale) {
382+
return [
383+
[scale, 0],
384+
[0, scale]
385+
]
386+
}
387+
388+
function magnitude(basis) {
389+
return sqrt(basis.x * basis.x + basis.y * basis.y);
390+
}

0 commit comments

Comments
 (0)