player-javascript
点击事件

点击事件

通过配置 onClick 参数,可以监听元素的点击事件,并执行相应的回调函数。

使用示例

下面是一个示例,展示了如何使用 iCraft Player 组件进行元素点击事件交互。

<!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
onClick点击事件回调(params: ClickParams) => voidundefinedfalse

ClickParams

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