我一直在練習使用JavaScript和畫布,我已經到了我正在渲染一個形狀或兩個和幾行來創建自定義形狀的問題。現在我需要能夠單獨爲每個單獨着色,但是當我使用新的Image();其中第一個形狀是指該紋理在繪製的最後一個ctx形狀上呈現。如何分隔每個HTML Canvas路徑/形狀以單獨填充?
這裏是我的代碼:
renderCanvas: function(topLength, heightLength, slant) {
var canvases = Array.from(document.getElementsByClassName("DesignerCanvas"));
for (var canvas of canvases) {
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#000';
ctx.lineWidth = 0.5;
// String for Hanging
ctx.beginPath();
ctx.rect(150, 0, 3, 75);
ctx.stroke();
ctx.closePath();
// Top Trimming
ctx.beginPath();
ctx.moveTo((150 - (topLength/2)) , 80);
ctx.quadraticCurveTo(150 , 70, 150 + (topLength/2), 80);
ctx.lineTo(150 + (topLength/2), 83);
ctx.quadraticCurveTo(150 , 73, 150 - (topLength/2), 83);
ctx.closePath();
ctx.stroke();
// Shade Outter Body
ctx.beginPath();
ctx.moveTo(150 + (topLength/2), 83);
ctx.quadraticCurveTo(150 , 73, 150 - (topLength/2), 83);
ctx.lineTo(150 - ((topLength/2) + slant), heightLength);
ctx.quadraticCurveTo(150 , (heightLength - 10), 150 + ((topLength/2) + slant), heightLength);
ctx.closePath();
ctx.stroke();
// Shade Inner Body
ctx.beginPath();
ctx.moveTo(150 + ((topLength/2) + slant), heightLength);
ctx.quadraticCurveTo(150 , (heightLength - 10), 150 - ((topLength/2) + slant), heightLength);
ctx.quadraticCurveTo(150 , (heightLength + 5), 150 + ((topLength/2) + slant), heightLength);
ctx.stroke();
ctx.closePath();
// Bottom Trimming
ctx.beginPath();
ctx.moveTo(150 + ((topLength/2) + slant), (heightLength - 3));
ctx.quadraticCurveTo(150 , (heightLength - 15), 150 - ((topLength/2) + slant), (heightLength - 3));
ctx.lineTo(150 - ((topLength/2) + slant), heightLength);
ctx.quadraticCurveTo(150 , (heightLength + 5), 150 + ((topLength/2) + slant), heightLength);
ctx.closePath();
ctx.stroke();
}
},
init: function() {
console.log("Product Designer: Initialized.");
this.bindActions();
this.renderPage("DesignerTab1", "DesignerPanel1");
}
}
好澄清,我的最終結果應該基本相同,因爲它現在是怎樣,但與改變紋理背景的能力我所做過的所有路徑。例如第一個路徑字符串應該能夠填充圖像,下一個路徑頂部修剪應該能夠填充圖像等。
我的代碼中的問題是,當您將圖像放置到新圖像( ),那麼紋理將呈現在整個形狀的底部,這不是我想要的,它應該只填充形狀的頂部。
對不起不知道怎麼我的jsfiddle這裏 – Elevant
環節一個環節的工作就好了。 –
你可以嘗試更清楚地解釋你想要完成的事情嗎?最終的結果應該是什麼?三個單獨的圖像或只是三個不同顏色的形狀? –