Html 在画布上画一个梯形

Html 在画布上画一个梯形,html,canvas,transformation,Html,Canvas,Transformation,有人能建议如何将画布上的图像从矩形转换为梯形吗 例如,我有一个图像矩形100x200和画布300x300。 然后我想变换我的图像并在以下几点放置角点: 100,0; 200,0; 0,300; 300,300 变换应该重新调整图像的大小以适应新的图形。我明白了,你想做y轴旋转(就像《星球大战》的滚动介绍) 当前画布2d上下文转换矩阵不可能 2d变换矩阵如下所示,最后一个值固定为0,0,1: M11,M21,dx M12,M22,dy 0,0,1 您需要一个y旋转矩阵,如下所示: cosA,0,新

有人能建议如何将画布上的图像从矩形转换为梯形吗

例如,我有一个图像矩形100x200和画布300x300。 然后我想变换我的图像并在以下几点放置角点: 100,0; 200,0; 0,300; 300,300
变换应该重新调整图像的大小以适应新的图形。

我明白了,你想做y轴旋转(就像《星球大战》的滚动介绍)

当前画布2d上下文转换矩阵不可能

2d变换矩阵如下所示,最后一个值固定为0,0,1:

M11,M21,dx

M12,M22,dy

0,0,1

您需要一个y旋转矩阵,如下所示:

cosA,0,新浪

0,1,0

-新浪,0,cosA

但你不能设置-新浪,0,cosA

[先前的答复]

下面介绍如何将包含图像的矩形更改为包含图像的梯形

你必须分别画出秋千的每一条腿。但您可以绘制其中3条边,然后使用closePath()自动绘制第4条边

此代码在矩形和梯形之间设置动画,并缩放剪裁的图像。这段代码假设您希望以一种使缩放图像尽可能大的方式显示图像

下面是代码和小提琴:


正文{背景色:象牙色;填充:20px;}
画布{边框:1px纯红;}
$(函数(){
var canvas=document.getElementById(“canvas”);
var ctx=canvas.getContext(“2d”);
ctx.线宽=5;
window.requestAnimFrame=(函数(回调){
返回window.requestAnimationFrame | | | window.webkitRequestAnimationFrame | | | window.mozRequestAnimationFrame | | window.oRequestAnimationFrame | | window.msRequestAnimationFrame||
函数(回调){
设置超时(回调,1000/60);
};
})();
左var=1.0;
var右=300;
变量大小=.25;
var img=新图像();
img.onload=函数(){
制作动画();
}
img.src=”http://dl.dropbox.com/u/139992952/stackoverflow/KoolAidMan.png";
函数animate(){
//更新比例因子
左+=尺寸;
右-=尺寸;
如果(left100){sizing=-sizing;}
控制台日志(左+“/”+右);
//清除并保存上下文
clearRect(0,0,canvas.width,canvas.height);
ctx.save();
//绘制裁剪梯形
定义类人猿(左、右);
ctx.clip();
//画梯形边框
定义类人猿(左、右);
ctx.stroke();
//画一幅用秋千剪裁的图像
var imgX=左/2;
var imgY=左;
var w=300左;
ctx.drawImage(img,0,0,img.宽度,img.高度,imgX,imgY,w,w);
ctx.restore();
//请求新帧
requestAnimFrame(函数(){
制作动画();
});
}
制作动画();
函数defineTrapezoid(左、右){
ctx.beginPath();
ctx.moveTo(左,0);
ctx.lineTo(右,0);
ctx.lineTo(300300);
ctx.lineTo(0300);
ctx.closePath();
}        
}); // end$(函数(){});

我明白了,你想做y轴旋转(就像《星球大战》滚动介绍)

当前画布2d上下文转换矩阵不可能

2d变换矩阵如下所示,最后一个值固定为0,0,1:

M11,M21,dx

M12,M22,dy

0,0,1

您需要一个y旋转矩阵,如下所示:

cosA,0,新浪

0,1,0

-新浪,0,cosA

但你不能设置-新浪,0,cosA

[先前的答复]

下面介绍如何将包含图像的矩形更改为包含图像的梯形

你必须分别画出秋千的每一条腿。但您可以绘制其中3条边,然后使用closePath()自动绘制第4条边

此代码在矩形和梯形之间设置动画,并缩放剪裁的图像。这段代码假设您希望以一种使缩放图像尽可能大的方式显示图像

下面是代码和小提琴:


正文{背景色:象牙色;填充:20px;}
画布{边框:1px纯红;}
$(函数(){
var canvas=document.getElementById(“canvas”);
var ctx=canvas.getContext(“2d”);
ctx.线宽=5;
window.requestAnimFrame=(函数(回调){
返回window.requestAnimationFrame | | | window.webkitRequestAnimationFrame | | | window.mozRequestAnimationFrame | | window.oRequestAnimationFrame | | window.msRequestAnimationFrame||
函数(回调){
设置超时(回调,1000/60);
};
})();
左var=1.0;
var右=300;
变量大小=.25;
var img=新图像();
img.onload=函数(){
制作动画();
}
img.src=”http://dl.dropbox.com/u/139992952/stackoverflow/KoolAidMan.png";
函数animate(){
//更新比例因子
左+=尺寸;
右-=尺寸;
如果(left100){sizing=-sizing;}
控制台日志(左+“/”+右);
//清除并保存上下文
clearRect(0,0,canvas.width,canvas.height);
ctx.save();
//绘制裁剪梯形
定义类人猿(左、右);
ctx.clip();
//画梯形边框
定义类人猿(左、右);
ctx.stroke();
//画一幅用秋千剪裁的图像
var imgX=左/2;
var imgY=左;
var w=300左;
ctx.drawImage(img,0,0,img.宽度,img.高度,imgX,imgY,w,w);
ctx.restore();
//请求新帧
requestAnimFrame(函数(){
制作动画();
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; padding:20px;}
    canvas{border:1px solid red;}
</style>

<script>
    $(function(){

        var canvas=document.getElementById("canvas");
        var ctx=canvas.getContext("2d");
        ctx.lineWidth=5;

        window.requestAnimFrame = (function(callback) {
          return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
          function(callback) {
            window.setTimeout(callback, 1000 / 60);
          };
        })();

        var left=1.0;
        var right=300;
        var sizing=.25;

        var img=new Image();
        img.onload=function(){
          animate();
        }
        img.src="http://dl.dropbox.com/u/139992952/stackoverflow/KoolAidMan.png";

        function animate() {

          // update scaling factors
          left+=sizing;
          right-=sizing;
          if(left<0 || left>100){sizing = -sizing;}
          console.log(left+"/"+right);


          // clear and save the context
          ctx.clearRect(0, 0, canvas.width, canvas.height);
          ctx.save();

          // draw the clipping trapezoid
          defineTrapezoid(left,right);
          ctx.clip();

          // draw trapezoid border
          defineTrapezoid(left,right);
          ctx.stroke();

          // draw image clipped in trapeze
          var imgX=left/2;
          var imgY=left;
          var w=300-left;
          ctx.drawImage(img,0,0,img.width,img.height,imgX,imgY,w,w);

          ctx.restore();

          // request new frame
          requestAnimFrame(function() {
            animate();
          });
        }
        animate();

        function defineTrapezoid(left,right){
            ctx.beginPath();
            ctx.moveTo(left,0);
            ctx.lineTo(right,0);
            ctx.lineTo(300,300);
            ctx.lineTo(0,300);
            ctx.closePath();
        }        

    }); // end $(function(){});
</script>

</head>

<body>

<canvas id="canvas" width=300 height=300></canvas>


</body>
</html>