【问题标题】:HTML Canvas: Rotate the Image 3D effectHTML Canvas:旋转图像 3D 效果
【发布时间】:2013-04-17 03:08:25
【问题描述】:

如何旋转图像(例如 45 度)并挤压图像。假设我有一个完美的方形图像。我可以将它旋转到我想要的任何角度,但我想让旋转的正方形被压扁,使高度比宽度小 2/3。生成的图像将不是一个完美的旋转正方形,而是一个压扁的正方形。

你知道我怎样才能达到效果吗?

【问题讨论】:

    标签: html canvas


    【解决方案1】:

    压扁一个正方形非常容易,只需应用一个比例:

    ctx.scale(1, 2/3); // squish it to 2/3 vertical size

    不过,您必须通过 (opposite fraction * the height) / 2 对其进行翻译才能使其居中。

    所以要旋转然后挤压一个 200x200 的方形图像,您只需:

    // rotation first
    ctx.translate(100,100);
    ctx.rotate(.3);
    ctx.translate(-100,-100);
    
    // than scale
    ctx.translate(0,200 * (1/3) / 2) // move by half of the 1/3 space to center it
    ctx.scale(1, 2/3); // squish it to 2/3 vertical size
    
    ctx.drawImage(img, 0,0);
    

    示例:http://jsfiddle.net/simonsarris/3Qr3S/

    【讨论】:

    • +1 为 OP 提供了他所要求的,但我认为他实际上是在寻找使用 2D 失真来“伪造”3D。如果我误解了,请原谅我踩到脚趾。顺便说一句,我在新罕布什尔州彼得伯勒住了 3 年——不错的地区!
    • 您好,感谢您的代码。你能给我看一个旋转到 90 度或 45 度但高度(从上到下)比宽度(从左到右)相对于平面/水平线小 1/3 的猫样本吗? (我希望我问的问题是正确的)
    【解决方案2】:

    您可以使用 2D 画布通过扭曲宽度与高度来“伪造”3d

    通过使用 context.drawImage 并不成比例地改变宽度和高度来做到这一点

    // draw image increasingly "squashed"
    // to fake a 3d effect
    ctx.drawImage(img,0,0,img.width,img.height,
        left,
        10,
        (300-(right-left))/1,
        300-(right-left)/1.5);
    

    您可以使用失真率来获得不同的效果,但这只是“挤压”。

    这是代码和小提琴:http://jsfiddle.net/m1erickson/J2WfS/

    <!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");
    
            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="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 image increasingly "squashed"
              // to fake a 3d effect
              ctx.drawImage(img,0,0,img.width,img.height,
                  left,
                  10,
                  (300-(right-left))/1,
                  300-(right-left)/1.5);
    
              ctx.restore();
    
              // request new frame
              requestAnimFrame(function() {
                animate();
              });
            }
            animate();
    
        }); // end $(function(){});
    </script>
    
    </head>
    
    <body>
        <canvas id="canvas" width=300 height=235></canvas>
    </body>
    </html>
    

    【讨论】:

    • 您好,感谢您的代码和帮助。我想要的是,我想要一个图像被旋转......就像 0 到 360 度......但我想从上到下被完全压扁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多