javascript - HTML5 Canvas : Draw lines from a centered DIV, 连接到周围的其他 DIV 吗? (蜘蛛网效应)Kinetic JS

标签 javascript jquery html html5-canvas kineticjs

我的问题是:如何绘制来自特定 DIV 的线,并让它们连接到页面上的其他 DIV? (蜘蛛网效应)

到目前为止,我得到了 - jsFiddle: http://jsfiddle.net/audnB/3/

但我想做的是......
让所有行都来自母 div,如下所示:

enter image description here

下面是我目前正在使用的代码(与我的 JsFiddle 中的代码相同):
我实际上正在使用 <a> (链接)用于此而不是 DIV,但你知道我的意思......

var lineCoords = new Array();
var stage;
var globalTimer = null;

$(window).resize(function() {
  clearTimeout(globalTimer);
  globalTimer = setTimeout(doneResize, 100);
});

function doneResize(){
 drawLines();
}

function drawLines(){
      lineCoords = new Array();
      $('div#container > a').each(function(){
        if ($(this).attr('data-action-properties').length>0){
          var actionProperties = $.parseJSON($(this).attr('data-action-properties'));
        }
    var dx = $(this).css('left').replace('px','');
    var dy = $(this).css('top').replace('px','');
    var wd = ($(this).css('width').replace('px','') /2);
    var hi = ($(this).css('height').replace('px','') /2);

    var position = $(this).offset();

    lineCoords.push(parseInt(dx)+(wd));
    lineCoords.push(parseInt(dy)+(hi));
   }); 

      var layer = new Kinetic.Layer();
      var redLine = new Kinetic.Line({
      points: lineCoords,
      stroke: '#000',
      strokeWidth: 2,
      lineCap: 'round',
      lineJoin: 'round',
    dashArray: '0 30 0 30'
  });

      layer.add(redLine); 
      stage.add(layer);
  }

$(document).ready(function() {
   stage = new Kinetic.Stage({
        container: "container",
        width: $('#container').width(),
        height: $(window).height()
      });
    setTimeout(drawLines,100);
  });

  $(window).resize(function(e){
stage.clear();
});

 function imageresize() {
   var contentwidth = $('body').width();
   if ((contentwidth) < '700'){
   $('.fluidimage').attr('src','little.jpg');
   } else {
   $('.fluidimage').attr('src','big.jpg');
    }
   }

   imageresize();// Triggers when document first loads

   $(window).bind("resize", function(){ // Adjusts image when browser resized
    imageresize();
   });

非常感谢您的帮助,非常感谢!

最佳答案

当您如下声明 redLine 时

var redLine = new Kinetic.Line({
    points: lineCoords,
    stroke: '#000',
    strokeWidth: 2,
    lineCap: 'round',
    lineJoin: 'round',
    dashArray: '0 30 0 30'
    });

使用 points: lineCoords 本质上是在连续的点之间画线,所以你有两个选择, 1. 从点 2 的坐标开始,在每个点之后添加点 1(母亲)的坐标,假设母亲总是第一个。

points: [lineCoords[0],lineCoords[1],lineCoords[2],lineCoords[3],lineCoords[0],lineCoords[1],lineCoords[4],lineCoords[5]]

或 2. 制作一个 for 循环,在其中分别绘制每条线,其中指定点为: point: [lineCoords[0],lineCoords[1],lineCoords[2*i],lineCoords[2*i+1]],本质上是 i = 1,2

行的起点和终点

关于javascript - HTML5 Canvas : Draw lines from a centered DIV, 连接到周围的其他 DIV 吗? (蜘蛛网效应)Kinetic JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14279921/

相关文章:

php - 在 php 和 javascript 中制作 "mosaic"效果

javascript - 使用 jQuery 的 ShortKeys 插件检测转义键

javascript - 在 IE 中隐藏文本字段闪烁的光标,甚至将闪烁的光标颜色更改为白色

javascript - Kaazing、AngularJS 和 BasicChallengeHandler

javascript - 我的JSON文件格式正确吗?

javascript - 在 Array.find() 中使用异步函数

jquery - 多维数组 JQUERY 和 JSON

html - 当 div 在其他 div 下时,第 n 个 child 不工作

html - 下划线问题与形式

javascript - 响应标签问题