how can we get the size of a surface within famo.us?

前端 未结 3 2077
说谎
说谎 2020-12-20 02:03

If i create a famo.us surface giving a size of [true, true] and put some arbitrary html content into it, is there a clean way to retrieve the size of the object?

3条回答
  •  粉色の甜心
    2020-12-20 02:48

    The easiest way to get the actual size of a surface is surface.getSize(true). You don't need to subclass Surface.

    However, you will get an error if the surface isn't rendered to DOM yet. When a surface is rendered, it will emit a 'deploy' event. You can listen for it like this:

    surface.on('deploy', function () {
        // Your Code Here
    }.bind(this));
    

    More than likely you will also have to wrap your code in a Timer.setTimeout() too. This is because the surface.getSize(true) method will execute faster than the next render cycle (60 frames per second or ~16 milliseconds per cycle). The final code will look like this:

    // With the rest of your require statements
    var Timer = require('famous/utilities/Timer');
    
    surface.on('deploy', function () {
        Timer.setTimeout( function () {
            // Your code here
            // this.surface.getSize(true)
        }.bind(this), 16);
    }.bind(this));
    

    I'm new to Famo.us too but I have had to implement this fix for a few different situations in my project and it seems to work perfectly.

    Let me know if it works for you.

提交回复
热议问题