這個項目http://biduleohm.free.fr/ledohm/(對不起,用戶界面是法語但代碼是英文)我需要一個角度漸變,但它不存在原生的,所以我已經在一條線上使用線性漸變來實現它,並且我將線畫得越來越長以形成一個三角形。其結果是圖形確定,但速度並不是很好(125個三角形爲1850毫秒)。它位於[Répartition]選項卡中,如果其中一個輸入端存在關鍵事件,則重新繪製,不要害怕明顯的慢,我每2000毫秒限制一次重繪。畫布:用漸變漸變繪製大量元素(模擬角度漸變)
在整個三角形上使用了一個簡單的線性漸變(但這與現實不匹配)並且速度正常之前,它在不到一秒的時間內就繪製了數千個三角形。使用此功能:
drawFrontLightForColor : function(x, y, w, h, color) {
var x2 = x - w;
var x3 = x + w;
var gradient = Distri.frontCanvas.createLinearGradient(x2, y, x3, y);
gradient.addColorStop(0, 'rgba(' + color + ', ' + Distri.lightEdgeAlpha + ')');
gradient.addColorStop(0.5, 'rgba(' + color + ', ' + (color == Distri.lightColors.cw ? Distri.lightCenterAlphaCw : Distri.lightCenterAlphaOther) + ')');
gradient.addColorStop(1, 'rgba(' + color + ', ' + Distri.lightEdgeAlpha + ')');
Distri.frontCanvas.fillStyle = gradient;
Distri.frontCanvas.beginPath();
Distri.frontCanvas.moveTo(x, y);
Distri.frontCanvas.lineTo(x2, (y + h));
Distri.frontCanvas.lineTo(x3, (y + h));
Distri.frontCanvas.lineTo(x, y);
Distri.frontCanvas.fill();
Distri.frontCanvas.closePath();
},
然後我切換到該功能:
drawFrontLightForColor : function(x, y, w, h, centerColor, edgeColor) {
var ratio = w/h;
var tmpY;
var tmpW;
var x2;
var x3;
var gradient;
Distri.frontCanvas.lineWidth = 1;
for (var tmpH = 0; tmpH < h; tmpH++) {
tmpY = y + tmpH;
tmpW = Math.round(tmpH * ratio);
x2 = x - tmpW;
x3 = x + tmpW;
gradient = Distri.frontCanvas.createLinearGradient(x2, tmpY, x3, tmpY);
gradient.addColorStop(0, edgeColor);
gradient.addColorStop(0.5, centerColor);
gradient.addColorStop(1, edgeColor);
Distri.frontCanvas.beginPath();
Distri.frontCanvas.moveTo(x2, tmpY);
Distri.frontCanvas.lineTo(x, tmpY);
Distri.frontCanvas.lineTo(x3, tmpY);
Distri.frontCanvas.strokeStyle = gradient;
Distri.frontCanvas.stroke();
Distri.frontCanvas.closePath();
}
},
你可以找到整個源here
我不能把beginPath方法,中風,closePath出來的因爲每次迭代都會改變漸變的循環(我試過了,但是它對每一行使用了最後一個漸變(具有諷刺意味的是,它與第一個功能相同......)這是可以理解的,但不是我想要的)。
我接受任何建議(包括重做整個功能,並修改他的調用者外包一些代碼),以提高速度讓我們說5倍(理想情況下更多)。
你可以試試'WebGL'。它應該能夠處理數百萬個三角形。 – kirilloid
我在鏈接上找不到「Répartition」選項卡。你能附上一個.png到你的問題。 – markE
是的,但如果可能的話,我想保留畫布。此外,在我看來,WebGL與所有瀏覽器不兼容,但我會檢查,謝謝。 @markE我不能發佈超過2個鏈接,因爲這是我的第一個問題。但是執行Shared。$ tabs._1.click();在控制檯。 – Biduleohm