我看到使用拉斐爾繪畫中超過SVG的文本項絕對定位的div主要有兩個原因:
- 你必須多行文字,不想手動把它包(如 提示)
- 你真的需要完整的CSS樣式可用於HTML文本
在你的情況,我會做一個小拉斐爾函數放置標籤,並繪製一條線到圖像上的適當位置。這個例子把標籤上的表示現場的y值圖像的邊緣,但是你可以很容易地調整到把標籤上的圖像,並在不同的海拔高度(即對角線)。
//x and y relative to image
Raphael.el.label = function (label, dx, dy) {
//absolute x and y
var x = dx + this.attr("x"),
y = dy + this.attr("y"),
label,
line;
//decide whether to put label on left or right
if (dx < this.attr("width")/2) {
label = paper.text(this.attr("x") - 10, y, label).attr("text-anchor" , "end");
line = paper.path("M" + (this.attr("x") - 5) + "," + y + "l" + (dx + 5) + ",0").attr("stroke", 2);
} else {
label = paper.text(this.attr("x") + this.attr("width") + 10, y, label).attr("text-anchor" , "start");
line = paper.path("M" + (this.attr("x") + this.attr("width") + 5) + "," + y + "L" + (x) + "," + y).attr("stroke", 2);
}
};
jsFiddle
爲什麼不使用PHP的GD庫?可能是最好的選擇恕我直言 – jeremy
因爲較大的項目不在PHP中。我將澄清這個問題:圖像和標籤數據將作爲單獨的資源提供給瀏覽器。 – pjmorse