仅当滚动后 canvas 变为可见时才启动动画 (Animate CC)。

Start Animation (Animate CC) only when the canvas become visible after scrolling.

我有一个动画(使用 Adob​​e animate CC 制作)已包含在我的 html 文件中。我正在尝试更改其原始脚本,以便仅当它在浏览器的视口中可见时才从头开始播放动画。不幸的是,我进行了广泛的搜索,但无法找到适合我的案例的任何解决方案。

这是我要更改的原始代码:

<html>

<head>

<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, 
fnStartAnimation;

function init() {
    canvas = document.getElementById("canvas");
    anim_container = document.getElementById("animation_container");
    dom_overlay_container = document.getElementById("dom_overlay_container");
    var comp=AdobeAn.getComposition("07578064E5C140B781D146A1AE4968A3");
    var lib=comp.getLibrary();
    handleComplete({},comp);
}
function handleComplete(evt,comp) {
    //This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
    var lib=comp.getLibrary();
    var ss=comp.getSpriteSheet();
    exportRoot = new lib.Water_illustrazione_revista_scrollplay();
    stage = new lib.Stage(canvas);  
    //Registers the "tick" event listener.
    fnStartAnimation = function() {
        stage.addChild(exportRoot);
        createjs.Ticker.setFPS(lib.properties.fps);
        createjs.Ticker.addEventListener("tick", stage);
    }

 //follow a code to support hidpi screens and responsive scaling.

</script>

</head>

<body onload="init();" style="margin:0px;">

<div id="anchor5" class="Anime_p">
 <div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:1280px; height:700px">
        <canvas id="canvas" width="1280" height="700" style="position: absolute; display: block; background-color:rgba(255, 255, 255, 1.00);"></canvas>
        <div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:1280px; height:700px; position: absolute; left: 0px; top: 0px; display: block;">
        </div>
    </div>
</div>

</body>

</html>

您只需将动画导出为最初停止的状态,然后检查 div 何时出现在浏览器视图中。每当它出现时,启动动画。

  1. 在fnStartAnimation()的末尾添加exportRoot.stop();;
  2. 按照此处的说明检查动画 div 在浏览器中的可见性 - How to tell if a DOM element is visible in the current viewport?
  3. 每当满足条件时,添加代码 - exportRoot.play();