<small id='P2B0O'></small><noframes id='P2B0O'>

      <bdo id='P2B0O'></bdo><ul id='P2B0O'></ul>

    1. <legend id='P2B0O'><style id='P2B0O'><dir id='P2B0O'><q id='P2B0O'></q></dir></style></legend>
    2. <tfoot id='P2B0O'></tfoot>
    3. <i id='P2B0O'><tr id='P2B0O'><dt id='P2B0O'><q id='P2B0O'><span id='P2B0O'><b id='P2B0O'><form id='P2B0O'><ins id='P2B0O'></ins><ul id='P2B0O'></ul><sub id='P2B0O'></sub></form><legend id='P2B0O'></legend><bdo id='P2B0O'><pre id='P2B0O'><center id='P2B0O'></center></pre></bdo></b><th id='P2B0O'></th></span></q></dt></tr></i><div id='P2B0O'><tfoot id='P2B0O'></tfoot><dl id='P2B0O'><fieldset id='P2B0O'></fieldset></dl></div>

      如何旋转 HTML5 画布的现有内容?

      时间:2023-10-13

          <small id='MRlnv'></small><noframes id='MRlnv'>

          <tfoot id='MRlnv'></tfoot>
          <legend id='MRlnv'><style id='MRlnv'><dir id='MRlnv'><q id='MRlnv'></q></dir></style></legend>

          1. <i id='MRlnv'><tr id='MRlnv'><dt id='MRlnv'><q id='MRlnv'><span id='MRlnv'><b id='MRlnv'><form id='MRlnv'><ins id='MRlnv'></ins><ul id='MRlnv'></ul><sub id='MRlnv'></sub></form><legend id='MRlnv'></legend><bdo id='MRlnv'><pre id='MRlnv'><center id='MRlnv'></center></pre></bdo></b><th id='MRlnv'></th></span></q></dt></tr></i><div id='MRlnv'><tfoot id='MRlnv'></tfoot><dl id='MRlnv'><fieldset id='MRlnv'></fieldset></dl></div>

              <bdo id='MRlnv'></bdo><ul id='MRlnv'></ul>

                  <tbody id='MRlnv'></tbody>
                本文介绍了如何旋转 HTML5 画布的现有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有没有办法通过 Javascript 来旋转 HTML5 画布的现有内容?我知道可以旋转将要绘制到画布上的图像,但我想旋转已绘制到画布上的内容,例如 400x400 画布的 200x200 角,或现有画布的任何特定区域.

                Is there a way to rotate the existing content of HTML5 canvas by Javascript? I know it's possible to rotate an image that will be drawn on to canvas, but I want to rotate the content that has been drawn on to canvas, for example, a 200x200 corner of a 400x400 canvas, or any specific region of an existing canvas.

                缩放现有画布内容的相同问题...

                Same question to scale the existing canvas content...

                我知道 getImageData/putImageData 提供了转换像素数组的潜力,但它太慢且效率低下.

                I know getImageData/putImageData provide a potential to transform the pixel array, but it's just too slow and inefficient.

                推荐答案

                使用临时画布非常容易.

                It's pretty easy to do with a temp canvas.

                现场演示

                现场演示动画(只是为了见鬼)

                Live Demo Animated (just for the heck of it)

                上面的例子画了 2 个盒子,然后在 0,0 到 200,200 之间旋转和缩放

                The above example draws 2 boxes, then rotates and scales from 0,0 to 200,200

                var canvas = document.getElementById("canvas"),
                    ctx = canvas.getContext("2d");
                
                canvas.width = canvas.height = 400;
                
                // fill the canvas black, and draw 2 boxes
                ctx.fillStyle = "#000";
                ctx.fillRect(0,0,400,400);
                ctx.fillStyle = "rgb(255,0,0)";
                
                ctx.fillRect(10,10,190,190);
                ctx.fillStyle = "rgb(255,255,0)";
                ctx.fillRect(250,250,90,90);
                
                // Create a temp canvas to store our data (because we need to clear the other box after rotation.
                var tempCanvas = document.createElement("canvas"),
                    tempCtx = tempCanvas.getContext("2d");
                
                tempCanvas.width = canvas.width;
                tempCanvas.height = canvas.height;
                // put our data onto the temp canvas
                tempCtx.drawImage(canvas,0,0,canvas.width,canvas.height);
                
                // Append for debugging purposes, just to show what the canvas did look like before the transforms.
                document.body.appendChild(tempCanvas);
                
                // Now clear the portion to rotate.
                ctx.fillStyle = "#000";
                ctx.fillRect(0,0,200,200);
                ctx.save();
                // Translate (190/2 is half of the box we drew)
                ctx.translate(190/2, 0);
                // Scale
                ctx.scale(0.5,0.5);  
                // Rotate it
                ctx.rotate(45*Math.PI/180);
                // Finally draw the image data from the temp canvas.
                ctx.drawImage(tempCanvas,0,0,200,200,10,10,190,190);
                ctx.restore();
                

                这篇关于如何旋转 HTML5 画布的现有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:用html的两侧翻转div 下一篇:旋转后调整大小时如何计算平移x和y值..?

                相关文章

                最新文章

                <i id='ocQ1N'><tr id='ocQ1N'><dt id='ocQ1N'><q id='ocQ1N'><span id='ocQ1N'><b id='ocQ1N'><form id='ocQ1N'><ins id='ocQ1N'></ins><ul id='ocQ1N'></ul><sub id='ocQ1N'></sub></form><legend id='ocQ1N'></legend><bdo id='ocQ1N'><pre id='ocQ1N'><center id='ocQ1N'></center></pre></bdo></b><th id='ocQ1N'></th></span></q></dt></tr></i><div id='ocQ1N'><tfoot id='ocQ1N'></tfoot><dl id='ocQ1N'><fieldset id='ocQ1N'></fieldset></dl></div>
                <legend id='ocQ1N'><style id='ocQ1N'><dir id='ocQ1N'><q id='ocQ1N'></q></dir></style></legend>

                <tfoot id='ocQ1N'></tfoot>
                • <bdo id='ocQ1N'></bdo><ul id='ocQ1N'></ul>

                    <small id='ocQ1N'></small><noframes id='ocQ1N'>