我想用画布画一个梯形的图像。
我尝试了转换,但我没有得到梯形视图。
请给我一个解决方案,用画布在梯形视图中绘制图像。
我希望实际的图像应该像这个/_\一样被转换
发布于 2014-05-13 09:22:40
下面是如何将图像“转换”成梯形形状的方法。该技术是众所周知的切片图像线为线,并绘制的线,一点一点地缩放。
此函数允许设置梯形形状的数量(%),并处理缩放图像:
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
};
}
}小提琴

希望这能有所帮助!
https://stackoverflow.com/questions/23624269
复制相似问题