22
33const DOM_CANVAS = document . querySelector ( "canvas" ) ;
44const DOM_TRANSFROMATION_MATRIX = document . querySelector ( ".transformation-matrix" ) ;
5+ const DOM_MODE_BUTTONS = document . querySelectorAll ( ".header-bottom input" )
6+ const DOM_INSTRUCTION_TEXT = document . querySelector ( ".instruction-text" )
57
68// Colors
79
@@ -20,15 +22,27 @@ let BASIS = [null, null];
2022let INITIAL_BASIS = [ null , null ] ;
2123let BASIS_HANDLE_ACTIVE = [ false , false ] ;
2224let MOUSE_TOLERANCE = 0.2 ;
25+ let CURRENT_MODE = "free-movement-mode"
2326
2427// P5
2528
2629function setup ( ) {
30+ DOM_MODE_BUTTONS [ 0 ] . click ( ) ;
31+
2732 createCanvas ( windowWidth + 200 , windowHeight + 200 , P2D , DOM_CANVAS ) ;
2833 BASIS [ 0 ] = createVector ( 1 , 0 ) ;
2934 BASIS [ 1 ] = createVector ( 0 , 1 ) ;
3035 INITIAL_BASIS [ 0 ] = createVector ( 1 , 0 ) ;
3136 INITIAL_BASIS [ 1 ] = createVector ( 0 , 1 ) ;
37+
38+ DOM_MODE_BUTTONS . forEach ( button => {
39+ button . addEventListener ( "change" , ( e ) => {
40+ CURRENT_MODE = e . target . value ;
41+ domChangeMode ( e . target . value ) ;
42+ } )
43+ } )
44+
45+ domChangeMode ( CURRENT_MODE ) ;
3246}
3347
3448function draw ( ) {
@@ -46,7 +60,6 @@ function draw() {
4660 drawBasisArrows ( BASIS ) ;
4761 drawBasisHandles ( BASIS ) ;
4862
49- domSetMatrix ( DOM_TRANSFROMATION_MATRIX , basisToMatrix ( BASIS ) , "span" ) ;
5063 CANVAS_HAS_CHANGED = false ;
5164}
5265
@@ -56,6 +69,10 @@ function windowResized() {
5669}
5770
5871function mouseMoved ( ) {
72+ if ( CURRENT_MODE !== "free-movement-mode" ) {
73+ return ;
74+ }
75+
5976 const col = ( mouseX - ( windowWidth / 2 ) ) / SCALE ;
6077 const row = ( ( windowHeight / 2 ) - mouseY ) / SCALE ;
6178
@@ -81,6 +98,10 @@ function mouseMoved() {
8198}
8299
83100function mouseDragged ( ) {
101+ if ( CURRENT_MODE !== "free-movement-mode" ) {
102+ return ;
103+ }
104+
84105 let col = ( mouseX - ( windowWidth / 2 ) ) / SCALE ;
85106 const nearestCol = round ( col ) ;
86107 let row = ( ( windowHeight / 2 ) - mouseY ) / SCALE ;
@@ -97,10 +118,12 @@ function mouseDragged() {
97118 if ( BASIS_HANDLE_ACTIVE [ 0 ] ) {
98119 BASIS [ 0 ] . set ( col , row ) ;
99120 CANVAS_HAS_CHANGED = true ;
121+ domSetMatrix ( DOM_TRANSFROMATION_MATRIX , basisToMatrix ( BASIS ) , "span" ) ;
100122 }
101123 else if ( BASIS_HANDLE_ACTIVE [ 1 ] ) {
102124 BASIS [ 1 ] . set ( col , row ) ;
103125 CANVAS_HAS_CHANGED = true ;
126+ domSetMatrix ( DOM_TRANSFROMATION_MATRIX , basisToMatrix ( BASIS ) , "span" ) ;
104127 }
105128}
106129
@@ -192,6 +215,7 @@ function domSetMatrix(element, matrix, childElementName) {
192215 let item = document . createElement ( childElementName ) ;
193216 if ( childElementName === "input" ) {
194217 item . value = matrix [ i ] [ j ] ;
218+ item . classList . add ( "matrix-input" )
195219 }
196220 else {
197221 item . innerText = matrix [ i ] [ j ] ;
@@ -201,6 +225,19 @@ function domSetMatrix(element, matrix, childElementName) {
201225 }
202226}
203227
228+ function domChangeMode ( mode ) {
229+ document . querySelector ( "body" ) . dataset . mode = mode ;
230+ if ( mode === "free-movement-mode" ) {
231+ DOM_INSTRUCTION_TEXT . innerText = "Click and drag the heads of the basis vectors to see the required transformation" ;
232+ domSetMatrix ( DOM_TRANSFROMATION_MATRIX , basisToMatrix ( BASIS ) , "span" ) ;
233+ }
234+ else {
235+ DOM_INSTRUCTION_TEXT . innerText = "Input required transformation matrix and click play"
236+ domSetMatrix ( DOM_TRANSFROMATION_MATRIX , basisToMatrix ( BASIS ) , "input" ) ;
237+ }
238+ CANVAS_HAS_CHANGED = true ;
239+ }
240+
204241// Math utils
205242
206243function basisToMatrix ( basis ) {
0 commit comments