Integration involves 3 very simple steps and if you are familiar with ES6 Modules then you probably know it already. Let's take a look at the procedure:
-
Download the starline.min.js file from the GitHub repository and save it anywhere in your project directory.
-
We need to add a Javascript file as an ES6 Module in the <head> of our page. We need this script to initiate Starline.js on our canvas elements. Let's call it initiator.js. We'll create this file later.
<script type="module" src="/path/to/initiator.js" defer ></script>
Now, place a <canvas> element wrapped in a <div> with position: relative style applied in it. Create a data attribute with the name data-starline-srcset in the canvas and type in the path of the images you want to display separated by comma as its value like below:
<div style="position: relative;" >
<canvas data-starline-srcset="https://eg.site/external/image.jpg, /local/image.webp, /like/so/on.png" ></canvas>
</div>
You can also place the canvas items freely - without wrapping them in a relatively positioned element anywhere but placing them like as we mentioned earlier is recommended for it enables certain features!
-
Remember that in the last step, we talked about the initiator.js file? Let's create it and import our class from the downloaded file with it as depicted below. This line must reside at the very top of the script.
import starline from '/path/to/starline.min.js';
After that, use the Query Selector for the <canvas> elements in your HTML to initialize. You can put this line anywhere based on your needs. In this example, we are targeting canvas element with class starline:
new starline(".starline");
Now, just save the file initiator.js. Congratulations, you have successfully integrated and activated Starline.js!