gpt4 book ai didi

meteor - 我们如何在 famo.us 中获取表面的大小?

转载 作者:行者123 更新时间:2023-12-04 00:26:32 25 4
gpt4 key购买 nike

如果我创建一个尺寸为 [true, true] 的 famo.us 表面并将一些任意的 html 内容放入其中,有没有一种干净的方法来检索对象的大小?

surface.getSize() 只是返回相同的 [true,true]

我注意到有一些显然是私有(private)的方法,例如: s._currTarget.clientHeight但使用它们似乎是自找麻烦!

最佳答案

有几种方法可以解决这个问题。 Famo.us 意识到了这个限制,它应该在优先级列表中排在相当高的位置。

在此示例中,创建了一个新的表面类,它在部署函数中或渲染表面时发出一个事件。一定要在构造的时候抓取原来deploy函数的引用,这样后面才能正常调用。收到渲染事件后,您可以按照自己的意愿编辑表面。

祝你好运!

var Engine            = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var StateModifier = require('famous/modifiers/StateModifier');
var EventHandler = require('famous/core/EventHandler');


function MySurface(options) {
Surface.apply(this, arguments);
this._superDeploy = Surface.prototype.deploy;
}

MySurface.prototype = Object.create(Surface.prototype);
MySurface.prototype.constructor = MySurface;


MySurface.prototype.deploy = function deploy(target) {
this._superDeploy(target);
this.eventHandler.trigger('surface-has-rendered', this);
};

var context = Engine.createContext();

var event_handler = new EventHandler();

event_handler.on('surface-has-rendered', function(surface){

var size = surface.getSize();
var width = (size[0] == true) ? surface._currTarget.offsetWidth : size[0] ;
var height = (size[1] == true) ? surface._currTarget.offsetHeight : size[1] ;

surface.setSize([width,height]);

console.log(surface.getSize());

})

var surface = new MySurface({
size: [true,true],
content: "Hello",
properties: {
color: 'white',
textAlign: 'center',
padding: '20px',
backgroundColor: 'green'
}
})

surface.pipe(event_handler);

context.add(new StateModifier({origin:[0.5,0.5]})).add(surface);

关于meteor - 我们如何在 famo.us 中获取表面的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23319496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com