Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
    <svg id="container" viewBox="-200 -200 500 500" width="100%" height="500px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <!-- <ellipse id="ellipse" cx="200" cy="200" rx="50" ry="100" style="transform-origin: 200px 200px; transform: matrix(0.87, 0.5, -0.5, 0.87, 0, 0)"></ellipse> -->

      <!-- 旋转 30 度,没有斜切 -->
      <ellipse id="ellipse" cx="0" cy="0" rx="1" ry="1" style="transform: matrix(43.3, 25, -50, 86.6, 200, 200)"></ellipse>

      <!-- <ellipse id="ellipse" cx="0" cy="0" rx="1" ry="1" style="transform: matrix(50, 0, 0, 100, 0, 0)"></ellipse> -->

      <!-- 有斜切 -->
      <!-- <ellipse id="ellipse" cx="0" cy="0" rx="1" ry="1" style="transform: matrix(43.3, 25, -24.02, 101.6, 200, 200)"></ellipse> -->

      <polyline id="bounding-box" points="" fill="none" stroke="red" stroke-dasharray="5"></polyline>
    </svg>

    <script>
      if (typeof SVGElement.prototype.getTransformToElement === "undefined") {
        SVGElement.prototype.getTransformToElement = function (toElement) {
          return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());
        };
      }

      function $$(id) {
        return document.getElementById(id);
      }

      let container = $$("container");
      let ellipse = $$("ellipse");

      function main() {
        updateBoundingBox();
        addEvent();
      }

      main();

      function updateBoundingBox() {
        let points = transformedBoundingBox(ellipse);
        createBoundingBox(points);
        createCircle(points);
      }

      function scaleElli(xScale, yScale) {
        let m = container.createSVGMatrix();
        let m2 = m.scale(xScale, yScale);
        m2.a = xScale;
        m2.d = yScale;
        return m2;
      }

      function translateElli(x, y) {
        let m = container.createSVGMatrix();
        return m.translate(x, y);
      }

      function rotateElli(degree) {
        let m = container.createSVGMatrix();
        return m.rotate(degree);
      }

      function updateTransform(el, m2) {
        el.style.transform = `matrix(${m2.a},${m2.b},${m2.c},${m2.d},${m2.e},${m2.f})`;
      }

      function addEvent() {
        let isDragDot = false;
        let isDragEll = false;
        let startP = { x: 0, y: 0 };
        let dragDotIndex;
        let dragDot;
        let dotsMap = {
          0: 2,
          1: 3,
          2: 0,
          3: 1,
        };
        let ellOldMatrix;
        let target;
        let old4points;

        function mousedownHandler(e) {
          let id = e.target.id || "";
          target = e.target;

          if (id.includes("circle")) isDragDot = true;

          ellOldMatrix = getElementMatrix(ellipse);

          if (id === "ellipse") {
            isDragEll = true;
          }

          startP.x = e.clientX || e.touches[0].clientX;
          startP.y = e.clientY || e.touches[0].clientY;

          if (isDragDot) {
            dragDot = e.target;
            dragDotIndex = id.split("-")[1];

            dragDot.setAttribute("old-cx", dragDot.getAttribute("cx"));
            dragDot.setAttribute("old-cy", dragDot.getAttribute("cy"));

            old4points = transformedBoundingBox($$("ellipse"));
          }
        }
        document.addEventListener("mousedown", mousedownHandler);
        document.addEventListener("touchstart", mousedownHandler);

        function mousemoveHandler(e) {
          e.preventDefault();
          
          let dx = (e.clientX || e.touches[0].clientX) - startP.x;
          let dy = (e.clientY || e.touches[0].clientY) - startP.y;

          if (isDragDot) {
            let cx = +dragDot.getAttribute("old-cx") + dx;
            let cy = +dragDot.getAttribute("old-cy") + dy;

            let oldP = old4points[dragDotIndex];
            let originDot = old4points[dotsMap[dragDotIndex]];

            let rotation = 30;

            let m1 = translateElli(-originDot.x, -originDot.y);
            let m2 = rotateElli(-rotation);
            let m3 = m1.multiply(m2);

            let newOldP = oldP.matrixTransform(m3);
            let newOriginDot = originDot.matrixTransform(m3);

            let oldWidth = newOldP.x - newOriginDot.x;
            let oldHeight = newOldP.y - newOriginDot.y;

            let p = container.createSVGPoint();
            p.x = cx;
            p.y = cy;
            let newP = p.matrixTransform(m3);

            let newWidth = newP.x - newOriginDot.x;
            let newHeight = newP.y - newOriginDot.y;

            console.log(oldWidth, oldHeight, "-", newWidth, newHeight);

            let scaleX = newWidth / oldWidth;
            let scaleY = newHeight / oldHeight;

            let translateBackMatrix = translateElli(originDot.x, originDot.y);
            let scaleMatrix = scaleElli(scaleX, scaleY);
            let rotateBackMatrix = rotateElli(rotation);

            let m4 = m1.multiply(ellOldMatrix);
            let m8 = m2.multiply(m4);
            let m5 = scaleMatrix.multiply(m8);
            let m7 = rotateBackMatrix.multiply(m5);
            let m6 = translateBackMatrix.multiply(m7);

            let newMatrix = m6;

            updateTransform(ellipse, newMatrix);

            updateBoundingBox();
          }

          if (isDragEll) {
            let m1 = translateElli(dx, dy);
            let m2 = m1.multiply(ellOldMatrix);

            updateTransform(target, m2);

            updateBoundingBox();
          }
        }
        document.addEventListener("mousemove", mousemoveHandler);
        document.addEventListener("touchmove", mousemoveHandler);

        function mouseupHandler() {
          if (isDragDot) {
            isDragDot = false;
          }

          if (isDragEll) {
            isDragEll = false;
          }
        }
        document.addEventListener("mouseup", mouseupHandler);
        document.addEventListener("touchend", mouseupHandler);
      }

      function createCircle(points) {
        let namespace = container.getAttribute("xmlns");

        let index = 0;
        for (const p of points) {
          let id = "circle-" + index++;

          let exist = $$(id);
          let c = exist || document.createElementNS(namespace, "circle");

          c.setAttribute("id", id);
          c.setAttribute("cx", p.x);
          c.setAttribute("cy", p.y);
          c.setAttribute("r", 5);

          !exist && container.appendChild(c);
        }
      }

      function createBoundingBox(points) {
        let box = $$("bounding-box");
        let arr = [];

        for (const p of points) {
          arr.push(p.x, p.y);
        }

        box.setAttribute("points", arr.concat(arr[0], arr[1]).join(" "));
      }

      function getElementMatrix(el) {
        let bb = el.getBBox(),
          svg = el.ownerSVGElement,
          m = el.getTransformToElement(el.parentNode);

        return m;
      }

      // Calculate the bounding box of an element with respect to its parent element
      function transformedBoundingBox(el) {
        var bb = el.getBBox(),
          svg = el.ownerSVGElement,
          m = el.getTransformToElement(el.parentNode);

        let scale = scaleElli(2, 2);
        let translate = translateElli(20, 0);
        let m2 = translate.multiply(m);

        // el.style.transform = `matrix(${m2.a},${m2.b},${m2.c},${m2.d},${m2.e},${m2.f})`;

        // Create an array of all four points for the original bounding box
        var pts = [svg.createSVGPoint(), svg.createSVGPoint(), svg.createSVGPoint(), svg.createSVGPoint()];
        pts[0].x = bb.x;
        pts[0].y = bb.y;
        pts[1].x = bb.x + bb.width;
        pts[1].y = bb.y;
        pts[2].x = bb.x + bb.width;
        pts[2].y = bb.y + bb.height;
        pts[3].x = bb.x;
        pts[3].y = bb.y + bb.height;

        // Transform each into the space of the parent,
        // and calculate the min/max points from that.
        var xMin = Infinity,
          xMax = -Infinity,
          yMin = Infinity,
          yMax = -Infinity;

        for (let pt of pts) {
          let temp = pt.matrixTransform(m);

          pt.x = temp.x;
          pt.y = temp.y;

          xMin = Math.min(xMin, pt.x);
          xMax = Math.max(xMax, pt.x);
          yMin = Math.min(yMin, pt.y);
          yMax = Math.max(yMax, pt.y);
        }

        // Update the bounding box with the new values
        bb.x = xMin;
        bb.width = xMax - xMin;
        bb.y = yMin;
        bb.height = yMax - yMin;

        return pts;
      }
    </script>
 
              
            
!

CSS

              
                svg {
  border: 1px solid red;
}
circle,
ellipse {
  cursor: move;
}
              
            
!

JS

              
                
              
            
!
999px

Console