私は迷路を持っています。私が望むのは、そのセルのすべてに円を作成し、それらの円を木のように迷路の右側に移動することです。私はd3トランジションとhtml5キャンバスを使用します。私の質問は、私ができる最適化があるかどうかです。私はキャンバスのパフォーマンスを知らないので、どれくらい期待できるのかわかりません。キャンバスレンダリングの最適化
1600要素(600 px x 600 pxおよび15 px cellSize)の場合、アニメーションはスムーズです。 3600要素についてはそうではありません。
mazeSelection.selectAll('cell')
.data(root.descendants())
.enter()
.append('cell')
.attr('radius', 0)
.attr('cx', d => d.data.ix * cellSize + 0.5 * cellSize,)
.attr('cy', d => d.data.iy * cellSize + 0.5 * cellSize)
.transition().duration(2000)
.attr('radius', cellSize/2)
.transition().duration(3000)
.attr('radius', cellSize/4)
.tween('position', function(d) {
const i = d3.interpolate([this.getAttribute('cx'), this.getAttribute('cy')], [width + d.x, cellSize * 0.5 + d.y]);
return (t) => {
[d.cx, d.cy] = i(t);
this.setAttribute('cx', d.cx);
this.setAttribute('cy', d.cy);
};
});
context.fillStyle = "white";
let timer = d3.timer(function redraw() {
// clear maze
context.beginPath();
context.fillRect(0, 0, width * 2, height);
//drawMaze(grid, context, width, height, cellSize);
context.beginPath();
// here we must use a function to have access to "this"
mazeSelection.selectAll('cell')
.each(function (d) {
const radius = this.getAttribute('radius'),
x = this.getAttribute('cx'),
y = this.getAttribute('cy');
context.moveTo(x, y);
context.arc(x, y, radius * 0.5, 0, 2 * Math.PI);
(d.children || []).forEach(child => {
context.moveTo(x, y);
context.lineTo(child.cx, child.cy);
});
});
context.stroke();
context.fill();
});
ここでDOM要素を作成していますが、これを避けて、javascript構造のリストを作成してから繰り返します。 –
返信ありがとうございます。私は2つの質問があります:1.私がappendを使用しても、私はchrome開発ツールを使用すると、DOMツリーに要素が表示されません。彼らはとても悪いですか? 2. DOM要素を作成せずに遷移を適用する方法を知っていますか? –
1. DOMツリー上に複数のフォルダを作成することはできますが、chrome開発ツールはドキュメントに添付したものだけを表示することができます。 1b)はい2.私の最初のコメントにつきコードを記入してください。 –