530 |
530 |
531 svg_root.setAttribute('viewBox',new_desc.bbox.join(" ")); |
531 svg_root.setAttribute('viewBox',new_desc.bbox.join(" ")); |
532 current_visible_page = page_name; |
532 current_visible_page = page_name; |
533 }; |
533 }; |
534 |
534 |
|
535 /* From https://jsfiddle.net/ibowankenobi/1mmh7rs6/6/ */ |
|
536 function getAbsoluteCTM(element){ |
|
537 var height = svg_root.height.baseVal.value, |
|
538 width = svg_root.width.baseVal.value, |
|
539 viewBoxRect = svg_root.viewBox.baseVal, |
|
540 vHeight = viewBoxRect.height, |
|
541 vWidth = viewBoxRect.width; |
|
542 if(!vWidth || !vHeight){ |
|
543 return element.getCTM(); |
|
544 } |
|
545 var sH = height/vHeight, |
|
546 sW = width/vWidth, |
|
547 matrix = svg_root.createSVGMatrix(); |
|
548 matrix.a = sW; |
|
549 matrix.d = sH |
|
550 var realCTM = element.getCTM().multiply(matrix.inverse()); |
|
551 realCTM.e = realCTM.e/sW + viewBoxRect.x; |
|
552 realCTM.f = realCTM.f/sH + viewBoxRect.y; |
|
553 return realCTM; |
|
554 } |
|
555 |
|
556 function apply_reference_frames(){ |
|
557 const matches = svg_root.querySelectorAll("g[svghmi_x_offset]"); |
|
558 matches.forEach((group) => { |
|
559 let [x,y] = ["x", "y"].map((axis) => Number(group.getAttribute("svghmi_"+axis+"_offset"))); |
|
560 let ctm = getAbsoluteCTM(group); |
|
561 // zero translation part of CTM |
|
562 // to only apply rotation/skewing to offset vector |
|
563 ctm.e = 0; |
|
564 ctm.f = 0; |
|
565 let invctm = ctm.inverse(); |
|
566 let vect = new DOMPoint(x, y); |
|
567 let newvect = vect.matrixTransform(invctm); |
|
568 let transform = svg_root.createSVGTransform(); |
|
569 transform.setTranslate(newvect.x, newvect.y); |
|
570 group.transform.baseVal.appendItem(transform); |
|
571 ["x", "y"].forEach((axis) => group.removeAttribute("svghmi_"+axis+"_offset")); |
|
572 }); |
|
573 } |
|
574 |
535 // Once connection established |
575 // Once connection established |
536 ws.onopen = function (evt) { |
576 ws.onopen = function (evt) { |
|
577 apply_reference_frames(); |
537 init_widgets(); |
578 init_widgets(); |
538 send_reset(); |
579 send_reset(); |
539 // show main page |
580 // show main page |
540 prepare_svg(); |
581 prepare_svg(); |
541 switch_page(default_page); |
582 switch_page(default_page); |