style body min height 600px box wrapper overflow visible position absolute transform origin 50 50 user select none width 200px height 100px left 110px top 110px dot r 10px fill 1E88E5 position absolute border radius 100px stroke orange user select none transition delay 0 2s opacity 0 fill 1E88E5 r 5 stroke color blue stroke red box wrapper hover dot box wrapper hover rotate link transition delay 0 0s opacity 1 dot hover fill 0D47A1 rotate link position absolute r 1px height 15px background color 1E88E5 cx 20px cy calc 50 0 5px z index 1 opacity 0 transition delay 0 2s dot left top cx 5px cy 5px cursor nw resize dot left bottom cx 5px cy calc 100 5px cursor sw resize dot right top cx calc 100 5px cy 5px cursor ne resize dot right bottom cx calc 100 5px cy calc 100 5px cursor se resize dot top mid cx calc 50 0px cy 5px cursor n resize dot left mid cx 5px cy calc 50 0px cursor w resize dot right mid cx calc 100 5px cy calc 50 0px cursor e resize dot bottom mid cx calc 50 0px cy calc 100 5px cursor s resize dot rotate cx calc 50 0px cy calc 0 20px cursor url https findicons com files icons 1620 crystal_project 16 rotate_ccw png auto style svg id svgcontainer style width 100 height 500px border 1px solid blue svg id rect0 x 50 y 40 width 100 height 100 style overflow visible class box wrapper g class subgroup rect x 0 y 0 width 100 height 100 fill yellow stroke orange rect circle class dot rotate circle circle class dot left top circle circle class dot left bottom circle circle class dot top mid circle circle class dot bottom mid circle circle class dot left mid circle circle class dot right mid circle circle class dot right bottom circle circle class dot right top circle circle class rotate link circle g svg svg div style position absolute left 10px top 10px button onclick addrect Add Rect button div script function addDrag elm let elmt elm let initX initY mousePressX mousePressY function repositionElement xx yy elmt setAttribute x xx elmt setAttribute y yy drag support elmt addEventListener mousedown function event if event target classList contains dot true return initX parseInt elmt getAttribute x this offsetLeft initY parseInt elmt getAttribute y this offsetTop mousePressX event clientX mousePressY event clientY function eventMoveHandler event repositionElement initX event clientX mousePressX initY event clientY mousePressY window addEventListener mousemove eventMoveHandler false window addEventListener mouseup function eventEndHandler window removeEventListener mousemove eventMoveHandler false window removeEventListener mouseup eventEndHandler false false function addRotate elm let elmt elm elmt style transform origin left top elmt children 0 style transform origin center center handle rotation let rotatedot elmt querySelector rotate if rotatedot console log adding rotate dot const svgns http www w3 org 2000 svg rotatedot document createElementNS svgns circle rotatedot classList add dot rotatedot classList add rotate rotatedot style fill blue elmt children 0 appendChild rotatedot let dotRotate elmt querySelector dot rotate rotatedot setAttribute cx 50 rotatedot setAttribute cy 20px rotatedot addEventListener mousedown function event initX this offsetLeft initY this offsetTop initX parseInt elmt getAttribute x this offsetLeft initY parseInt elmt getAttribute y this offsetTop mousePressX event clientX mousePressY event clientY let arrow elmt querySelector box let arrowRects elmt getBoundingClientRect let arrowX arrowRects left arrowRects width 2 let arrowY arrowRects top arrowRects height 2 function eventMoveHandler event let angle Math atan2 event clientY arrowY event clientX arrowX Math PI 2 rotateBox angle 180 Math PI window addEventListener mousemove eventMoveHandler false window addEventListener mouseup function eventEndHandler window removeEventListener mousemove eventMoveHandler false window removeEventListener mouseup eventEndHandler false false function rotateBox deg elmt children 0 style transform rotate deg deg elmt parentElement setAttribute transform rotate deg elmt rotation deg end addRotate function addResize elm let elmt elm function getDot dname let dotEl elmt querySelector dname if dotEl const svgns http www w3 org 2000 svg dotEl document createElementNS svgns circle dotEl classList add dot dotEl classList add dname elmt children 0 appendChild dotEl return dotEl let dotRotate elmt querySelector dot rotate rotatedot setAttribute cx 50 rotatedot setAttribute cy 20px handle resize let rightMid getDot right mid let leftMid getDot left mid let topMid getDot top mid let bottomMid getDot bottom mid let leftTop getDot left top let rightTop getDot right top let rightBottom getDot right bottom let leftBottom getDot left bottom moveDots rightMid style background color green rightMid addEventListener mousedown e resizeHandler e false false true false leftMid addEventListener mousedown e resizeHandler e true false true false topMid addEventListener mousedown e resizeHandler e false true false true bottomMid addEventListener mousedown e resizeHandler e false false false true leftTop addEventListener mousedown e resizeHandler e true true true true rightTop addEventListener mousedown e resizeHandler e false true true true rightBottom addEventListener mousedown e resizeHandler e false false true true leftBottom addEventListener mousedown e resizeHandler e true false true true function resize w h elmt setAttribute width w w px elmt setAttribute height h style height h px function moveDots rightMid setAttribute cx 100 rightMid setAttribute cy 50 leftMid setAttribute cx 0 leftMid setAttribute cy 50 topMid setAttribute cx 50 topMid setAttribute cy 0 bottomMid setAttribute cx 50 bottomMid setAttribute cy 100 leftTop setAttribute cx 0 leftTop setAttribute cy 0 leftBottom setAttribute cx 0 leftBottom setAttribute cy 100 rightTop setAttribute cx 100 rightTop setAttribute cy 0 rightBottom setAttribute cx 100 rightBottom setAttribute cy 100 leftBottom setAttribute cx 0 leftBottom setAttribute cy 100 let dotRotate elmt querySelector dot rotate dotRotate setAttribute cx 50 dotRotate setAttribute cy 20px function getCurrentRotation el let st window getComputedStyle el null let tm st getPropertyValue webkit transform st getPropertyValue moz transform st getPropertyValue ms transform st getPropertyValue o transform st getPropertyValue transform none if tm none let values tm split 1 split 0 split let angle Math round Math atan2 values 1 values 0 180 Math PI return angle 0 angle 360 angle if typeof el rotation undefined return el rotation return 0 function resizeHandler event left false top false xResize false yResize false initX elmt offsetLeft initY elmt offsetTop initX parseInt elmt getAttribute x this offsetLeft initY parseInt elmt getAttribute y this offsetTop mousePressX event clientX mousePressY event clientY initW elmt offsetWidth initH elmt offsetHeight initW parseInt elmt getAttribute width this offsetLeft initH parseInt elmt getAttribute height this offsetTop initCX initX initW 0 5 initCY initY initH 0 5 initRotate getCurrentRotation elmt let initRadians initRotate Math PI 180 let cosFraction Math cos initRadians let sinFraction Math sin initRadians function eventMoveHandler event let wDiff event clientX mousePressX let hDiff event clientY mousePressY let rotatedWDiff cosFraction wDiff sinFraction hDiff let rotatedHDiff cosFraction hDiff sinFraction wDiff if xResize yResize rotatedHDiff 0 if yResize xResize rotatedWDiff 0 let newW initW rotatedWDiff let newH initH rotatedHDiff let newCX initX initW rotatedWDiff 0 5 let newCY initY initH rotatedHDiff 0 5 let dw initW newW let dh initH newH let xx0 initCX cosFraction initW sinFraction initH 0 5 let yy0 initCY cosFraction initH sinFraction initW 0 5 let xx newCX cosFraction newW sinFraction newH 0 5 let yy newCY cosFraction newH sinFraction newW 0 5 let dx xx xx0 let dy yy yy0 let newX initX dx let newY initY dy if top newY dh newH 2 dh if left newX dw newW 2 dw elmt setAttribute x newX elmt setAttribute y newY elmt setAttribute width newW elmt setAttribute height newH elmt style left newX px elmt style top newY px elmt style width newW px elmt style height newH px moveDots end eventMoveHandler window addEventListener mousemove eventMoveHandler false window addEventListener mouseup function eventEndHandler window removeEventListener mousemove eventMoveHandler false window removeEventListener mouseup eventEndHandler false end addResize function addrect const svgns http www w3 org 2000 svg let svg document getElementById svgcontainer let svgcont document createElementNS svgns svg svgcont classList add box wrapper svg appendChild svgcont let svggroup document createElementNS svgns g svggroup classList add subgroup svgcont appendChild svggroup let svgrect document createElementNS svgns rect svggroup appendChild svgrect svgcont setAttribute x 50 Math random 260 svgcont setAttribute y 50 Math random 160 svgcont setAttribute width 100 svgcont setAttribute height 100 svgrect setAttribute x 0 svgrect setAttribute y 0 svgrect setAttribute width 100 svgrect setAttribute height 100 svgrect setAttribute fill yellow svgrect setAttribute stroke orange addDrag svgcont addRotate svgcont addResize svgcont svg id rect0 x 50 y 40 width 100 height 100 style overflow visible class box wrapper g class subgroup rect x 0 y 0 width 100 height 100 fill yellow stroke orange rect g svg addDrag document getElementById rect0 addRotate document getElementById rect0 addResize document getElementById rect0 console log ready script
moveDots rightMid style background color green rightMid addEventListener mousedown e resizeHandler e false false true false leftMid addEventListener mousedown e resizeHandler e true false true false topMid addEventListener mousedown e resizeHandler e false true false true bottomMid addEventListener mousedown e resizeHandler e false false false true leftTop addEventListener mousedown e resizeHandler e true true true true rightTop addEventListener mousedown e resizeHandler e false true true true rightBottom addEventListener mousedown e resizeHandler e false false true true leftBottom addEventListener mousedown e resizeHandler e true false true true function resize w h elmt setAttribute width w w px elmt setAttribute height h style height h px function moveDots rightMid setAttribute cx 100 rightMid setAttribute cy 50 leftMid setAttribute cx 0 leftMid setAttribute cy 50 topMid setAttribute cx 50 topMid setAttribute cy 0 bottomMid setAttribute cx 50 bottomMid setAttribute cy 100 leftTop setAttribute cx 0 leftTop setAttribute cy 0 leftBottom setAttribute cx 0 leftBottom setAttribute cy 100 rightTop setAttribute cx 100 rightTop setAttribute cy 0 rightBottom setAttribute cx 100 rightBottom setAttribute cy 100 leftBottom setAttribute cx 0 leftBottom setAttribute cy 100 let dotRotate elmt querySelector dot rotate dotRotate setAttribute cx 50 dotRotate setAttribute cy 20px function getCurrentRotation el let st window getComputedStyle el null let tm st getPropertyValue webkit transform st getPropertyValue moz transform st getPropertyValue ms transform st getPropertyValue o transform st getPropertyValue transform none if tm none let values tm split 1 split 0 split let angle Math round Math atan2 values 1 values 0 180 Math PI return angle 0 angle 360 angle if typeof el rotation undefined return el rotation return 0 function resizeHandler event left false top false xResize false yResize false initX elmt offsetLeft initY elmt offsetTop initX parseInt elmt getAttribute x this offsetLeft initY parseInt elmt getAttribute y this offsetTop mousePressX event clientX mousePressY event clientY initW elmt offsetWidth initH elmt offsetHeight initW parseInt elmt getAttribute width this offsetLeft initH parseInt elmt getAttribute height this offsetTop initCX initX initW 0 5 initCY initY initH 0 5 initRotate getCurrentRotation elmt let initRadians initRotate Math PI 180 let cosFraction Math cos initRadians let sinFraction Math sin initRadians function eventMoveHandler event let wDiff event clientX mousePressX let hDiff event clientY mousePressY let rotatedWDiff cosFraction wDiff sinFraction hDiff let rotatedHDiff cosFraction hDiff sinFraction wDiff if xResize yResize rotatedHDiff 0 if yResize xResize rotatedWDiff 0 let newW initW rotatedWDiff let newH initH rotatedHDiff let newCX initX initW rotatedWDiff 0 5 let newCY initY initH rotatedHDiff 0 5 let dw initW newW let dh initH newH let xx0 initCX cosFraction initW sinFraction initH 0 5 let yy0 initCY cosFraction initH sinFraction initW 0 5 let xx newCX cosFraction newW sinFraction newH 0 5 let yy newCY cosFraction newH sinFraction newW 0 5 let dx xx xx0 let dy yy yy0 let newX initX dx let newY initY dy if top newY dh newH 2 dh if left newX dw newW 2 dw elmt setAttribute x newX elmt setAttribute y newY elmt setAttribute width newW elmt setAttribute height newH elmt style left newX px elmt style top newY px elmt style width newW px elmt style height newH px moveDots end eventMoveHandler window addEventListener mousemove eventMoveHandler false window addEventListener mouseup function eventEndHandler window removeEventListener mousemove eventMoveHandler false window removeEventListener mouseup eventEndHandler false end addResize function addrect const svgns http www w3 org 2000 svg let svg document getElementById svgcontainer let svgcont document createElementNS svgns svg svgcont classList add box wrapper svg appendChild svgcont let svggroup document createElementNS svgns g svggroup classList add subgroup svgcont appendChild svggroup let svgrect document createElementNS svgns rect svggroup appendChild svgrect svgcont setAttribute x 50 Math random 260 svgcont setAttribute y 50 Math random 160 svgcont setAttribute width 100 svgcont setAttribute height 100 svgrect setAttribute x 0 svgrect setAttribute y 0 svgrect setAttribute width 100 svgrect setAttribute height 100 svgrect setAttribute fill yellow svgrect setAttribute stroke orange addDrag svgcont addRotate svgcont addResize svgcont svg id rect0 x 50 y 40 width 100 height 100 style overflow visible class box wrapper g class subgroup rect x 0 y 0 width 100 height 100 fill yellow stroke orange rect g svg addDrag document getElementById rect0 addRotate document getElementById rect0 addResize document getElementById rect0 console log ready script