Open In App

D3.js image() Function

The d3.image() function in D3.js is a part of the request API that is used to fetch the images from any given image URL. If init is also given in the function then it sets any additional properties on the image before loading the image.  

Syntax:



d3.image(input[, init]);

Parameters: This function accepts a single parameter as mentioned above and described below:

Return Values: It returns the image from the image URL.



Below examples illustrate the D3.js image() Function in JavaScript.

Example1:




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            path1tent="width=device-width, 
                       initial-scale=1.0"/>
        <title>D3.js image() Function</title>
    </head>
    <style></style>
    <body>
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
  
        <script>
            d3.image("https://robohash.org/image", 
                     { crossOrigin: "anonymous" }).then((img) => {
                console.log(img);
            });
        </script>
    </body>
</html>

Output:

Example 2:




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            path1tent="width=device-width, 
                       initial-scale=1.0"/>
        <title>D3.js image() Function</title>
    </head>
    <style></style>
    <body>
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
  
        <script>
            d3.image(
              { crossOrigin: "anonymous" }).then((img) => {
                document.body.append("Image using d3.image()");
                document.body.append(img);
            });
        </script>
    </body>
</html>

Output:


Article Tags :