【问题标题】:Animation html code in canvas not running画布中的动画html代码未运行
【发布时间】:2019-03-31 02:33:48
【问题描述】:

我已经编写了下面的代码以在 html5 中运行,但它似乎不起作用。下面的代码应该在用户单击画布时运行。我似乎无法确定这里的问题所在。据我所知,其他一切似乎都是正确的:

var ctr = document.getElementById("canvas").getContext("2d");

function sol86() {
  var ctx = d3.select('#lines')
    .append('ctx')
    .attr("width", "100%")
    .attr("height", "100%");



  function lineData() {
    function getRandomArbitrary(min, max) {
      return Math.random() * (max - min) + min;
    }
    var data = new Array();
    var id = 1;
    var ww = window.innerWidth; // Width of the window viewing area
    var wh = window.innerHeight; // Height of the window viewing area
    // iterate for cells/columns inside rows
    for (var line = 0; line < 1000; line++) { // 1000 lines
      var x1 = getRandomArbitrary(-50, ww); // initial points can start 100px off the screen to make even distribution work
      var y1 = getRandomArbitrary(-50, wh);
      data.push({
        id: id, // For identification and debugging
        x1: x1,
        y1: y1,
        x2: x1 + 50, // Move 100 to the right
        y2: y1 + 50, // Move 100 up
        rotate: getRandomArbitrary(0, 360) // Pick a random angle between 0 and 360
      })
      id++;
    }
    return data;
  }

  var lineData = lineData();
  console.log(lineData);


  var line = ctx.selectAll("line")
    .data(lineData)
    .enter().append('line')
    .attr("id", function(d) {
      return d.id;
    })
    .attr("x1", function(d) {
      return d.x1;
    })
    .attr("y1", function(d) {
      return d.y1;
    })
    .attr("transform", function(d) {
      return "rotate(" + d.rotate + " " + (d.x1 + 25) + " " + (d.y1 + 25) + ")";
    })
    .attr("x2", function(d) {
      return d.x1;
    })
    .attr("y2", function(d) {
      return d.y1;
    }).transition().delay(function(d, i) {
      return 1.5 * i;
    }).duration(750)
    .attr("x2", function(d) {
      return d.x2;
    })
    .attr("y2", function(d) {
      return d.y2;
    });
}

// run on load

$(document).ready(function(event) {
  $(window).on("click", function(event) {
    d3.selectAll("ctr").remove();
    sol86();
  });
});



$(window).resize(function() {
  if (this.resizeTO) clearTimeout(this.resizeTO);
  this.resizeTO = setTimeout(function() {
    $(this).trigger('resizeEnd');
  }, 500);
});

//resize on resizeEnd function
$(window).bind('resizeEnd', function() {
  d3.selectAll("ctr").remove();
  sol86();
});
body {
  margin: 0;
}

.drawing {
  margin: 0;
}

#lines {
  width: 100%;
  height: 100%;
}

line {
  stroke: #111;
  stroke-width: 1;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<canvas id='canvas' width='1000' height='1000'></canvas>
<div class="drawing">
  <div id="lines"></div>
</div>

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 当你在画布上按下时应该发生什么?
  • @A.JAlhorr 当我点击屏幕时,动画会加载并绘制 1000 条固定长度的随机线

标签: javascript jquery html html5-canvas mouseevent


【解决方案1】:

您正在创建奇怪的&lt;ctx&gt; 标签而不是&lt;svg&gt; 标签。我已经改变了。画布有一个背景,以便我可以看到它并单击它。我也把它变小了。希望对你有帮助。

var ctr = document.getElementById("canvas").getContext("2d");

function sol86() {
 var ctx = d3.select('#lines')
.append('svg')
.attr("width", "100%")
.attr("height", "100%");



  function lineData() {
    function getRandomArbitrary(min, max) {
      return Math.random() * (max - min) + min;
    }
    var data = new Array();
    var id = 1;
    var ww = window.innerWidth; // Width of the window viewing area
    var wh = window.innerHeight; // Height of the window viewing area
    // iterate for cells/columns inside rows
    for (var line = 0; line < 1000; line++) { // 1000 lines
      var x1 = getRandomArbitrary(-50, ww); // initial points can start 100px off the screen to make even distribution work
      var y1 = getRandomArbitrary(-50, wh);
      data.push({
        id: id, // For identification and debugging
        x1: x1,
        y1: y1,
        x2: x1 + 50, // Move 100 to the right
        y2: y1 + 50, // Move 100 up
        rotate: getRandomArbitrary(0, 360) // Pick a random angle between 0 and 360
      })
      id++;
    }
    return data;
  }

  var lineData = lineData();
  //console.log(lineData);


  var line = ctx.selectAll("line")
    .data(lineData)
    .enter().append('line')
    .attr("id", function(d) {
      return d.id;
    })
    .attr("x1", function(d) {
      return d.x1;
    })
    .attr("y1", function(d) {
      return d.y1;
    })
    .attr("transform", function(d) {
      return "rotate(" + d.rotate + " " + (d.x1 + 25) + " " + (d.y1 + 25) + ")";
    })
    .attr("x2", function(d) {
      return d.x1;
    })
    .attr("y2", function(d) {
      return d.y1;
    }).transition().delay(function(d, i) {
      return 1.5 * i;
    }).duration(750)
    .attr("x2", function(d) {
      return d.x2;
    })
    .attr("y2", function(d) {
      return d.y2;
    });
}

// run on load

$(document).ready(function(event) {
  $(window).on("click", function(event) {
    d3.selectAll("ctr").remove();
    sol86();
  });
});



$(window).resize(function() {
  if (this.resizeTO) clearTimeout(this.resizeTO);
  this.resizeTO = setTimeout(function() {
    $(this).trigger('resizeEnd');
  }, 500);
});

//resize on resizeEnd function
$(window).bind('resizeEnd', function() {
  d3.selectAll("ctr").remove();
  sol86();
});
body {
  margin: 0;
}

.drawing {
  margin: 0;
}

#lines {
  width: 100%;
  height: 100%;
}

