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

              
                <head>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<html>
<body>
  <div id="app">
    <svg ref="container" id="container" width="360" height="270" viewBox="0 0 400 315" preserveAspectRatio="xMinYMin align">
      <g id="coordinate">
        <line v-for="line in lines" :x1="line.x1" :y1="line.y1" :x2="line.x2" :y2="line.y2" style="stroke: black; " />
        <text v-for="axisXlabel in axisXlabels" :x="axisXlabel.x" :y="axisXlabel.y">{{axisXlabel.text}}</text>
        <text v-for="axisYlabel in axisYlabels" :x="axisYlabel.x" :y="axisYlabel.y">{{axisYlabel.text}}</text>
      </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>
    <h4 ref='displaySlope'>
    </h4>
  </div>
  
</body>
</html>
              
            
!

CSS

              
                #app {
  display: flex;
  flex-direction: column;
}
svg {
  display: block;
/*   width="400";
  height="300"; */
/*   border: 1px solid #f00; */
}
h4 {
  margin: 10px;
}
.text {
}
.point {
/*       position: absolute; */
      cursor: none
    }
              
            
!

JS

              
                console.clear();

const GRID_SIZE_X = 15;
const GRID_SIZE_Y = 10;
const GRID_WIDTH = 25;
const WIDTH = GRID_SIZE_X * GRID_WIDTH; 
const HEIGHT = GRID_SIZE_Y * GRID_WIDTH;
function getGcd(number1, number2) { 
  if (number2 == 0) {
    return number1; 
  } else {
    return getGcd(number2, number1 % number2); 
  }
}
  vm = new Vue({
    el: "#app",
    data: {
      point1: null,
      point2: null,
      x1: null,
      y1: null,
      x2: null,
      y2: null,
    },
    computed: {
      lines() {
        const lines = []
        const [xLeft, yTop, xRight, yBottom] = [0, 0, WIDTH, HEIGHT]
        for (i = 0; i < GRID_SIZE_Y + 1; i++) {
          let line = {}
          line.x1 = xLeft
          line.x2 = xRight
          line.y1 = line.y2 = yTop + i * GRID_WIDTH
          lines.push(line)
        }
        for (i = 0; i < GRID_SIZE_X + 1; i++) {
          let line = {}
          line.x1 = line.x2 = xLeft + i * GRID_WIDTH
          line.y1 = yTop
          line.y2 = yBottom
          lines.push(line)
        }
        return lines
      },
      axisXlabels() {
        const axisLabels = []
        // const [xLeft, yTop, xRight, yBottom] = [0, 0, WIDTH, HEIGHT]
        for (i = 1; i < GRID_SIZE_X + 1; i++) {
          let axisLabel = {}
          axisLabel.x = i * GRID_WIDTH - 10
          axisLabel.y = HEIGHT + 15 
          axisLabel.text = i.toString()    
          axisLabels.push(axisLabel)
        }
        return axisLabels
      },
      axisYlabels() {
        const axisLabels = []
        // const [xLeft, yTop, xRight, yBottom] = [0, 0, WIDTH, HEIGHT]
        for (i = 1; i < GRID_SIZE_Y + 1; i++) {
          let axisLabel = {}
          axisLabel.x = WIDTH + 10
          axisLabel.y = HEIGHT - (i - 1) * GRID_WIDTH
          axisLabel.text = i.toString()    
          axisLabels.push(axisLabel)
        }
        return axisLabels
      },
      deltaX() {
        return this.x2 - this.x1
      },
      deltaY() {
        return this.y1 - this.y2
      },
      slope() {
       return Math.round(this.deltaY / this.deltaX * 100) / 100
      }
    },
    mounted() {
      this.point1 = this.createDraggable(this.$refs.point1, 5 * GRID_WIDTH, 5 * GRID_WIDTH);
      this.point2 = this.createDraggable(this.$refs.point2, 8 * GRID_WIDTH, 5 * GRID_WIDTH);
      this.updateLine();
    },
    methods: {
      createDraggable(target, x, y) {
        
        gsap.set(target, { x, y });
        
        return new Draggable(target, {
          onDrag: this.updateLine,
          onDragEnd: this.updateLine,
          bounds: {
            maxX: WIDTH,
            maxY: HEIGHT
          },
          liveSnap: {
            x: function (value) {
              //snap to the closest increment of 50.
              return Math.round(value / GRID_WIDTH) * GRID_WIDTH;
            },
            y: function (value) {
              //snap to the closest increment of 25.
              return Math.round(value / GRID_WIDTH) * GRID_WIDTH;
            }
          }
        });
      },
      updateLine() {
        const x1 = this.point1.x;
        const y1 = this.point1.y;
        const x2 = this.point2.x;
        const y2 = this.point2.y;
        
        let y_left = this.intercept(0, x1, y1, x2, y2);
        [this.x1, this.y1] = this.getEndPointOfLine(0, y_left, x1, y1, x2, y2)
        
        let y_right = this.intercept(WIDTH , x1, y1, x2, y2);
        [this.x2, this.y2] = this.getEndPointOfLine(WIDTH, y_right, x1, y1, x2, y2)
        delta_x = this.x2 - this.x1
        delta_y = this.y1 - this.y2
        gcd = getGcd(delta_x, delta_y)
        delta_x /= gcd
        delta_y /= gcd
					// MathJax.Hub.Queue(
					//   ["Delay", MathJax.Callback, 0],
					//   ["Typeset", MathJax.Hub, "displaySlope"]
					// );
        this.$nextTick(function() {
						 MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
					 });
        this.$refs.displaySlope.innerHTML = `The slope of the line is: \\(\\Large \\frac{ ${delta_y} }  {               ${delta_x} } \\large \\simeq ${this.slope}\\)`
        
      },
      intercept(c, a1, b1, a2, b2) {
        return (c - a1) * (b2 - b1) / (a2 - a1)  + b1;
      },
      
      getEndPointOfLine(x, y, x1, y1, x2, y2) {
        var xEnd, yEnd
        if (y < 0) {
          yEnd = 0
          xEnd = Math.round(this.intercept(0, y1, x1, y2, x2))
        } else if (y > HEIGHT) {
          yEnd = HEIGHT
          xEnd = Math.round(this.intercept(HEIGHT, y1, x1, y2, x2))
        } else {
          xEnd = x
          yEnd = Math.round(y)
        }
        return [xEnd, yEnd]
      }
    }
  });

              
            
!
999px

Console