Pen Settings



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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.



  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  ArcGIS API for JavaScript,
  For more information about the draw-line sample, read the original sample description at
<title>Draw polyline - 4.10</title>

  <link rel="stylesheet" href="">
  <script src=""></script>

    #viewDiv {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;

    ], function(
      Map, MapView, Draw, Graphic, geometryEngine, GraphicsLayer
    ) {
      const map = new Map(/*{
        basemap: "gray"

      const view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 16,
        center: [18.06, 59.34]

      // add the button for the draw tool
      view.ui.add("line-button", "top-left");

      const draw = new Draw({
        view: view

      const gLayer = new GraphicsLayer();
      // draw polyline button
      document.getElementById("line-button").onclick = function() {;

        // creates and returns an instance of PolyLineDrawAction
        const action = draw.create("polyline");

        // focus the view to activate keyboard shortcuts for sketching

        // listen polylineDrawAction events to give immediate visual feedback
        // to users as the line is being drawn on the view.
        action.on(["vertex-add", "vertex-remove", "cursor-update",/* "redo",
          "undo",*/ "draw-complete"
        ], updateVertices);

      // Checks if the last vertex is making the line intersect itself.
      function updateVertices(event) {
        // create a polyline from returned vertices
        const result = createGraphic(event);

        // if the last vertex is making the line intersects itself,
        // prevent the events from firing
        if (result.selfIntersects) {

      // create a new graphic presenting the polyline that is being drawn on the view
      function createGraphic(event) {
        const vertices = event.vertices;
        // a graphic representing the polyline that is being drawn
        const graphic = new Graphic({
          geometry: {
            type: "polyline",
            paths: vertices,
            spatialReference: view.spatialReference
          symbol: {
            type: "simple-line", // autocasts as new SimpleFillSymbol
            color: [4, 90, 141],
            width: 4,
            cap: "round",
            join: "round"

        // check if the polyline intersects itself.
        const intersectingSegment = getIntersectingSegment(graphic.geometry);

        // Add a new graphic for the intersecting segment.
        if (intersectingSegment) {
          //[graphic, intersectingSegment]);
          gLayer.addMany([graphic, intersectingSegment]);
        // Just add the graphic representing the polyline if no intersection
        else {

        // return intersectingSegment
        return {
          selfIntersects: intersectingSegment

      // function that checks if the line intersects itself
      function isSelfIntersecting(polyline) {
        if (polyline.paths[0].length < 3) {
          return false
        const line = polyline.clone();

        //get the last segment from the polyline that is being drawn
        const lastSegment = getLastSegment(polyline);
        line.removePoint(0, line.paths[0].length - 1);

        // returns true if the line intersects itself, false otherwise
        return geometryEngine.crosses(lastSegment, line);

      // Checks if the line intersects itself. If yes, change the last
      // segment's symbol giving a visual feedback to the user.
      function getIntersectingSegment(polyline) {
        if (isSelfIntersecting(polyline)) {
          return new Graphic({
            geometry: getLastSegment(polyline),
            symbol: {
              type: "simple-line", // autocasts as new SimpleLineSymbol
              style: "short-dot",
              width: 3.5,
              color: "yellow"
        return null;

      // Get the last segment of the polyline that is being drawn
      function getLastSegment(polyline) {
        const line = polyline.clone();
        const lastXYPoint = line.removePoint(0, line.paths[0].length - 1);
        const existingLineFinalPoint = line.getPoint(0, line.paths[0].length -

        return {
          type: "polyline",
          spatialReference: view.spatialReference,
          hasZ: false,
          paths: [
              [existingLineFinalPoint.x, existingLineFinalPoint.y],
              [lastXYPoint.x, lastXYPoint.y]

  <div id="viewDiv">
    <div id="line-button" class="esri-widget esri-widget--button esri-interactive"
      title="Draw polyline">
      <span class="esri-icon-polyline"></span>