A hint about scrolling the map

Open in CodeSandbox

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {LOCATION} from '../variables';
      import {createTimeout} from './common';

      window.map = null;

      main();
      async function main() {
        // Waiting for all api elements to be loaded
        await mappable.ready;
        const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer} = mappable;

        const timeout = createTimeout();

        // Initialize the map
        map = new MMap(
          // Pass the link to the HTMLElement of the container
          document.getElementById('app'),
          // Pass the map initialization parameters
          {location: LOCATION, showScaleInCopyrights: true},
          [
            // Add a map scheme layer
            new MMapDefaultSchemeLayer({}),
            // Add a layer of geo objects to display the line
            new MMapDefaultFeaturesLayer({})
          ]
        );

        const scrollEvent = (event: WheelEvent) => {
          event.preventDefault();
          const hintElement = document.getElementById('hint');
          if (event.ctrlKey || event.metaKey) {
            hintElement.classList.add('hidden');
          } else {
            hintElement.classList.remove('hidden');
            event.stopPropagation();
            timeout(() => {
              hintElement.classList.add('hidden');
            }, 500);
          }
        };

        const container = document.getElementById('container');
        container.addEventListener('wheel', scrollEvent, {passive: false, capture: true});
      }
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="container" class="container">
      <div id="hint" class="hint hidden">
        <div class="text">Use<span class="command">&#8984;/Ctrl + scroll</span>to zoom</div>
      </div>
      <div id="app"></div>
    </div>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
      import {LOCATION} from '../variables';
      import {createTimeout} from './common';

      window.map = null;

      main();
      async function main() {
        // For each object in the JS API, there is a React counterpart
        // To use the React version of the API, include the module @mappable-world/mappable-reactify
        const [mappableReact] = await Promise.all([
          mappable.import('@mappable-world/mappable-reactify'),
          mappable.ready
        ]);
        const reactify = mappableReact.reactify.bindTo(React, ReactDOM);
        const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapListener} = reactify.module(mappable);
        const {useState, useEffect, useCallback, useMemo} = React;
        const timeout = createTimeout();

        function App() {
          const [show, setShow] = useState(false);

          const scrollEvent = useCallback((event: WheelEvent) => {
            event.preventDefault();
            if (event.ctrlKey || event.metaKey) {
              setShow(false);
            } else {
              setShow(true);
              event.stopPropagation();
              timeout(() => {
                setShow(false);
              }, 500);
            }
          }, []);

          useEffect(() => {
            const container = document.getElementById('container');
            container.addEventListener('wheel', scrollEvent, {passive: false, capture: true});
            return () => container.removeEventListener('wheel', scrollEvent);
          }, []);

          return (
            <div id="container" className="container">
              <div className={`hint ${show ? '' : 'hidden'}`}>
                <div className="text">
                  Use<span className="command">&#8984;/Ctrl + scroll</span>to zoom
                </div>
              </div>
              {/* Initialize the map and pass initialization parameters*/}
              <MMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
                {/* Add a map scheme layer */}
                <MMapDefaultSchemeLayer />
                {/* Add a layer of geo objects to display the line */}
                <MMapDefaultFeaturesLayer />

                <MMapListener />
              </MMap>
            </div>
          );
        }

        ReactDOM.render(
          <React.StrictMode>
            <App />
          </React.StrictMode>,
          document.getElementById('app')
        );
      }
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {LOCATION} from '../variables';
      import {createTimeout} from './common';

      window.map = null;

      async function main() {
        const [mappableVue] = await Promise.all([mappable.import('@mappable-world/mappable-vuefy'), mappable.ready]);
        const vuefy = mappableVue.vuefy.bindTo(Vue);
        const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer} = vuefy.module(mappable);
        const timeout = createTimeout();

        const App = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapDefaultFeaturesLayer
          },
          setup() {
            const refMap = (ref) => {
              window.map = ref?.entity;
            };

            const show = Vue.ref(false);

            const scrollEvent = (event: WheelEvent) => {
              event.preventDefault();
              if (event.ctrlKey || event.metaKey) {
                show.value = false;
              } else {
                show.value = true;
                event.stopPropagation();
                timeout(() => {
                  show.value = false;
                }, 500);
              }
            };

            Vue.onUnmounted(() => {
              const container = document.getElementById('container');
              container.removeEventListener('wheel', scrollEvent);
            });

            Vue.onMounted(() => {
              const container = document.getElementById('container');
              container.addEventListener('wheel', scrollEvent, {passive: false, capture: true});
            });

            return {
              refMap,
              LOCATION,
              show
            };
          },
          template: `
      <div id="container" class="container">
        <div :class="[show ? '' : 'hidden', 'hint']">
          <div class="text">
            Use<span class="command">&#8984;/Ctrl + scroll</span>to zoom
          </div>
        </div>
        <MMap
          :location="LOCATION"
          :ref="refMap"
          :showScaleInCopyrights="true"
        >
          <MMapDefaultSchemeLayer />
          <MMapDefaultFeaturesLayer />
        </MMap>
      </div>
    `
        });

        App.mount('#app');
      }

      main();
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
.container {
  width: 100%;
  height: 100%;
}

.hint {
  width: 100%;
  display: flex;
  height: 100%;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  background-color: var(--color-bg-overlay);

  user-select: none;
  z-index: 10;
  visibility: visible;
  transition: all 0.4s ease-in-out;
  opacity: 1;
}

.hint.hidden {
  visibility: hidden;
  opacity: 0;
}

.text {
  display: flex;
  color: #f2f5fa;
  font-size: 16px;
  gap: 4px;
  align-items: center;
  font-weight: 500;
  line-height: 21px;
}

.command {
  height: 20px;
  border-radius: 4px;
  padding: 2px 6px;
  background-color: #050d3366;
  vertical-align: center;
}
export function createTimeout() {
  let timeoutId = null;

  return function (callback: () => void, delay: number) {
    if (timeoutId !== null) {
      clearTimeout(timeoutId);
    }

    timeoutId = setTimeout(() => {
      callback();
      timeoutId = null;
    }, delay);
  };
}
:root {
  --color-bg-overlay: rgba(5, 13, 51, 0.4);
}
import type {MMapLocationRequest} from '@mappable-world/mappable-types';

export const LOCATION: MMapLocationRequest = {
  center: [55.2811, 25.2239], // starting position [lng, lat]
  zoom: 14.2 // starting zoom
};