Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                <html>

<body>
  <div id="app">
    <svg ref="container" id="container" width="300" height="300">
      <g id="coordinate">
        <line v-for="line in lines" :x1="line.x1" :y1="line.y1" :x2="line.x2" :y2="line.y2" style="stroke: black; " />
      </g>
      <circle ref="point1" class="point" r="5" stroke-width="3" fill="orange" />
      <circle ref="point2" class="point" r="5" stroke-width="3" fill="orange" />
      <line
        ref="line"
        id="line"
        :x1="x1"
        :y1="y1"
        :x2="x2"
        :y2="y2"
        style="stroke: red; "
      />
    </svg>
  </div>
</body>

</html>
              
            
!

CSS

              
                .point {
      position: absolute;
      cursor: none
    }
              
            
!

JS

              
                console.clear();

const lineDistance = 20
  vm = new Vue({
    el: "#app",
    data: {
      count: 11,
      point1: null,
      point2: null,
      x1: 0,
      y1: 0,
      x2: 100,
      y2: 100
    },
    computed: {
      lines() {
        const lines = []
        const [xLeft, yTop, xRight, yBottom] = [0, 0, 200, 200]
        for (i = 0; i < this.count; i++) {
          let line = {}
          line.x1 = xLeft
          line.x2 = xRight
          line.y1 = line.y2 = yTop + i * lineDistance
          lines.push(line)
        }
        for (i = 0; i < this.count; i++) {
          let line = {}
          line.x1 = line.x2 = xLeft + i * lineDistance
          line.y1 = yTop
          line.y2 = yBottom
          lines.push(line)
        }
        return lines
      }
    },
    mounted() {
      this.point1 = this.createDraggable(this.$refs.point1, 100, 100);
      this.point2 = this.createDraggable(this.$refs.point2, 160, 100);
      this.updateLine();
    },
    methods: {
      createDraggable(target, x, y) {
        
        gsap.set(target, { x, y });
        
        return new Draggable(target, {
          onDrag: this.updateLine,
          onDragEnd: this.updateLine,
          bounds: this.$refs.container,
          liveSnap: {
            x: function (value) {
              //snap to the closest increment of 50.
              return Math.round(value / 20) * 20;
            },
            y: function (value) {
              //snap to the closest increment of 25.
              return Math.round(value / 20) * 20;
            }
          }
        });
      },
      updateLine() {
        this.x1 = this.point1.x;
        this.y1 = this.point1.y;
        this.x2 = this.point2.x;
        this.y2 = this.point2.y;        
      }
    }
  });
//   $container = document.querySelector("#coordinate")
//   var $points = document.querySelectorAll('.point')
//   var $line = document.querySelector('#line')
  
//   function getLineXY(x, y) {
//     if (y < 0) {
//       yResult = 0;
//       xResult = (p1X - p0X) / (p1Y - p0Y) * (0 - p0X) + p0X;
//     } else if (y > 200) {
//       yResult = 200;
//       xResult = (p1X - p0X) / (p1Y - p0Y) * (200 - p0X) + p0X;
//     } else {
//       xResult = x;
//       yResult = y;
//     }
//     return [xResult, yResult]
//   }
  
//   function drawLine() {
//     console.log('drawLine...')
//     this.target.cx.baseVal.value += this.endX
//     this.target.cy.baseVal.value += this.endY
    
//     let p0X = $points[0].cx.baseVal.value
//     let p0Y = $points[0].cy.baseVal.value
//     let p1X = $points[1].cx.baseVal.value
//     let p1Y = $points[1].cy.baseVal.value

//     let yRight = (200 - p0Y) * (p1Y - p0Y) / (p1X - p0X)  + p0Y;
//     [$line.x1.baseVal.value, $line.y1.baseVal.value] = getLineXY(200, yRight);
//     let yLeft = (0 - p0Y) * (p1Y - p0Y) / (p1X - p0X) + p0Y;
//     [$line.x2.baseVal.value, $line.y2.baseVal.value] = getLineXY(0, yLeft);

//   }
  
//   Draggable.create('.point', {
//     onDragEnd: drawLine,
//     bounds: $container,
//     edgeResistance: 0.65,
//     type: "x, y",
//     liveSnap: {
//       x: function (value) {
//         //snap to the closest increment of 50.
//         return Math.round(value / 20) * 20;
//       },
//       y: function (value) {
//         //snap to the closest increment of 25.
//         return Math.round(value / 20) * 20;
//       }
//     }
//   });
              
            
!
999px

Console