0

我正在尝试在这里调整看起来很酷的 WebGL Story Sphere、源代码和 css 。有一个大问题:一旦你点击一篇新闻文章,弹出的文章文本总是一样的。我想修改代码,以便当您单击一篇文章时,正确的文本会出现在弹出窗口中。

我正在使用我在代码中指定的一组文章文本,例如var captions = ["good","better","best"]. 虽然文章标题和图像在弹出窗口中正确填充,但我无法让文本这样做。你能帮助我吗??这是我所拥有的:

// function code
var passvar = null; // failed attempt to store texts for later
function initialize() {
  math = tdl.math;
  fast = tdl.fast;
  canvas = document.getElementById("canvas");
  g_fpsTimer = new tdl.fps.FPSTimer();
  hack();

  canvas.addEventListener("mousedown", handleMouseDown, false);
  canvas.addEventListener("mousemove", handleMouseMove, false);
  canvas.addEventListener("mouseup", handleMouseUp, false);

  // Create a canvas 2d for making textures with text.
  g_canvas2d = document.createElement('canvas');

  window.two2w = window.two2h = g_tilesize; 

  g_canvas2d.width = two2w;
  g_canvas2d.height = two2h;
  g_ctx2d = g_canvas2d.getContext("2d");

  window.gl = wu.create3DContext(canvas);
  if (g_debug) {
    gl = wd.makeDebugContext(gl, undefined, LogGLCall);
  }
  //gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, gl.TRUE);

  // Here is where I specify article titles, images, captions
  // Titles and images populate the popup correctly, captions don't...

  var titles = ["a","b","c"];
  var captions = ["good","better","best"];
  var images = ['imagesphere/assets/1.jpg', 
                'imagesphere/assets/bp/2.png', 
                'imagesphere/assets/bp/3.png'
                ];

  var headlines = titles.concat( titles);
  var blurbs = captions.concat( captions);

  var tmpImages = [];
  var tmpHeadlines = [];
  var tmpCaptions = [];

  // make a bunch of textures.
  for (var ii = 0; ii < g_imagesDownGrid; ++ii) {
    var textures = [];
    for (var jj = 0; jj < g_imagesAcrossGrid; ++jj) {
      var imgTexture = new ImgTexture();

      textures.push(imgTexture);
      if (tmpImages.length == 0) {
        tmpImages = images.slice();
      }
      if (tmpHeadlines.length == 0) {
        tmpHeadlines = headlines.slice();
      }
      if (tmpCaptions.length == 0) {
          tmpCaptions = blurbs.slice();
      }
      var rando = math.randomInt(tmpImages.length);
      var img = tmpImages.splice(rando, 1)[0];
      var headline = tmpHeadlines.splice(rando, 1)[0];
      var caption = tmpCaptions.splice(rando, 1)[0];
      passvar = caption;

      if (img.indexOf('videoplay.jpg') > -1){
        window.vidtexture = imgTexture;
        images = images.slice(1); // dont use that thumb again.
        headlines = 'WebGL Brings Video To the Party as Well'
      }

      imgTexture.load(img, /* "[" + jj + "/" + ii + "]" + */ headline);
    }
    g_textures.push(textures);
  }

  // And here's where I try to put this in a popup, finally
  // But passvar, the stored article text, never refreshes!!!

 <div id="story" class="prettybox" style="display:none"> 
    <img class="close" src="imagesphere/assets/close.png">
    <div id="storyinner">
        <input id = "mytext">
            <script>document.getElementById("mytext").value = passvar;</script>
    </div>
 </div>

这是我的点击处理程序代码:

   function sphereClick(e){
   window.console && console.log('click!', e, e.timeStamp);

   var selected = g_textures[sel.y][sel.x];
   window.selected = selected;

   animateGL('eyeRadius', glProp('eyeRadius'), 4, 500); 

   var wwidth   = $(window).width(),
       wheight  = $(window).height(),
       story    = $('#story').width( ~~(wwidth / 7 * 4) ).height( ~~(wheight / 6 * 5) ), 
       width    = story.width(),
       height   = story.height(),
       miniwidth = 30;

   story.detach()
    .find('#storyinner').find('h3,img,caption').remove().end().end()
    .show();

   story.css({
     left        :   e.pageX,
     top         :   e.pageY,
     marginLeft  :   - width / 2,
     marginTop   :   - height / 2

   }).appendTo($body); // we remove and put back on the DOM to reset it to the correct position.

   $('style.anim.story').remove();
   $('<style class="anim story">')
      .text( '.storyopen #story { left : ' + (wwidth  / 3 * 2) + 'px !important;  top : ' + wheight / 2 + 'px !important; }' )
      .appendTo($body);
   $(selected.img).prependTo('#storyinner').parent();

   $('<h3>').text(selected.msg.replace(/\(.*/,'')).prependTo('#storyinner');

   $body.addClass('storyopen');


} // eo sphereClick()
4

1 回答 1

0

这里有很多错误,但这是一个开始。它不会解决您的问题,但会帮助您避免此类问题。


var passvar = null;是一个全局变量。

您的循环for (var ii = 0; ...在每次迭代时将该全局变量设置为一个新值。

稍后,您单击某些东西,全局变量passvar永远不会改变。

如果您想使用此模式,您需要passvar从您的点击处理程序中进行设置,以便它具有被点击的值。由于您实际上并没有发布您的点击处理程序,因此很难提供更多建议。


但这也是一种不好的模式,函数接受参数是有充分理由的。既然你必须在点击处理程序中找到你点击的项目,为什么不直接传递它,它确实涉及一个共享的全局变量?

var handleMouseUp = function(event) {
  var story = findClickedThing(event);
  if (obj) {
    showPopup(story.texture, story.caption);
  }
}

这让我想到了这一点:

var titles = ["a","b","c"];
var captions = ["good","better","best"];
var images = ['imagesphere/assets/1.jpg', 
              'imagesphere/assets/bp/2.png', 
              'imagesphere/assets/bp/3.png'
              ];

当你有 3 个长度相同的数组,每个数组描述一个对象的不同属性时,你做错了。您想要的是组对象。

var stories = [
  {
    title: "a",
    caption: "good",
    image: "imagesphere/assets/1.jpg"
  }, {
    title: "b",
    caption: "better",
    image: "imagesphere/assets/bp/2.jpg"
  }, {
    title: "c",
    caption: "best",
    image: "imagesphere/assets/bp/3.jpg"
  },
];

console.log(stories[1].caption); // "better"

现在一旦你找到点击的对象,你可以问它的标题是什么。您可以将整个对象传递给弹出窗口制作者。并且没有任何字段以不同的方式处理或以不同的方式传递,因为您没有传递字段。您正在传递整个对象

于 2012-12-16T00:05:02.210 回答