Famous Engine(2015)创建场景时,如何设置选择器?规则是什么?

When creating a scene in Famous Engine (2015), how can I set the selector? What are the rules?

到目前为止,我只能默认将它附加到 body,或者如果我指定 div,则 famous-dom-renderer 附加到它找到的第一个 div .如何根据class或id设置选择器?

var scene = FamousEngine.createScene('the div I am targeting');

好吧,我尝试的下一件事情奏效了,所以这里是答案:为您的目标 div 设置一个 id 创建这样的场景:

<div id="scene-target"></div>

var scene = FamousEngine.createScene('#scene-target');

任何 DOM 选择器 (string) 值都可以传递给 FamousEngine.createScene 方法。

var scene = FamousEngine.createScene('body');

如果没有参数传递给 FamousEngine.createScene 方法,那么它将默认为 body DOM 元素(同上)。

var scene = FamousEngine.createScene();

使用以下 HTML,以下任何一项都有效。

var scene = FamousEngine.createScene('#top');
var scene = FamousEngine.createScene('.top');
var scene = FamousEngine.createScene('#bottom');
var scene = FamousEngine.createScene('.bottom');
<!DOCTYPE html>
<html>
    <head>
      <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link rel="icon" href="favicon.ico?v=1" type="image/x-icon">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="http://code.famo.us/famous/0.6.2/famous.min.js"></script>
        <style>
            html, body {
                width: 100%;
                height: 100%;
                margin: 0px;
                padding: 0px;

            }
            body {
                position: absolute;
                -webkit-transform-style: preserve-3d;
                transform-style: preserve-3d;
                -webkit-font-smoothing: antialiased;
                -webkit-tap-highlight-color: transparent;
                background-color: black;
                -webkit-perspective: 0;
                perspective: none;
                overflow: hidden;
            }
          .top {
            color: white;
            height: 50%;
          }
          .bottom {
            color: white;
            height: 50%;
          }
        </style>
    </head>
    <body>
      <div id="top" class="top">This is the top DOM element</div>
      <div id="bottom" class="bottom">This is the bottom DOM element</div>
    </body>
</html>

使用下面的代码片段测试上面的代码。

// Famous dependencies
var DOMElement = famous.domRenderables.DOMElement;
var FamousEngine = famous.core.FamousEngine;
var Camera = famous.components.Camera;
var clock = FamousEngine.getClock();

// Initialize with a scene; then, add a 'node' to the scene root
var scene = FamousEngine.createScene('#bottom');
var node = scene.addChild();

node.addUIEvent('load');
var myComponent = {
  onReceive: function(event, payload) {
    console.log(
      'Received ' + event + ' event!',
      payload
    );
    if (event === 'load') {
      payload.node.requestUpdate(spinner);
    }
  }
};
node.addComponent(myComponent);

// Create an [image] DOM element providing the logo 'node' with the 'src' path
var el = new DOMElement(node, {
    tagName: 'img'
  })
  .setAttribute('src', 'http://staging.famous.org/examples/images/famous-logo.svg');

// Chainable API
node
// Set size mode to 'absolute' to use absolute pixel values: (width 250px, height 250px)
  .setSizeMode('absolute', 'absolute', 'absolute')
  .setAbsoluteSize(50, 50)
  // Center the 'node' to the parent (the screen, in this instance)
  .setAlign(0.5, 0.5)
  // Set the translational origin to the center of the 'node'
  .setMountPoint(0.5, 0.5, 0.5)
  // Set the rotational origin to the center of the 'node'
  .setOrigin(0.5, 0.5);

// Add a spinner component to the logo 'node' that is called, every frame
var spinner = node.addComponent({
  onUpdate: function(time) {
    node.setRotation(0, time / 500, 0);
    node.requestUpdateOnNextTick(spinner);
  }
});

// Let the magic begin...
node.requestUpdate(spinner);
FamousEngine.init();

// To set perspective
var camera = new Camera(scene);
camera.setDepth(1000);
html,
            body {
              width: 100%;
              height: 100%;
              margin: 0px;
              padding: 0px;
            }
            body {
              position: absolute;
              -webkit-transform-style: preserve-3d;
              transform-style: preserve-3d;
              -webkit-font-smoothing: antialiased;
              -webkit-tap-highlight-color: transparent;
              background-color: black;
              -webkit-perspective: 0;
              perspective: none;
              overflow: hidden;
            }
            .top {
              color: white;
              height: 50%;
            }
            .bottom {
              color: white;
              height: 50%;
            }
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="icon" href="favicon.ico?v=1" type="image/x-icon">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="http://code.famo.us/famous/0.6.2/famous.min.js"></script>
</head>

<body>
  <div id="top" class="top">This is the top DOM element</div>
  <div id="bottom" class="bottom">This is the bottom DOM element</div>
</body>

</html>