player-javascript
Click Events

Click Events

By configuring the onClick parameter, you can listen to element click events and execute corresponding callback functions.

Usage Example

Below is an example showing how to handle element click events with the iCraft Player component.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
 
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self';
               script-src 'self' 'unsafe-inline' 'unsafe-eval' https://unpkg.com blob:;
               worker-src blob: 'self';
               style-src 'self' 'unsafe-inline';
               img-src 'self' data: blob:;
               font-src 'self' blob:;
               connect-src 'self' https://icraft.gantcloud.com blob:;
               object-src 'none';
               base-uri 'self';
               form-action 'self'"
    />
 
    <title>ICraft Player Click Demo</title>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      #container {
        width: 100%;
        height: 100vh;
        position: relative;
      }
      #text {
        position: absolute;
        bottom: 10px;
        right: 10px;
      }
    </style>
    <script src="https://unpkg.com/@icraft/player@latest/dist/umd/icraft-player.min.js"></script>
  </head>
  <body>
    <div id="container">
      <div id="text"></div>
    </div>
  </body>
  <script>
    const player = new ICraftPlayer({
      src: "https://icraft.gantcloud.com/api/static/templates/AWSCloud.iplayer",
      container: document.getElementById("container"),
      onClick: (e) => {
        console.log(e);
        const { instance: element } = e;
        const name = element?.options?.name || "";
        document.getElementById("text").innerText = "Clicked:" + name;
      },
    });
  </script>
</html>

API

NameDescriptionTypeDefaultRequired
onClickClick event callback(params: ClickParams) => voidundefinedfalse

ClickParams

interface ClickParams {
  instance: Element3D | null;
  event: MouseEvent;
  data: OptionsType | null;
}