[英]How to draw an image in trapezium shape using canvas
我想使用畫布繪制梯形圖像。
我嘗試了轉換,但沒有得到梯形視圖。
請任何人給我解決方案,以使用畫布在梯形視圖中繪制圖像。
我希望實際圖像應該像這樣/ _ \\
這是將圖像“變換”為梯形的方法。 該技術是眾所周知的,將圖像線切成線,然后一點一點地按比例繪制線。
此功能允許您設置梯形形狀的數量(%)並處理縮放的圖像:
function drawTrapezoid(ctx, img, x, y, w, h, factor) {
var startPoint = x + w * 0.5 * (factor*0.01), // calculate top x
xi, yi, scale = img.height / h, // used for interpolation/scale
startLine = y, // used for interpolation
endLine = y + h; // abs. end line (y)
for(; y < endLine; y++) {
// get x position based on line (y)
xi = interpolate(startPoint, y, x, endLine, (y - startLine) / h);
// get scaled y position for source image
yi = (y * scale + 0.5)|0;
// draw the slice
ctx.drawImage(img, 0, yi, img.width, 1, // source line
xi.x, y, w - xi.x * 2, 1); // output line
}
// sub-function doing the interpolation
function interpolate(x1, y1, x2, y2, t) {
return {
x: x1 + (x2 - x1) * t,
y: y1 + (y2 - y1) * t
};
}
}
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.