1- import React from "https://unpkg.com/es-react@latest/dev/react.js" ;
2- import ReactDOM from "https://unpkg.com/es-react@latest/dev/react-dom.js" ;
1+ // import React from "https://unpkg.com/es-react@latest/dev/react.js";
2+ // import ReactDOM from "https://unpkg.com/es-react@latest/dev/react-dom.js";
3+ import React from "https://unpkg.com/es-react@latest/react.js" ;
4+ import ReactDOM from "https://unpkg.com/es-react@latest/react-dom.js" ;
35// import PropTypes from "https://unpkg.com/es-react@latest/dev/prop-types.js";
46import htm from "../js/htm.js" ;
57import { init_draw2d } from './wdk_draw2d.js'
@@ -43,7 +45,8 @@ function Comments() {
4345 repo ="28raining/28raining.github.io "
4446 repo-id ="MDEwOlJlcG9zaXRvcnkxMjcyMzY4NjM= "
4547 category-id ="DIC_kwDOB5V6_84CUAVa "
46- mapping ="pathname "
48+ mapping ="number "
49+ term ="1 "
4750 strict ="0 "
4851 reactions-enabled ="0 "
4952 emit-metadata ="0 "
@@ -370,6 +373,43 @@ function unitStrToVal(unit) {
370373 console . log ( "You used a unit I don't know about " , unit )
371374}
372375
376+ function centerSchematic ( schem ) {
377+ var canvasHolder = document . getElementById ( 'canvasHolder' ) ;
378+ // var wrapperComputedStyle = window.getComputedStyle(canvasHolder, null);
379+ var wrapperWidth = canvasHolder . clientWidth - 26 ;
380+ var wrapperHeight = canvasHolder . offsetHeight ;
381+ var centerX = ( wrapperWidth - 16 ) / 2 ;
382+ var centerY = ( wrapperHeight - 128 ) / 2 ;
383+ console . log ( centerX , centerY ) ;
384+ var minX = 0 ;
385+ var maxX = 0 ;
386+ var minY = 0 ;
387+ var maxY = 0 ;
388+ //find min and max x coordinates
389+ schem . forEach ( item => {
390+ if ( item . type != "connection" ) {
391+ if ( ( minX == 0 ) || ( item . x < minX ) ) minX = item . x ;
392+ if ( ( maxX == 0 ) || ( item . x > maxX ) ) maxX = item . x ;
393+ if ( ( minY == 0 ) || ( item . y < minY ) ) minY = item . y ;
394+ if ( ( maxY == 0 ) || ( item . y > maxY ) ) maxY = item . y ;
395+ }
396+ } ) ;
397+ var schemX = ( maxX + minX ) / 2.0 ;
398+ var schemY = ( maxY + minY ) / 2.0 ;
399+ if ( ( centerX < schemX ) || ( centerY < schemY ) ) return schem ;
400+ var xShift = 16 * Math . round ( ( centerX - schemX ) / 16 ) ;
401+ var yShift = 16 * Math . round ( ( centerY - schemY ) / 16 ) ;
402+ schem . forEach ( item => {
403+ if ( item . type != "connection" ) {
404+ item . x = item . x + xShift ;
405+ item . y = item . y + yShift ;
406+ }
407+ } ) ;
408+ console . log ( xShift , yShift , maxX , minX , schem ) ;
409+ return schem ;
410+
411+ }
412+
373413class Game extends React . Component {
374414 constructor ( props ) {
375415 super ( props ) ;
@@ -543,14 +583,14 @@ class Game extends React.Component {
543583 }
544584 } ) ;
545585
546- console . log ( 'schstate' , schematicState ) ;
586+ // console.log('schstate', schematicState);
547587
548588
549589 // console.log('newElementMap', newElementMap, elements)
550590 current . elements = elements ;
551591 current . schematic = schematicState ;
552592
553- console . log ( schematicReadiness ) ;
593+ // console.log(schematicReadiness);
554594
555595 if ( this . preventNewState ) {
556596 this . setState ( {
@@ -591,8 +631,10 @@ class Game extends React.Component {
591631 }
592632
593633 componentDidMount ( ) {
634+ var current = this . state . history [ this . state . history . length - 1 ] ;
635+ centerSchematic ( current . schematic ) ;
594636 // #after dom tree is updated
595- this . a = new init_draw2d ( ( a , b ) => this . handledropCb ( a , b ) , ( b ) => this . handleCanvasChange ( b ) ) ;
637+ this . a = new init_draw2d ( ( a , b ) => this . handledropCb ( a , b ) , ( b ) => this . handleCanvasChange ( b ) , current . schematic ) ;
596638 // pass this.tff as above
597639 this . a . addEvL ( this . a . view , this . a . writer , ( canvasState ) => this . handleCanvasChange ( canvasState ) ) ;
598640 this . TESTER = document . getElementById ( 'tester' ) ;
@@ -640,7 +682,6 @@ class Game extends React.Component {
640682 console . log ( 'here' , this . state . history , a )
641683 this . preventNewState = true ;
642684 this . state . elOnSchematic = { } ;
643- // this.a = new init_draw2d((a, b) => this.handledropCb(a, b), (b) => this.handleCanvasChange(b));
644685
645686 this . a . reUpdateCanvas ( this . state . history [ this . state . history . length - 1 ] . schematic , ( b ) => this . handleCanvasChange ( b ) ) ;
646687 this . preventNewState = false ;
@@ -657,7 +698,7 @@ class Game extends React.Component {
657698
658699 render ( ) {
659700 const current = this . state . history [ this . state . history . length - 1 ] ;
660- console . log ( this . state )
701+ // console.log(this.state)
661702
662703 // // Use state (variable containing all user inputs) to do MNA (modified nodal analysis)
663704 //
0 commit comments