get optimus size in div element

Discussion in 'JavaScript' started by weavercrowds, Jul 9, 2020.

  1. #1
    For this I have developed a function that creates elements within window.document, the argument is an object element, so the grammar is known, within the function I have added various functionalities such as rotating the elements created in the different XYZ axes or simply the vertical or horizontal centering of a div within a div, this same has been created to teach how a word should be delimited inside a box such as a visible div with borders, in which we are interested in knowing what is the size not to exceed the edges of the div.
    window["$"]=(...a)=>{
        if(!window["isElement"]){window["isElement"]=(obj)=>{try {return obj instanceof HTMLElement;}catch(e){return (typeof obj==="object") && (obj.nodeType===1) && (typeof obj.style === "object") && (typeof obj.ownerDocument ==="object");}}}
        if(!window["optimSize"]){
            window["optimSize"]=(width,height,font,S)=>{
                var font=$({parent:"B",tag:"div",id:"font",style:{position:"absolute",width:"auto",height:"auto",border:"1px solid orange",top:"5px",left:"5px",fontSize:"1px",fontFamily:font},textContent:S});var cost=0;
                for(var i=2;i<800;i++){
                    $(font,{style:{fontSize:i+"px"}});
                    if(font.offsetHeight>height){cost=i-1;break;}
                    if(font.offsetWidth>width){cost=i-1;break;}
                }
                font.remove();
                return cost;
            }
        }
        if(!window["CE"]){window["CE"]=String("kind,style,tag,parent,obj,centerV,centerH,zoom,divTextInDiv").split(",");}
        let l=a.length;
        if(l==1){
            if(isElement(a[0])){return a[0];}else{
                if(typeof a[0]=="string"){return document.getElementById(a[0]);}else{
                    if((a[0].kind!=undefined)||(a[0].kind!="dom")){
                        if(a[0].tag!=undefined){let dom=document.createElement(a[0].tag);
                            $(dom,a[0]);
                            if(a[0].parent!=undefined){$(a[0].parent).appendChild(dom);dom.obj=a[0];}                   
                            return dom;
                        }
                    };
                };
            };   
        };
        if(l==2){
            let dom=$(a[0]);
            let p=a[1];
            for(let property in p){if(!window["CE"].includes(property)){dom[property]=p[property];}}
            if(p.style!=undefined){
                let style=p.style;
                for(let property in style){dom.style[property]=style[property];}       
            }   
            if(p.centerV!=undefined){
                let jk=dom.style.transform;
                if((String(p.centerV)=="true")){$(dom,{style:{margin:"0px",position:"absolute",top:"50%",transform:jk+" translateY(-50%)"}});}
            }
            if(p.centerH!=undefined){
                let jk=dom.style.transform;
                if((String(p.centerH)=="true")){$(dom,{style:{margin:"0px",position:"absolute",left:"50%",transform:jk+" translateX(-50%)"}});}
            }
            if(p.zoom!=undefined){
                let jk=dom.style.transform;
                $(dom,{style:{transform:jk+" scale("+p.zoom+")"}});
            }
            if(p.rotX!=undefined){
                let jk=dom.style.transform;
                $(dom,{style:{transform:jk+" rotateX("+p.rotX+"deg)"}});
            }
            if(p.rotY!=undefined){
                let jk=dom.style.transform;
                $(dom,{style:{transform:jk+" rotateY("+p.rotY+"deg)"}});
            }
            if(p.rotZ!=undefined){
                let jk=dom.style.transform;
                $(dom,{style:{transform:jk+" rotateZ("+p.rotZ+"deg)"}});
            }
            if(p.divTextInDiv!=undefined){
                let font=$({parent:dom,tag:"div",textContent:p.divTextInDiv,centerH:"true",centerV:"true"});
                if((dom.style.width)&&(dom.style.height)){$(font,{style:{fontSize:optimSize(parseInt(dom.style.width),parseInt(dom.style.height),p.style.fontFamily,p.divTextInDiv)+"px"}})}
            }
        }
    }
    var body=document.body;body.id="B";
    
    var k=$({parent:"B",tag:"div",id:"aza",style:{position:"absolute",width:"100px",height:"100px",border:"1px solid orange",top:"100px",left:"100px"}});
    var k2=$({parent:k,tag:"div",id:"aza2",style:{position:"absolute",width:"20px",height:"20px",border:"1px solid purple",backgroundColor:"orange",top:"10px",left:"10px"}});
    $(k2,{centerH:"true",centerV:"true",zoom:2});
    var k3=$({parent:"B",tag:"div",id:"k3",style:{position:"absolute",width:"150px",height:"150px",border:"1px solid orange",top:"5px",left:"5px"},divTextInDiv:"hanna"});
    Code (JavaScript):
    you can paste everything from the browser console
     
    weavercrowds, Jul 9, 2020 IP