我有下面的代碼應該在canvas元素中顯示淹死的線條。D3js的畫布和線條不可見
var initCanvas = function() {
var episodeLengthInPixels = moment.duration(episodeLogLength).asSeconds() * episodeWidthMultiplication;
console.log("Length of chart is "+episodeLengthInPixels +" px");
try {
canvas = d3.select("body").append("canvas")
.attr("width", 500)
.attr("height", canvasHeight)
.attr("class", canvasSelector);
//Draw the Line
canvas.append("line") // attach a line
.style("stroke", "black") // colour the line
.attr("x1", 0) // x position of the first end of the line
.attr("x2", 500)
.attr("y1", waveHeight)
.attr("y2", waveHeight) ;
} catch (e) {
console.error(e);
}
}
問題是畫布和線條在DOM模型中可用但不可見(不會拋出異常)。當我嘗試使用SVG而不是畫布時,一切正常。
如何使用D3.js庫在畫布上顯示內容?我試圖找到任何例子,但沒有運氣。我應該使用畫布使用D3.js還是其他的東西(例如在畫布中純畫圖)?
非常感謝您的任何建議。
畫布是不是基於DOM的事情。您將獲得一個畫布上下文並通過畫布API在其上繪製線條。 –