line {
  stroke: #111;
  stroke-width: 1;
}

canvas{background:#d9d9d9;}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<canvas id='canvas' width='1000' height='50%'></canvas>
<div class="drawing">
  <div id="lines"></div>
</div>

更新

OP 不想要 SVG 元素,她/他想要在画布上绘制线条。我已经从 HTML 中删除了所有 d3 人员、SVG 样式和无用的部分。从她/他的代码中,我仅使用函数 lineData() 创建用于绘制线条的随机数据。 线条是在点击和调整大小时绘制的。

let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
let ww = (canvas.width = window.innerWidth);
let wh = (canvas.height = window.innerHeight);
let rid = null;
let length = 0;

function lineData() {
  function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
  }

  var data = new Array();
  //var id = 1;
  //var ww = window.innerWidth; // Width of the window viewing area
  //var wh = window.innerHeight; // Height of the window viewing area
  // iterate for cells/columns inside rows
  for (var line = 0; line < 1000; line++) {
    // 1000 lines
    var x1 = getRandomArbitrary(-50, ww); // initial points can start 100px off the screen to make even distribution work
    var y1 = getRandomArbitrary(-50, wh);
    data.push({
      //id: id, // For identification and debugging

      x1: x1,
      y1: y1,
      //x2: x1 + 50, // Move 100 to the right
      //y2: y1 + 50, // Move 100 up
      rotate: getRandomArbitrary(0, 2 * Math.PI) // Pick a random angle between 0 and 360
    });
    //id++;
  }
  return data;
}

let linedata = lineData();
//console.log(linedata)

function drawLine(l) {
  ctx.save();
  ctx.rotate(l.rotate);
  ctx.translate(l.x1, l.y1);
  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(length, length);
  ctx.stroke();
  ctx.restore();
}

function Draw() {
  rid = requestAnimationFrame(Draw);
  length++;

  if (length <= 50) {
    ctx.clearRect(0, 0, ww, wh);
    linedata.map(l => {
      drawLine(l);
    });
  } else {
    if (rid) {
      cancelAnimationFrame(rid);
      rid = null;
    }
  }
}

function Init() {
  if (rid) {
    cancelAnimationFrame(rid);
    rid = null;
  }
  ww = canvas.width = window.innerWidth;
  wh = canvas.height = window.innerHeight;
  length = 0;
  linedata = lineData();
  Draw();
}

canvas.addEventListener("click", Init, false);

window.setTimeout(function() {

  window.addEventListener("resize", Init, false);
}, 15);
canvas{background:#d9d9d9;}
&lt;canvas id='canvas'&gt;&lt;/canvas&gt;

【讨论】:

  • 这是您一如既往的完美!但是 d3.selectAll("ctr").remove(); 行中只有一个问题。画布不清晰,因为它假设它会重新激活绘图,将前一个留在那里......
  • 另外动画也没有在画布上绘制,为什么?
  • 这行代码:d3.selectAll("ctr").remove(); 正在删除所有&lt;ctr&gt; 标记(如果有的话)。如果你打算删除&lt;canvas&gt;,你应该写d3.selectAll("canvas").remove();。但是,每次点击都会得到一个新的&lt;svg&gt;element
  • 动画永远不会在画布上绘制,因为你永远不会在画布上绘制任何东西。
  • 这是我的主要问题,我想要画布上的这个动画,我想替换 svg
猜你喜欢
  • 2018-04-21
  • 2019-12-16
  • 1970-01-01
  • 2016-07-27
  • 2017-05-03
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多