Menu
Close

Canvas Source

Sample Implementation

    
      
      <html>
        <head>
          <title>Canvas Source</title>
          <meta name="viewport" content="initial-scale=1.0">
          <meta charset="utf-8">
          <style>
          html,
          body,
          #map {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100vh;
          }
          </style>
             <script src="https://apis.mappls.com/advancedmaps/api/<Token>/map_sdk?layer=vector&v=3.0&callback=initMap1" defer async></script>
            </head>
        <body>
                    <div id="map"></div>
            <canvas id="canvasID" width="400" height="400">Canvas not supported</canvas>
                     <script>
      const canvas = document.getElementById("canvasID");
      const ctx = canvas.getContext("2d");
      const circles = [];
      const radius = 20;
      if (ctx) {
        canvas.style.display = "none";
      }

      function Circle(x, y, dx, dy, radius, color) {
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;

        this.radius = radius;

        this.draw = function () {
          ctx.beginPath();
          ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
          ctx.strokeStyle = color;
          ctx.stroke();
        };

        this.update = function () {
          if (this.x + this.radius > 400 || this.x - this.radius < 0) {
            this.dx = -this.dx;
          }

          if (this.y + this.radius > 400 || this.y - this.radius < 0) {
            this.dy = -this.dy;
          }

          this.x += this.dx;
          this.y += this.dy;

          this.draw();
        };
      }

      for (let i = 0; i < 5; i++) {
        const color =
          "#" +
          (0x1000000 + Math.random() * 0xffffff).toString(16).substr(1, 8);
        const x = Math.random() * (400 - radius * 2) + radius;
        const y = Math.random() * (400 - radius * 2) + radius;

        const dx = (Math.random() - 0.5) * 2;
        const dy = (Math.random() - 0.5) * 2;

        circles.push(new Circle(x, y, dx, dy, radius, color));
      }

      function animate() {
        requestAnimationFrame(animate);
        ctx.clearRect(0, 0, 400, 400);

        for (let r = 0; r < 5; r++) {
          circles[r].update();
        }
      }

      animate();

      function initMap1() {
        map = new mappls.Map("map", {
          center: [23.83, 78.74],
          minZoom: 4,
          zoom: 5,
        });

        map.on("load", () => {
          map.addSource("canvas-source", {
            type: "canvas",
            canvas: "canvasID",
            coordinates: [
              [79.6832293345214, 26.531130256948174],
              [71.84406042026126, 26.531130256948174],
              [71.84406042026126, 20.807123725237517],
              [79.6832293345214, 20.807123725237517],
            ],
            animate: true,
          });

          map.addLayer({
            id: "canvas-layer",
            type: "raster",
            source: "canvas-source",
          });
        });
      }
  </script>
        </body>
      </html>
      
      
      
    
  
Call at Mappls
Request Call Back

Personalisation SDK, by Mappls, is India's first O2O engagement tool that can 3X your customer engagement, retention and conversion.

There's so much that Mappls MapmyIndia can do for your enterprise or individual requirements. Explore our website to learn more or request a callback/email if you'd like us to connect with you.

or