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

              
                <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <title>Vary point sizes by scale - 4.14</title>

    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.14/esri/themes/light/main.css"
    />
    <script src="https://js.arcgis.com/4.14/"></script>

  </head>

  <body>
    <div id="viewDiv"></div>
    <div id="infoDiv" class="esri-widget">
      <button id="toggle-auto-size" class="esri-button">
        Enable auto size by scale
      </button>
    </div>
    <div id="scaleInfo">
      <div id="scaleDiv">View scale: <span id="viewScale"></span></div>
      <div id="sizeDiv">Line width (pt): <span id="size"></span></div>
      <div id="codeDiv"></div>
    </div>
  </body>
</html>

              
            
!

CSS

              
                html,
body,
#viewDiv {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
#infoDiv {
  background: white;
  padding: 10px;
}
#codeDiv {
  color: rgb(148, 4, 40);
  overflow-y: auto;
  /* overflow: scroll; */
  max-height: 250px;
}
#scaleInfo {
  background: white;
  padding: 10px;
  font-family: monospace;
}
              
            
!

JS

              
                require([
  "esri/Map",
  "esri/views/MapView",
  "esri/layers/FeatureLayer",
  "esri/renderers/SimpleRenderer",
  "esri/renderers/smartMapping/creators/location",
  "esri/renderers/visualVariables/SizeVariable",
  "esri/widgets/Expand",
  "esri/widgets/Home"
], function(
  Map,
  MapView,
  FeatureLayer,
  SimpleRenderer,
  locationRendererCreator,
  SizeVariable,
  Expand,
  Home
) {

  const renderer = new SimpleRenderer({
    symbol: {
      type: "simple-line",
      color: "dodgerblue",
      width: 3
    }
  });

  const layer = new FeatureLayer({
    portalItem: {
      id: "5002e07683a642d4b5ca55a7e142fd79"
    },
    renderer: renderer
  });

  const map = new Map({
    basemap: {
      portalItem: {
        id: "3582b744bba84668b52a16b0b6942544"
      }
    },
    layers: [layer]
  });

  const view = new MapView({
    container: "viewDiv",
    map: map,
    extent: {
      "spatialReference": {
        "latestWkid": 3857,
        "wkid": 102100
      },
      "xmin": -9870993.829756167,
      "ymin": 2785669.5979774236,
      "xmax": -8797206.456406295,
      "ymax": 3761617.5751222945
    }
  });
  
  view.when(toggleAutoSize);

  view.ui.add(new Home({ view: view }), "top-left");

  const scaleInfo = document.getElementById("scaleInfo");
  const infoExpand = new Expand({
    content: scaleInfo,
    view: view
  });

  view.ui.add(infoExpand, "top-left");

  const infoDiv = document.getElementById("infoDiv");
  view.ui.add(infoDiv, "top-right");

  const toggleButton = document.getElementById("toggle-auto-size");
  toggleButton.addEventListener("click", toggleAutoSize);

  const codeElement = document.getElementById("codeDiv");

  const viewScaleElement = document.getElementById("viewScale");
  const sizeElement = document.getElementById("size");

  let scaleWatchHandle;

  function toggleAutoSize() {
    let sizeOptimizationEnabled = false;

    if (toggleButton.innerText === "Enable auto size by scale") {
      toggleButton.innerText = "Disable auto size by scale";
      sizeOptimizationEnabled = true;
    } else {
      toggleButton.innerText = "Enable auto size by scale";
      clearInfoDisplay();
      layer.renderer = renderer;
      return;
    }

    // generates a new renderer with a scale-dependent
    // size visual variable to vary icon size by scale
    locationRendererCreator
      .createRenderer({
        layer: layer,
        view: view,
        outlineOptimizationEnabled: layer.geometryType === "polygon" && sizeOptimizationEnabled,
        sizeOptimizationEnabled: layer.geometryType !== "polygon" && sizeOptimizationEnabled
      })
      .then(function(rendererResponse) {
        // the renderer contains a size variable with stops
        // mapping icon sizes to scale values
        const renderer = rendererResponse.renderer;
        renderer.symbol.color = "dodgerblue";

        layer.renderer = renderer;

        if(renderer.visualVariables){
          const scaleVVs = getScaleVariables(renderer.visualVariables);
          const scaleVVsStringified = JSON.stringify(scaleVVs, null, 2);

          codeElement.innerHTML = `<pre>${scaleVVsStringified}</pre>`;

          displayScaleAndSizeValues(view.scale);

          scaleWatchHandle = view.watch("scale", displayScaleAndSizeValues);

          function displayScaleAndSizeValues(scale){
            viewScaleElement.innerHTML = Math.round(scale);
            const effectiveSize = getSizeFromScale(scaleVVs[0], scale);
            sizeElement.innerHTML = effectiveSize.toPrecision(2);
          }
        }
      })
      .catch(function(error) {
        console.error(error);
      });
  }

  function getScaleVariables(visualVariables){
    return visualVariables.filter(function(vv){
      return vv.valueExpression && vv.valueExpression === "$view.scale";
    });
  }

  function clearInfoDisplay(){
    sizeElement.innerHTML = null;
    viewScaleElement.innerHTML = null;
    codeElement.innerHTML = null;
    scaleWatchHandle.remove();
    scaleWatchHandle = null;
    infoExpand.expanded = false;
  }

  function getSizeFromScale(sizeVV, scale){
    const sizeStops = sizeVV.stops;
    const stopsCount = sizeStops.length;
    const finalIndex = stopsCount-1;
    const scaleMin = sizeStops[0].value;
    const scaleMax = sizeStops[finalIndex].value;

    if(scale <= scaleMin){
      return sizeStops[0].size;
    }

    if(scale >= scaleMax){
      return sizeStops[finalIndex].size;
    }

    for (let i = 0; i <= finalIndex; i++){
      const currentStop = sizeStops[i];
      const nextStop = sizeStops[i+1];

      if(scale >= currentStop.value && scale < nextStop.value){
        const scaleRange = nextStop.value - currentStop.value;
        const sizeRange  = currentStop.size - nextStop.size;
        const relativePosition = (scale - currentStop.value) / scaleRange;

        const effectiveSize = currentStop.size - (sizeRange * relativePosition);
        return effectiveSize;
      }

    }

  }
});
              
            
!
999px

Console