简体   繁体   English

重用类的问题/不理解实例化

[英]Problem re-using a Class / Not understanding instantiation

Update - JSFiddle demo here: https://jsfiddle.net/vb2ptmuo/11/更新 - 这里的 JSFiddle 演示: https ://jsfiddle.net/vb2ptmuo/11/

I'm playing around with an interesting effect I came across: https://tympanus.net/codrops/2019/08/07/image-trail-effects/我正在玩我遇到的一个有趣的效果: https : //tympanus.net/codrops/2019/08/07/image-trail-effects/

To start, I have a div containing a bunch of img elements.首先,我有一个包含一堆img元素的div It dumps them into an array and then creates a trail effect from those images which follows the mouse.它将它们转储到一个数组中,然后从跟随鼠标的那些图像中创建轨迹效果。 You kick this all off via new ImageTrail(".content") .你通过new ImageTrail(".content")开始这一切。 But what if I have more than one set of images and I want to re-trigger it again with those, instead?但是,如果我有不止一组图像并且我想用这些图像再次重新触发它呢? Example:例子:

    <div class="content">
        <img src="1.jpg">
        <img src="2.jpg">
        <img src="3.jpg">
    </div>
    <div class="content-2">
        <img src="4.jpg">
        <img src="5.jpg">
        <img src="6.jpg">
    </div>

Doing a second new ImageTrail(".content-2") does not replace the first set of images with the second set, even though the code reads to me like it should.执行第二个new ImageTrail(".content-2")不会用第二组图像替换第一组图像,即使代码像它应该的那样读给我。 You still just see the first set of images in the trail.您仍然只能看到跟踪中的第一组图像。

I'm also slightly concerned with performance if I'm instantiating the ImageTrail class twice (if that's even a thing), but this is wholly secondary to my main issue.如果我将 ImageTrail 类实例化两次(如果这甚至是一件事),我也有点担心性能,但这完全是我的主要问题的次要问题。

I feel like there's a simple solution but I'm missing it.我觉得有一个简单的解决方案,但我很想念它。 Scroll down to the bottom of the demo for the commented code "This doesn't work"向下滚动到演示底部的注释代码“这不起作用”

The main cause is requestAnimationFrame as part of constructor and render method.主要原因是requestAnimationFrame作为构造函数和渲染方法的一部分。

Across instances, for this use case, one instance should have the rendering control using requestAnimationFrame .跨实例,对于这个用例,一个实例应该使用requestAnimationFrame具有渲染控制。

I did a trick here, for this use case.对于这个用例,我在这里做了一个技巧。

Every time a new instance is created, it will cancel earlier instance's render request (done by requestAnimationFrame ) by means of cancelAnimationFrame Even i cancelled the request done in render method too.每次创建新实例时,它都会通过cancelAnimationFrame取消先前实例的渲染请求(由requestAnimationFrame完成),即使我也取消了在渲染方法中完成的请求。

Check out this jsfiddle for modified code : https://jsfiddle.net/rahultarafdar/mfboqy9g/45/查看此 jsfiddle 以获取修改后的代码: https ://jsfiddle.net/rahultarafdar/mfboqy9g/45/

The Code that you posted has one major problem: it uses global variables to store state of the ImageTrail object.您发布的代码有一个主要问题:它使用全局变量来存储 ImageTrail 对象的状态。 This is a pretty bad programming practice as it leads to unexpected behaviour and defeats the purpose of having a class in the first place.这是一种非常糟糕的编程实践,因为它会导致意想不到的行为,并且首先违背了拥有类的目的。

The reason why only the first set of pictures is shown is because both instances of ImageTrail register a callback for the next animation frame with requestAnimationFrame .之所以只显示第一组图片,是因为 ImageTrail 的两个实例都使用requestAnimationFrame注册了下一个动画帧的回调。 The callback ( render ) of the first instance is always called first, because it registered the listener first.第一个实例的回调( render )总是首先被调用,因为它首先注册了监听器。 This first render function updates the global mousePos variables.第一个渲染函数更新全局mousePos变量。 The second render function thinks the mouse has not moved, because lastMousePos and mousePos are the same.第二个render函数认为鼠标没有移动,因为lastMousePosmousePos是一样的。

Now to address your problem: first you should move all the state that is used by ImageTrail into the class itsef.现在解决您的问题:首先,您应该将 ImageTrail 使用的所有状态移动到类 itef 中。 That includes mousePos , cacheMousePos and lastMousePos .这包括mousePoscacheMousePoslastMousePos If you have done that successfully, you should get both imagesets diyplaying at the same time, if you have two instances.如果你已经成功地做到了这一点,如果你有两个实例,你应该同时得到两个图像集。 To activate/deactivate the rendering of ImageTrails, you could add an active attribute, which gets checked at every render call or you could implement methods that cancel/setup the animation frame for a specific instance.要激活/停用 ImageTrails 的渲染,您可以添加一个活动属性,在每次渲染调用时都会检查该属性,或者您可以实现取消/设置特定实例的动画帧的方法。 (For details how to use cancelAnimationFrame you could look into the answer from rahul or into the MDN ) (有关如何使用cancelAnimationFrame详细信息,您可以查看rahulMDN的答案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM