@@ -24,11 +24,15 @@ let INITIAL_BASIS = [null, null];
2424let BASIS_HANDLE_ACTIVE = [ false , false ] ;
2525let MOUSE_TOLERANCE = 0.2 ;
2626let CURRENT_MODE = "free-movement-mode" ;
27+ let DELTA_ROTATION = 1 ;
28+ let TARGET_ROTATION = [ null , null ] ;
29+ let STATE = "idle" ;
2730
2831// P5
2932
3033function setup ( ) {
3134 DOM_MODE_BUTTONS [ 0 ] . click ( ) ;
35+ DOM_PLAY_TRANSFORMATION_BUTTON . addEventListener ( "click" , playTransformation ) ;
3236
3337 createCanvas ( windowWidth + 200 , windowHeight + 200 , P2D , DOM_CANVAS ) ;
3438 BASIS [ 0 ] = createVector ( 1 , 0 ) ;
@@ -61,7 +65,41 @@ function draw() {
6165 drawBasisArrows ( BASIS ) ;
6266 drawBasisHandles ( BASIS ) ;
6367
64- CANVAS_HAS_CHANGED = false ;
68+ 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+
81+ if ( TARGET_ROTATION [ 0 ] === 0 && TARGET_ROTATION [ 1 ] === 0 ) {
82+ STATE = "scaling" ;
83+ }
84+
85+ 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 ;
89+ }
90+
91+ 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 ;
95+ }
96+ }
97+ else if ( STATE === "scaling" ) {
98+ STATE = "idle"
99+ }
100+ else {
101+ CANVAS_HAS_CHANGED = false ;
102+ }
65103}
66104
67105function windowResized ( ) {
@@ -128,6 +166,16 @@ function mouseDragged() {
128166 }
129167}
130168
169+ function playTransformation ( ) {
170+ const transformation = domGetMatrix ( DOM_TRANSFROMATION_MATRIX ) ;
171+
172+ TARGET_ROTATION [ 0 ] = ( atan2 ( transformation [ 1 ] [ 0 ] , transformation [ 0 ] [ 0 ] ) - atan2 ( 0 , 1 ) ) * RAD_TO_DEG ;
173+ TARGET_ROTATION [ 1 ] = ( atan2 ( transformation [ 1 ] [ 1 ] , transformation [ 0 ] [ 1 ] ) - atan2 ( 1 , 0 ) ) * RAD_TO_DEG ;
174+
175+ STATE = "rotating" ;
176+ CANVAS_HAS_CHANGED = true ;
177+ }
178+
131179// Canvas utils
132180
133181function drawBasisArrows ( basis ) {
@@ -208,6 +256,15 @@ function drawBasisHandles(basis) {
208256
209257// DOM utils
210258
259+ function domGetMatrix ( element ) {
260+ const matrix = [
261+ [ Number ( element . children [ 0 ] . value ) , Number ( element . children [ 1 ] . value ) ] ,
262+ [ Number ( element . children [ 2 ] . value ) , Number ( element . children [ 3 ] . value ) ] ,
263+ ] ;
264+
265+ return matrix ;
266+ }
267+
211268function domSetMatrix ( element , matrix , childElementName ) {
212269 element . replaceChildren ( ) ;
213270
@@ -263,3 +320,24 @@ function domValidateInputTransformation() {
263320function basisToMatrix ( basis ) {
264321 return [ [ basis [ 0 ] . x , basis [ 1 ] . x ] , [ basis [ 0 ] . y , basis [ 1 ] . y ] ] ;
265322}
323+
324+ function rotateBasis ( basis , mat ) {
325+ const a = mat [ 0 ] [ 0 ] ;
326+ const b = mat [ 0 ] [ 1 ] ;
327+ const c = mat [ 1 ] [ 0 ] ;
328+ const d = mat [ 1 ] [ 1 ] ;
329+
330+ let e = basis . x ;
331+ let f = basis . y ;
332+
333+ basis . set ( a * e + b * f , c * e + d * f ) ;
334+ return basis ;
335+ }
336+
337+ function rotationMatrix ( degrees ) {
338+ let theta = radians ( degrees ) ;
339+ return [
340+ [ cos ( theta ) , - sin ( theta ) ] ,
341+ [ sin ( theta ) , cos ( theta ) ]
342+ ] ;
343+ }
0 commit comments